I am working on a emailer system that I have to plug in a CMS for a company. I am looking for a way to make the email sender script run in background while the admin may navigate or even close the browsers.
I found the PHP开发者_运维技巧 function named ignore_user_abort()
which is required to keep the page running even if it has timed-out.
Now I have three solutions to "start the script" :
I could use an iframe
I could use an Ajax call which I have previously configured to timeout very early. Eg: using the jQuery framework :
$.ajaxSetup({ timeout: 1000 });
I could use a cron job but I would like to avoid this solution since they are "virtual" and are unstable on that server.
Is there any other solutions? I dont really like the iframe solution but I already use them with an Ajax uploader script.
I dont want the admin to hit F5 and start a second instance of it.
The company's users have been told to only log in the CMS using Firefox. Thank you
You can run a PHP script in the background using Exec()
.
php docs provide an example on how you can do that:
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
If I were you, I would write a background daemon to do this. I would save all my emails to a database in the original HTTP call and have a script constantly running in the background that checks every, say, 30 seconds to see if there are any pending emails and then sends them.
This would also allow you to check for duplicates when you add emails to the queue, so you needn't worry about multiple additions.
Edit Given your reason for not using cron jobs (i.e. you don't have shell access to the server) I wouldn't use this approach.
Instead I would have a first AJAX call where the PHP inserted all the emails into a database, and returned a key to the Javascript. I would then have a Javascript call, using that key, where PHP processed emails for 5 seconds, then returned the number of emails remaining to the browser. I would then have Javascript keep repeating this request until there were no emails remaining.
This would have the additional benefit that you could have progress notifications and, if the server crashed for whatever reason, your emails would be recoverable.
Just because you close the browser the script started by php should not stop executing. So as I see it you have a couple of ways to do this:
Send a POST to the server telling it you want to do some action, and the script executes until it finishes. Reloading the page will warn the browser about that you will need to resend data etc... You could do the POST asynchronously with javascript to prevent this and to be able to do other things while the script is executing. One thing to watch out for here is that the script execution time might be limited.
Send a POST to a script that in turn starts a background system process doing the same thing. But now you will not be able to find out the progress unless you save the state somewhere, in a database or something
Cron (you already seem to have explored this option)
精彩评论