0

I have an array with about 500+ elements. These elements will be checked in a function and then I will have to grab data from an API for each element (every element is one query), that does not allow me that much requests in a short time. I will have, to run a delayed loop, that will very likely exceed 30 secs.

What I want is, that my PHP-script should do a certain amount of checks/requests and remove from the "todo"-list and then self refresh and continue the jobafter ~2 sec.

A cronjob will start this php-script.

How can I manage PHP to restart a script after "work is done" or after some kind of "failure" occurs? It depends on this thing, on how I store the data from the "todo-list"-array into either a file, or a $_SESSION. Don't want to store this into a DB.

How can I solve this without the need to setup something on the server, or outside of the script itself?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
wingman
  • 21
  • 2
  • I don't think you can. What you want is possibly something like a queue which you can push the job to in chunks and the worker will just pick up and run with it. If you use a framework such as laravel, they already have something build in for queues. If not, you probably have to find something external. – user663976 Jun 09 '20 at 12:24

1 Answers1

1

PHP life cycle is a request based, i.e. it can't be refreshed by itself.

You can either use:

  1. The cronjob to do some work on the background
  2. The Javascript on timer to launch a new request from the browser to the web server, which will execute the PHP API and return the new result. For example:
<script>
function onTimerRefreshFromApi() {
    var request = new XMLHttpRequest();
    request.open('GET', '/api/url', true);

    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            // On Success replace html
            element.innerHTML = this.response;
        } else {
            // We reached our target server, but it returned an error
        }
    };

    request.onerror = function () {
        // There was a connection error of some sort
    };

    request.send();
}

setInterval(onTimerRefreshFromApi, 1000);
</script>
  1. Use Ajax on timer in similar way as 2).
  2. Use headers to instruct the browser to do refresh as described: Refresh a page using PHP
  • Since a cronejob will start it, I can't use javascript/ajax or headers through browsers. thx for answering, but I have to find a solution in truly PHP. I thought about setting the exectiontime to 0, and then let the script load an other script file? and after it's done, to close the first script? – wingman Jun 09 '20 at 15:19
  • Yes, `set_time_limit (0)` will help you to prevent time out and allow php script to run over default 30 seconds time out. – Svetlozar Stoyanov Jun 09 '20 at 16:11
  • Of course, if the cron job start the `first.php`, then it can start `second.php` or you can spawn more php none blocking scripts https://stackoverflow.com/questions/18152442/spawning-a-php-script-from-inside-another-non-blockning . I don't undersand why you want the `first.php` to close the `second.php`, because the later will finish when it's ready. If you want a script to wait for 2 seconds before continue, then you can use `sleep(2)`. – Svetlozar Stoyanov Jun 09 '20 at 16:25