日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php resque 计划任务,PHP-RESQUE - 实现重试

發布時間:2023/12/1 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php resque 计划任务,PHP-RESQUE - 实现重试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

因為PHP-Resque 的重試部分需要自己寫,網上又沒啥輪子,而且resque也已經很久不更新了,所以自己研究下resque的源碼,然后也借鑒了Laravel的隊列重試機制,實現了PHP-Resque的重試機制。

Resque地址

設計思路

1.這里需要閱讀resque源碼,resque的worker把失敗的隊列的數據都放在了"resque:failed"列表,數據如下。

2.我的項目是在yii2下統一處理resque的,打算開一個守護進程不斷重試這些失敗的隊列。

3.我直接resque源碼在新增了retry方法,在每一個failJob的payload增加了attempts嘗試次數,

一開始創建的時候attempts為0,重試之后attempts每次加1然后進行retry,直到到達嘗試數次才停止重試。

核心代碼

調用重試的代碼

/**

* 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;

}

這里可以弄成守護進程一直處理失敗的隊列任務。

/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 the job.

* 'args' => Any optional arguments that should be passed when the job is executed.''

* 'attempts'=> 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;

}

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 the job.

* 'args' => Any optional arguments that should be passed when the job is executed.''

* 'attempts'=> 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(

'Supplied $args must be an array.'

);

}

$jobArray['attempts']++;

Resque::push($queue, array(

'class' => $jobArray['class'],

'args' => $args,

'id' => $jobArray['id'],

'attempts'=>$jobArray['attempts']

));

return true;

}

結果:

我這里我設置了重試次數為3次,每隔5秒處理一個隊列。

注意

1. 只有任務JOB實現類出現異常才會被重新扔回到"resque:failed"隊列里面。

2. 對于正常的業務錯誤不需要重試,目前只考慮到對curl會出現的異常進行重試。

總結

以上是生活随笔為你收集整理的php resque 计划任务,PHP-RESQUE - 实现重试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。