PHP - you need cron to process the reverse site when registering the user ... (or the fork process)

When a new user registers with my site, I want to do some preprocessing to shorten their searches in the future. This is due to a processing time of 30 to 2 minutes. Obviously, I cannot do this when they click the submit button when registering ... or on any PHP page that they visit. However, I would like this to be done within 5 minutes of signing them (or less).

Cron Route I THINK what this should be in the cron job, and if so, how do I configure cron to work? If so, what should my cron line look like every 2 minutes, and how can I insure that I do not have the same cron job that overlaps the next?

Event / fork route - preferred If I can throw some kind of event on my server without disrupting my users or unlocking the user registration process (instead of the cron job), how can I do this?

+3
source share
3 answers

I would recommend no solution.

(daemon), . , .

, .

, :

<?php
while(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
}
?>

script .

cron , .

, , .

+5

PHP.

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);
        }
    }
}

:

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

That's what I think. You have one cron for all your users. Thus, you can not overlap each other by placing them in a table that works as a queue. Run cron every hour, but check the queue first if the queue is not empty. If he does not miss the cron job in an hour, try the following again.

-1
source

Source: https://habr.com/ru/post/1710304/


All Articles