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)
监听无法启动,请问怎么解决?
自己配置少了-- 问题已解决!
.env配置了吗