新建了兩個job類,分別是StudyJob
和TeacherJob
//StudyJob
public function handle()
{
echo $this->name . PHP_EOL;
}
//TeacherJob
public function handle()
{
echo $this->teacherId . PHP_EOL;
}
和一個控制器SchoolController
public function TeacherCallStudentsName()
{
$data = [
'張三',
'張4',
'張5',
'張6',
'張7',
'張8',
'張9',
'張10',
'張11',
];
foreach ($data as $k => $v) {
$this->queueRun($v, $k);
}
return [
'code' => 200
];
}
protected function queueRun($name, $id)
{
StudyJob::withChain([
new TeacherJob($id)
])->dispatch($name);
}
然後我先只使用一個消費者,然後請求一下看看結果。
/www/study # php artisan queue:work
[2020-03-23 05:11:35][rVpuaImiVCjcxQZ4w4fTmq9Ir2nQ1GHE] Processing: App\Jobs\StudyJob
張三
[2020-03-23 05:11:35][rVpuaImiVCjcxQZ4w4fTmq9Ir2nQ1GHE] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][utirHewP3d8hzJ4t3m5ZfGcozFM57Yu0] Processing: App\Jobs\StudyJob
張4
[2020-03-23 05:11:35][utirHewP3d8hzJ4t3m5ZfGcozFM57Yu0] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][Tme5Va0YbBAzzlVARxiSLaXPOHcSQ4ss] Processing: App\Jobs\StudyJob
張5
[2020-03-23 05:11:35][Tme5Va0YbBAzzlVARxiSLaXPOHcSQ4ss] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][L1kV3sftYMAERfAay8ltVpA2tDwlEvt4] Processing: App\Jobs\StudyJob
張6
[2020-03-23 05:11:35][L1kV3sftYMAERfAay8ltVpA2tDwlEvt4] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][slXfov07kVbdLD2fqFyByJWDQrnEG1rV] Processing: App\Jobs\StudyJob
張7
[2020-03-23 05:11:35][slXfov07kVbdLD2fqFyByJWDQrnEG1rV] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][6zUrmOsUNNDJBPpkVCEBLa1w1LRULQdQ] Processing: App\Jobs\StudyJob
張8
[2020-03-23 05:11:35][6zUrmOsUNNDJBPpkVCEBLa1w1LRULQdQ] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][6hiIynhB7VxeTTrnzE17JS3Pjrq3Ii9G] Processing: App\Jobs\StudyJob
張9
[2020-03-23 05:11:35][6hiIynhB7VxeTTrnzE17JS3Pjrq3Ii9G] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][Qg7iqCjeVGlTb41mjRWOU7noab5tJBR1] Processing: App\Jobs\StudyJob
張10
[2020-03-23 05:11:35][Qg7iqCjeVGlTb41mjRWOU7noab5tJBR1] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][5i2qt00sPRjfVSQMB3vP1T0GnZz50sQc] Processing: App\Jobs\StudyJob
張11
[2020-03-23 05:11:35][5i2qt00sPRjfVSQMB3vP1T0GnZz50sQc] Processed: App\Jobs\StudyJob
[2020-03-23 05:11:35][n8nemYnt1cd0pqePCb8ruKpMLfzPkDbZ] Processing: App\Jobs\TeacherJob
0
[2020-03-23 05:11:35][n8nemYnt1cd0pqePCb8ruKpMLfzPkDbZ] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][ZlHx0kd2usiWnbFzQqa5TpKL9MyorVEn] Processing: App\Jobs\TeacherJob
1
[2020-03-23 05:11:35][ZlHx0kd2usiWnbFzQqa5TpKL9MyorVEn] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][qMoJt5j0X8UVqanMJC5HdbyFu9sDqiu8] Processing: App\Jobs\TeacherJob
2
[2020-03-23 05:11:35][qMoJt5j0X8UVqanMJC5HdbyFu9sDqiu8] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][ST8ql3ah1l9sfXItj4XAIdFvnMLNmy3x] Processing: App\Jobs\TeacherJob
3
[2020-03-23 05:11:35][ST8ql3ah1l9sfXItj4XAIdFvnMLNmy3x] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][rfX0D29sDPlI8uB4cw3qH6ncxStdNPQA] Processing: App\Jobs\TeacherJob
4
[2020-03-23 05:11:35][rfX0D29sDPlI8uB4cw3qH6ncxStdNPQA] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][KtXnkUkEo3hOVP9QB4HEl3nnzydeawBf] Processing: App\Jobs\TeacherJob
5
[2020-03-23 05:11:35][KtXnkUkEo3hOVP9QB4HEl3nnzydeawBf] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][eDFQVAcg9aT8He2Otpho5fpFd5a1fRvQ] Processing: App\Jobs\TeacherJob
6
[2020-03-23 05:11:35][eDFQVAcg9aT8He2Otpho5fpFd5a1fRvQ] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][8t0CqhPgUgYqWthyExv78OrATbazBIaw] Processing: App\Jobs\TeacherJob
7
[2020-03-23 05:11:35][8t0CqhPgUgYqWthyExv78OrATbazBIaw] Processed: App\Jobs\TeacherJob
[2020-03-23 05:11:35][bxDhuU6F12EAkuzIEwNmQwemYbTfgd8Y] Processing: App\Jobs\TeacherJob
8
[2020-03-23 05:11:35][bxDhuU6F12EAkuzIEwNmQwemYbTfgd8Y] Processed: App\Jobs\TeacherJob
我們可以看到任務連結保證了佇列的先後順序,先是老師呼叫了學生名字,然後列印老師id。那麼消費者不止一個的時候,是否任務連結可以保證消費順序呢?
我發現粘連複製控制檯輸出,樣式不好看,直接截圖吧。
喪心病狂的我,開了三個消費者,然後狂點了兩次postman,下面分別是第一次和第二次的截圖。
然後我們會看到,依舊是先是老師呼叫了學生名字,然後列印老師id。但是消費者順序卻沒有得到保證,當然這個就看業務場景是否嚴格了。
那麼怎麼讓消費者也乖乖按順序消費,看佇列順序性引發的思考 就好了。
文件中任務連結是這樣描述的:
任務連結允許您指定一組應在主任務成功執行後按順序執行的排隊任務。如果序列中的一個任務失敗,其餘的任務將不會執行。要執行一個排隊任務鏈,你可以在你的任何可排程任務上使用 withChain 方法:
它是讓不同的任務,有一個順序。而不是說該任務中,消費者順序消費,是兩個不一樣的概念。
如有錯誤,歡迎指出。
本作品採用《CC 協議》,轉載必須註明作者和本文連結