首先使用supervior管理Laravel队列进程主要是由于当代码异常时候会自动重启Laravel队列。
supervisor安装很简单,这里的服务器系统为Ubuntu 18.04.6 LTS,直接使用`apt-get install supervisor` 即可,安装完成后配置
`sudo systemctl enable supervisor` `sudo systemctl start supervisor` `ps -ef |grep supervisor` #可查看到相关进程
其配置文件位于:/etc/supervisor/supervisord.conf
如何添加进程?在/etc/supervisort/conf.d/下编写.conf结尾的配置文件,如、
[program:laravel-queue-worker] process_name=%(program_name)s_%(process_num)02d command=php /home/wwwroot/crm-api/artisan queue:listen --sleep=3 --tries=3 autostart=true autorestart=true user=root numprocs=2 redirect_stderr=true stdout_logfile=/home/wwwroot/crm-api/storage/logs/queue-worker.log
编辑完成后:
supervisorctl update #即可生效新的进程,如下 root@blog-server:/etc/supervisor/conf.d# ps -ef|grep queue root 2322047 2215991 0 Mar29 ? 00:17:48 php /home/wwwroot/crm-api/artisan queue:listen --sleep=3 --tries=3 root 2322048 2215991 0 Mar29 ? 00:17:42 php /home/wwwroot/crm-api/artisan queue:listen --sleep=3 --tries=3 root 2910016 2322048 4 15:32 ? 00:00:00 /usr/local/php/bin/php artisan queue:work --once --name=default --queue=default --backoff=0 --memory=128 --sleep=3 --tries=3 root 2910023 2322047 0 15:32 ? 00:00:00 /usr/local/php/bin/php artisan queue:work --once --name=default --queue=default --backoff=0 --memory=128 --sleep=3 --tries=3
使用Job之前我们首先得先初始化job驱动,这里我直接使用的是database驱动。初始化命令为 php artisan queue:table, php artisan queue:fail-table,初始化数据库队列及失败队列2张表,当然更多命令可以使用 php artisan 查看,如下
queue queue:batches-table Create a migration for the batches database table queue:clear Delete all of the jobs from the specified queue queue:failed List all of the failed queue jobs queue:failed-table Create a migration for the failed queue jobs database table queue:flush Flush all of the failed queue jobs queue:forget Delete a failed queue job queue:listen Listen to a given queue queue:monitor Monitor the size of the specified queues queue:prune-batches Prune stale entries from the batches database queue:prune-failed Prune stale entries from the failed jobs table queue:restart Restart queue worker daemons after their current job queue:retry Retry a failed queue job queue:retry-batch Retry the failed jobs for a batch queue:table Create a migration for the queue jobs database table queue:work Start processing jobs on the queue as a daemon
以发送邮件为例,我们在app/Jobs下新建文件ProcessSendMsg.php类文件,当然也可以通过命令生成php artisan make:job ProcessSendMsg
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Collection; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class ProcessSendMsg implements ShouldQueue { use Dispatchable; use InteractsWithQueue; use Queueable; use SerializesModels; public $timeout = 600; /** * Create a new job instance. * * @return void */ public function __construct() { // TODO } /** * Execute the job. * * @return void * @throws Exception */ public function handle() { // TODO 调度入口方法函数 } }
至此我们的服务就启动了,至于如何调度,那更简单了,在需要发送邮件地方直接调用。
ProcessSendMsg ::dispatch($model); // $model可以传入任意业务逻辑相关的参数,在ProcessSendMsg构造函数中接受该参数。
开发调试中我们也可直接使用如下命令启动控制台队列,这样可如果有日志输出便可直观看出,唯一的不好的地方如果代码遇到异常,控制台进程会中断退出。
php artisan queue:listen --tries=3 --sleep=N