PHP- Need a cron for back site processing on user signup... (or fork process) PHP- Need a cron for back site processing on user signup... (or fork process) apache apache

PHP- Need a cron for back site processing on user signup... (or fork process)


I would recommend neither solution.

Instead, you would be best off with a long running process (daemon) that gets its jobs from a message queue. The message queue itself could be off a database if that is your preferred method.

You will post an identifier for the job to your database, and then a long running process will iterate through them once in a while and act upon them.

This is as simple as:

<?phpwhile(true) {   jobs = getListOfJobsFromDatabase();  // get the jobs from the databbase   foreach (jobs as job) {      processAJob(job); // do whatever needs to be done for the job      deleteJobFromDatabase(job); //remember to delete the job once its done!   }   sleep(60); // sleep for a while so it doesnt thrash your database when theres nothing to do}?>

And just run that script from the command line.

The benefits of this over a cron job are that you wont get a race condition.

You may also want to fork off the actually processing of the jobs so many can be done in parallel, rather than processing sequentially.


You can use the following class to invoke a background PHP task.

class BackgroundProcess {    static function open($exec, $cwd = null) {        if (!is_string($cwd)) {            $cwd = @getcwd();        }        @chdir($cwd);        if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {            $WshShell = new COM("WScript.Shell");            $WshShell->CurrentDirectory = str_replace('/', '\\', $cwd);            $WshShell->Run($exec, 0, false);        } else {            exec($exec . " > /dev/null 2>&1 &");        }    }    static function fork($phpScript, $phpExec = null) {        $cwd = dirname($phpScript);        if (!is_string($phpExec) || !file_exists($phpExec)) {            if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {                $phpExec = str_replace('/', '\\', dirname(ini_get('extension_dir'))) . '\php.exe';                if (@file_exists($phpExec)) {                    BackgroundProcess::open(escapeshellarg($phpExec) . " " . escapeshellarg($phpScript), $cwd);                }            } else {                $phpExec = exec("which php-cli");                if ($phpExec[0] != '/') {                    $phpExec = exec("which php");                }                if ($phpExec[0] == '/') {                    BackgroundProcess::open(escapeshellarg($phpExec) . " " . escapeshellarg($phpScript), $cwd);                }            }        } else {            if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {                $phpExec = str_replace('/', '\\', $phpExec);            }            BackgroundProcess::open(escapeshellarg($phpExec) . " " . escapeshellarg($phpScript), $cwd);        }    }}

Use as such:

BackgroundProcess::fork('process_user.php');


Here's what i think. Have a single cron for all your users. This way you can assure no overlapping by putting them in a table that works like a queue. Run the cron every hour but check the queue first if the queue is not empty. If it's not skip the cron job for the hour try again the next.