Because Php-resque retry need to write their own, online and no wheels, and Resque also a long time not updated, so their own research under the Resque source, and then also borrowed from the Laravel queue retry mechanism, the implementation of Php-resque retry mechanism.
Design ideas
Read the Resque source code, we know, resque the failure of the queue data are placed in the resque:failed list, the data is as follows.
My project is in YII2 under the unified processing Resque,
I directly resque the source code in the new retry method, in each Failjob payload increased the number of attempts attempts,
At the beginning of the creation of the attempts is 0, after retrying attempts each add 1 and then retry, until the arrival of the attempt several times to stop retrying.
Core code
1.调用重试的代码
/** * Retry Job */ public function actionRetry() { $redis = new Redis(‘redis‘); while (true) { $json = $redis->rPop(‘resque:failed‘); $array = json_decode($json, true); if ($array) { $jobArray = $array[‘payload‘]; if ($jobArray[‘attempts‘] < $this->retryTimes) { //retry \Resque::retry($jobArray, $array[‘queue‘]); echo "Queued job " . $jobArray[‘id‘] . ‘ has retry!‘ . "\n"; } else { //stop retry $redis->lPush(‘resque:failed‘, [$json]); } } //take a sleep echo "*** Sleeping for ".$this->sleep. "\n"; sleep($this->sleep); } return true; }
这里可以弄成守护进程一直处理失败的队列任务。2./php-resque/lib/Resque.php
/** * Retry job and save it to the specified queue. * * @param array $jobArray The attempts of the the job. * Array (* ' id ' = = the ID of the job * ' class ' = = the name of the class that contains the code to execute T He job. * ' args ' and ' optional arguments that should was passed when the job is executed. ' * ' attempts ' and the retry attempts of the job *) * @param string $queue The name of the queue to place the job In. * * @return Boolean */public static function retry ($jobArray, $queue) {require_once dirname (__file__ ) . '/resque/job.php '; $result = Resque_job::retry ($jobArray, $queue); if ($result) {resque_event::trigger (' afterenqueue ', Array (' class ' = = ' $jobArray [' class '], ' Args ' = $jobArray [' args '], ' queue ' = ' $queue, '); } return true; }
3./php-resque/lib/resque/job.php
/** * Retry job and save it to the specified queue. * * * @param array $jobArray The data of the job. * Array (* ' id ' = = the ID of the job * ' class ' = = the name of the class that contains the code to execute T He job. * ' args ' and ' optional arguments that should was passed when the job is executed. ' * ' attempts ' and the retry attempts of the job *) * @param string $queue The name of the queue to place the job In. * * @return String */public static function retry ($jobArray, $queue) {$args = $jobArray [' args ']; if ($args!== null &&!is_array ($args)) {throw new InvalidArgumentException (' Suppl IED $args must is an array. ' ); } $jobArray [' Attempts ']++; Resque::p ush ($queue, Array (' class ' = = $jobArray [' class '], ' args ' = = Array ($args), ' id ' = $jobArray [' id '], ' attempts ' and ' = ' $jobArray[' attempts ']); return true; }
Results:
I am here I set the retry count to 3 times, processing one queue every 5 seconds.
Php-resque retry mechanism