0

I've made a script that retrieves XML content from a web service. The process needs to be run once a week, but the script itself needs to be re-run about 180 times to complete the process. Each run of the script takes about 3-8 minutes. I would like it to re-run about 5 seconds after each completion.

My current solution for this is:

  • Task scheduler for windows opens the php page once a week.
  • When script is run and completed, javascript makes the page restart 5 seconds after completion.
  • When the last time of the script runs it removes the reload of the page so that it stops.

The problem with this solution is that it opens a new browser window every week. Is there any good alternative ways of doing this without having to manually close down the browser?

The reason of re-run of the script is due to script timeout settings of the php server max limit, and the possibility to after each run to see status whether any error occurred.

I'm not using cron since it would require to do extremely many polls in order to get the process to start within 5 seconds of last time run. For the weekly start up of the script I assume it wouldn't work as long as the script uses javascript to rerun itself?

laplagam
  • 11
  • 1
  • 5
  • I'm certain there's a better solution that doesn't involve opening a browser in order to run javascript. Why is javascript necessary for this rather than calling a function in php 180 times? – wolfemm Nov 05 '14 at 09:16
  • @wolfemm good question. I have, actually done something vaguelly similar in PHP - a script had to run several times to complete a task (bad design but solution was sufficient and anything more would have been an overkill) and what I ended up doing is just forking until there was no more work to be done. I didn't need a browser, though as I was just calling the script from the command line and it took care of the forking itself. Of course there should be care to not blow up the machine but keeping track of state is not really that bad. – VLAZ Nov 05 '14 at 09:45

1 Answers1

1

With PHP:

<?php

// increase the maximum execution time to 43200 seconds (12 hours)
set_time_limit(43200);

function runTask() {
    static $cycles = 0;

    // do whatever you need to do

    // Increments cycle count then compares against limit
    if ($cycles++ < 180)  {
        sleep(5);  // wait five seconds
        runTask(); // run it again
    }
}

runTask(); // fire up the loop

Or, if you're a fan of Javascript...

With node.js:

var cycles = 0;

function runTask() {
  // do whatever you need to do

  // Increments cycle count then compares against limit
  if (cycles++ < 180) {
    setTimeout(runTask, 5000); // run again in 5000 milliseconds
  }
}

runTask(); // fire up the loop

Both solutions will not run the function again until 5 seconds after each iteration has completed.

Just have your task runner execute either script directly; no need for browsers.

wolfemm
  • 993
  • 3
  • 11
  • 16
  • Hi, Thank you for the reply. The problem is that the PHP scripts then times out. In total the script with rerunning the same task/functions over and over again, the script would take about 11-12 hours to complete. – laplagam Nov 06 '14 at 13:30
  • @laplagam I've edited my answer with a line to increase the maximum execution time for PHP. – wolfemm Nov 06 '14 at 22:05
  • Ths cript still times out. The set_time_limit does unfortunately not work beyond I think it is 30 minutes or so. – laplagam Nov 08 '14 at 03:04
  • I've updated the main text for further clarification. My php server denies scripts to run for 12 hours straight, and also I would like the possibility to see status after each time it run whether everything is ok without having to wait 12 hours to find out and they maybe have to re-run everything. – laplagam Nov 10 '14 at 09:28