Reputation: 91
I have some php script updater.php
which does some job for 1-2 minutes. Finishing the script job is important.
I can run it with cron, but i have some idea.
Script will run concrete time only when user gets for example home page. In this case it's unnecessary to execute script every time, only when it's needed.
So, user loads http://localhost/index.php
, js script post small get request:
$.get('/json/updater.php', function(data){ return true; });
Requests proceed good in 1-2 minutes.
The question: Will processing of updater.php
work stop, if user closes the tab in browser and therefore break $.get request?
Upvotes: 0
Views: 297
Reputation: 29975
Requests don't get cancelled if you close the browser window, so PHP will simply be able to complete the script (assuming the execution time doesn't exceed the maximum configured time See: http://php.net/manual/en/function.set-time-limit.php). Of course, anything you do in JavaScript will stop once you close the window.
Upvotes: 4
Reputation: 23
Keep in mind you can execute PHP scripts directly from the command line or with cron, just use the path of your PHP interpreter. For scheduled jobs I would recommend not having any fronted requirements (JavaScript/Browser) as this can cause consistency issues, you want your job to run every 2 minutes without fail and adding another step (AJAX or Browser) introduces the possibility for that frequency to become inconsistent.
Upvotes: 2
Reputation: 218827
Will processing of updater.php work stop, if user closes the tab in browser and therefore break $.get request?
Only if they close the tab before the request is actually sent, which would be fairly quickly, in which case it wouldn't "stop" but would rather "never have started."
Once you receive the GET request from the client, the request itself is done. You can spawn a process on the server and the browser has no control over that. The browser can only stop client-side code from running.
Conversely, why do this on a separate request? If you want the process to be spawned when the page is loaded, why not spawn it from within index.php
? The benefits would include:
Upvotes: 1