Yii2 隊列 shmilyzxt/yii2-queue 簡單概述
shmilyzxt/yii2-queue 簡單解釋:
1.我用的yii2高級版,我們從配置開始看代碼,這里我用的是mysql隊列,首先配置文件,我把queue配置項寫在根目錄common\config\main-local.php下的 components數(shù)組下,更改一下數(shù)據(jù)庫配置.復制composer安裝后復制
vendor\shmilyzxt\yii2-queue\jobs\jobs.sql vendor\shmilyzxt\yii2-queue\failed\failed.sql
2個sql文件到數(shù)據(jù)庫中建立隊列數(shù)據(jù)表和執(zhí)行任務失敗時的數(shù)據(jù)表.
2.推送任務開始語法:\Yii::$app->queue->pushOn(new SendMial(),['email'=>'49783121@qq.com','title'=>'test','content'=>'email test'],'email'); 我們到vendor\shmilyzxt\queue\queues\DatabaseQueue.php去看看代碼,pushOn()方法寫在了DatabaseQueue類的父類vendor\shmilyzxt\queue\base\Queue.php中:
//入隊列
public function pushOn($job, $data = '', $queue = null)
{
//canPush 檢查隊列是否已達最大任務量
if ($this->canPush()) {
//beforePush 入隊列前的事件
$this->trigger(self::EVENT_BEFORE_PUSH);
//入隊列
$ret = $this->push($job, $data, $queue);
//afterPush 入隊列后的事件
$this->trigger(self::EVENT_AFTER_PUSH);
return $ret;
} else {
throw new \Exception("max jobs number exceed! the max jobs number is {$this->maxJob}");
}
}
注釋:這里最好去看看yii2 event事件類,http://www.digpage.com/event.html
關于入隊列: $this->push($job, $data, $queue);,這里在配合queue類文件查看,相關函數(shù)跳轉(zhuǎn),處理一下數(shù)據(jù)記錄到數(shù)據(jù)庫中.(函數(shù)走向:getQueue()-->createPayload()-->pushToDatabase()),pushOn()最終返回數(shù)據(jù)插入數(shù)據(jù)庫的結(jié)果,成功$ret是1.
3.后臺運行命令處理隊列,例:php ./yii worker/listen default 10 128 3 0 其中default是隊列的名稱,上面推送了一個email隊列 應該改為email.
啟動命令后,我們來看代碼:首先執(zhí)行:WorkerController控制器 actionListen方法,我們跟著代碼進入到 vendor\shmilyzxt\queue\Worker.php -- listen方法中,這里其實就是一直在循環(huán),執(zhí)行操作隊列的任務:
/**
* 啟用一個隊列后臺監(jiān)聽任務
* @param Queue $queue
* @param string $queueName 監(jiān)聽隊列的名稱(在pushon的時候把任務推送到哪個隊列,則需要監(jiān)聽相應的隊列才能獲取任務)
* @param int $attempt 隊列任務失敗嘗試次數(shù),0為不限制
* @param int $memory 允許使用的最大內(nèi)存
* @param int $sleep 每次檢測的時間間隔
*/
public static function listen(Queue $queue, $queueName = 'default', $attempt = 10, $memory = 512, $sleep = 3, $delay = 0){
while (true){
try{
//DatabaseQueue從數(shù)據(jù)庫隊列取出一個可用任務(實例),并且更新任務
$job = $queue->pop($queueName);
}catch (\Exception $e){
throw $e;
continue;
}
if($job instanceof Job){
//判斷執(zhí)行錯誤的次數(shù)是否大于傳入的執(zhí)行次數(shù)
if($attempt > 0 && $job->getAttempts() > $attempt){
$job->failed();
}else{
try{
//throw new \Exception("test failed");
$job->execute();
}catch (\Exception $e){
//執(zhí)行失敗,判斷是否被刪除,重新入隊
if (! $job->isDeleted()) {
$job->release($delay);
}
}
}
}else{
self::sleep($sleep);
}
if (self::memoryExceeded($memory)) {
self::stop();
}
}
}
注釋:在$queue->pop($queueName);是vendor\shmilyzxt\queue\queues\DatabaseQueue.php方法內(nèi)使用事務執(zhí)行SQL,并且創(chuàng)建vendor\shmilyzxt\queue\jobs\DatabaseJob.php的實例
//取出一個任務
public function pop($queue = null)
{
$queue = $this->getQueue($queue);
if (!is_null($this->expire)) {
//$this->releaseJobsThatHaveBeenReservedTooLong($queue);
}
$tran = $this->connector->beginTransaction();
//判斷是否有一個可用的任務需要執(zhí)行
if ($job = $this->getNextAvailableJob($queue)) {
$this->markJobAsReserved($job->id);
$tran->commit();
$config = array_merge($this->jobEvent, [
'class' => 'shmilyzxt\queue\jobs\DatabaseJob',
'queue' => $queue,
'job' => $job,
'queueInstance' => $this,
]);
return \Yii::createObject($config);
}
$tran->commit();
return false;
}
至于:$job->execute();是DatabaseJob繼承父類Job執(zhí)行的,順著代碼找下去是yii\base\Component trigger執(zhí)行的事件,
/**
* 執(zhí)行任務
*/
public function execute()
{
$this->trigger(self::EVENT_BEFORE_EXECUTE, new JobEvent(["job" => $this, 'payload' => $this->getPayload()]));//beforeExecute 執(zhí)行任務之前的一個事件 在JobEvent中并沒有什么可執(zhí)行的代碼
$this->resolveAndFire();//真正執(zhí)行的任務的方法
}
/**
* 真正任務執(zhí)行方法(調(diào)用hander的handle方法)
* @param array $payload
* @return void
*/
protected function resolveAndFire()
{
$payload = $this->getPayload();
$payload = unserialize($payload); //反序列化數(shù)據(jù)
$type = $payload['type'];
$class = $payload['job'];
if ($type == 'closure' && ($closure = (new Serializer())->unserialize($class[1])) instanceof \Closure) {
$this->handler = $this->getHander($class[0]);
$this->handler->closure = $closure;
$this->handler->handle($this, $payload['data']);
} else if ($type == 'classMethod') {
$payload['job'][0]->$payload['job'][1]($this, $payload['data']);
} else if ($type == 'staticMethod') {
$payload['job'][0]::$payload['job'][1]($this, $payload['data']);
} else {//執(zhí)行的`SendMail`類的`handle($job,$data)`方法
$this->handler = $this->getHander($class);
$this->handler->handle($this, $payload['data']);
}
//執(zhí)行完任務后刪除
if (!$this->isDeletedOrReleased()) {
$this->delete();
}
}
最后到了執(zhí)行的SendMail類的handle($job,$data),在這里就是推送到隊列的對象和數(shù)據(jù),接著就是我們的處理邏輯了.
public function handle($job,$data)
{
if($job->getAttempts() > 3){
$this->failed($job);
}
$payload = $job->getPayload();
echo '<pre>';print_r($payload);
//$payload即任務的數(shù)據(jù),你拿到任務數(shù)據(jù)后就可以執(zhí)行發(fā)郵件了
//TODO 發(fā)郵件
}
總結(jié)
以上所述是小編給大家介紹的Yii2 隊列 shmilyzxt/yii2-queue簡介,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
jQuery+Ajax+PHP“喜歡”評級功能實現(xiàn)代碼
本文基于jQuery,通過PHP與mysql實現(xiàn)了一個評級功能,是一個簡單的非常好的ajax應用實例,需要的朋友可以參考下2015-10-10
php 利用array_slice函數(shù)獲取隨機數(shù)組或前幾條數(shù)據(jù)
這篇文章主要介紹了php 利用array_slice函數(shù)獲取隨機數(shù)組或前幾條數(shù)據(jù)的相關資料,需要的朋友可以參考下2015-09-09
PHP獲取不了React Native Fecth參數(shù)的解決辦法
這篇文章的主要內(nèi)容是解決PHP獲取不了React Native Fecth參數(shù)的問題,本文通過示例詳細解釋如何解決這個問題,相信對大家的理解更有幫助,如果有這個問題的可以參考下本文,下面跟著小編一起來看看。2016-08-08

