1、修改config資料夾中的queue裡面的設定為:
'default' => env('QUEUE_DRIVER', 'database'),
/*
|--------------------------------------------------------------------------
| Queue Connections
|--------------------------------------------------------------------------
|
| Here you may configure the connection information for each server that
| is used by your application. A default configuration has been added
| for each back-end shipped with Laravel. You are free to add more.
|
*/
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'table' => 'jobs',/*在数据库的配置中有定义数据表前缀为ksb_,这里不知是否需要补全前缀*/
'queue' => 'default',
'expire' => 60,
],
……………………
2、建立隊列表
php artisan queue:table
php artisan migrate
#3、建立佇列任務類別
php artisan make:job CollectionBook
##4、推送任務到佇列
$job = (new CollectionBook(3565))->onQueue('collectionbook')->delay(60);
dispatch($job);
5、後台監聽
php artisan queue:work --queue=collectionbook --daemon --sleep=3 --tries=3
運行推送任務的頁面
發現佇列被同步運行了,並沒有寫入佇列任務表裡面,這個要怎麼解決?
在網站根目錄(/home/wwwroot/kshuba/)下執行
php artisan queue:work --queue=collectionbook --daemon --sleep=3 --tries=3
資料庫內的佇列任務被消費
此時查看 ps artisan 會看見行程為:
root 2287 2144 9 18:30 pts/0 00:00:02 php artisan queue:work --queue=collectionbook --daemon --sleep=3 --tries=3
##但是當斷開ssh客戶端時發現進程已經不見了
這時候採用supervisor監聽進程
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/wwwroot/kshuba/artisan queue:work --queue=collectionbook --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
啟動supervisor,並查詢
結果為監聽中
laravel-worker-collectionbook RUNNING pid 2271, uptime 0:00:02
ps artisan 會看見進程為:
root 2300 2144 9 18:30 pts/0 00:00:02 php /home/wwwroot/kshuba/artisan queue:work --queue=collectionbook --daemon --sleep=3 --tries= 3
問題來了查詢資料庫內的佇列任務,並沒有被消耗掉
另外如果在伺服器根目錄下執行
php /home/wwwroot/kshuba/artisan queue:work --queue=collectionbook --daemon --sleep=3 --tries=3
隊列任務一樣無法被消費
嘗試將supervisor的commend改為:
php artisan queue:work --queue=collectionbook --sleep=3 --tries=3 --daemon
發現supervisor> status 會提示:BACKOFF Exited too quickly (process log may have details)
監聽無法啟動,請問怎麼解決?
自己配置少了-- 問題已解決!