tags:

views:

47

answers:

2

i have develop a eblast application

The program is use to send a email to some recipients

the recipients email will be grab from a xls file

and the program has set it to send 10 email each time and sleep 30 seconds

and use ob_flush(); and flush(); to out put the steam of the process and display in frontend

yesterday my client test it with 9000 recipients (it should take arround 10hours)

and he told me the program has stop, and i found the log file has mark that the program has stopped at 65XX emails,

that mean the program has already sent 6XXX email (arround 7hour)

and this problem will never happen in cron job,but only happen when exec though the web browser

my frd told me because it is all about long time sleep?

and he suggest to use cron job, however my application already has cron job to set,

the client just want to have a feature to send the email immediately

any other solution to solve? use php call a linux command and excu a php email sending script?

A: 

Long running processes in Apache or IIS are tricky. The problem is if anything happens like a restart of the webserver or a timeout you lose your work. You are better off keeping this simple and making into a cron job but if you are up for the challenge it is possible to get around.

I've gotten around occasional webserver restarts by saving the state of my process into a database and a script that continually hits the page to check if it's up and working. So when the long running process first loads it checks if it should be running and if it should continue a job or not. In your case that might be the line number of the excel file.

It ends up being lot of extra work and you need to be very careful. From the sounds of your project I would keep it simple by going the cron job route you mentioned.

Jason Rowe
assume that i dont have the case for server restartand i have already save the process status in db on each loop of my programi found that will it be a solution when using ignore_user_abort(true)? im not surin your suggection, what do u mean for "a script that continually hits the page to check if it's up and working", it will be a client side script? or a cronjob to check?i have already in purpose to mislead my client, and tell him because lack of the resource of his browser, so the porgram stop, try to use cronjob, hahahahbut i still want to found out the solution
A: 

My solution is, try to set your cronjob to run every minutes. However, you should save the state of your cronjob so that it didn't run twice.

I usually do it this way (Note that this cron is intended to run every minute):

if(stat_check_file('cron.stat'))
{
     die("Found CRON.STAT, Exit!");
}
else
{
    stat_create_stat_file('cron.stat');
    //do your long process here...
}
stat_delete_stat_file('cron.stat');

function stat_check_file($filename)
{
    global $rootdir;

    return file_exists($rootdir.'/'.$filename);
}

function stat_create_stat_file($filename){
    global $rootdir;

    touch($rootdir.'/'.$filename);
}

function stat_delete_stat_file($filename)
{
    global $rootdir;

    if(stat_check_file($filename))
    {
        @unlink($rootdir.'/'.$filename);
    }
}

Now, on your cronjob, simply load the xls, run it and write log to either database / file. then, on your panel, read that log and display it so that your client will see right now, there's xxx email sent and xxx email to go.

silent