laravel 任务队列_Laravel异步队列全攻略

最近项目需求,研究了laravel的异步队列。官方文档虽然很是详细,但也有些晦涩难懂,在此记录下步骤,供大家参考。

1、修改/config/queue.php文件

| Default Queue Connection Name

|--------------------------------------------------------------------------

|

| Laravel's queue API supports an assortment of back-ends via a single

| API, giving you convenient access to each back-end using the same

| syntax for every one. Here you may define a default connection.

|*/

'default' => env('QUEUE_CONNECTION', 'sync'),

/*|--------------------------------------------------------------------------

| 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.

|

| Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null"

|*/

'connections' =>['sync' =>['driver' => 'sync',],

'database' =>['driver' => 'database',

'table' => 'jobs',

'queue' => 'default',

'retry_after' => 90,],

'beanstalkd' =>['driver' => 'beanstalkd',

'host' => 'localhost',

'queue' => 'default',

'retry_after' => 90,

'block_for' => 0,],

'sqs' =>['driver' => 'sqs',

'key' => env('AWS_ACCESS_KEY_ID'),

'secret' => env('AWS_SECRET_ACCESS_KEY'),

'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),

'queue' => env('SQS_QUEUE', 'your-queue-name'),

'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),],

'redis' =>['driver' => 'redis',

'connection' => 'default',

'queue' => env('REDIS_QUEUE', 'default'),

'retry_after' => 90,

'block_for' => null,],],

/*|--------------------------------------------------------------------------

| Failed Queue Jobs

|--------------------------------------------------------------------------

|

| These options configure the behavior of failed queue job logging so you

| can control which database and table are used to store the jobs that

| have failed. You may change them to any database / table you wish.

|*/

'failed' =>['database' => env('DB_CONNECTION', 'mysql'),

'table' => 'failed_jobs',],];

View Code

注意:修改.env文件如下参数,设置队列连接默认为数据库连接

QUEUE_CONNECTION=database

2、新建/app/Job/EmailJob.php,此文件为队列主文件

namespace App\Job;useIlluminate\Bus\Queueable;useIlluminate\Contracts\Queue\ShouldQueue;useIlluminate\Foundation\Bus\Dispatchable;useIlluminate\Queue\InteractsWithQueue;useIlluminate\Queue\SerializesModels;use Illuminate\Support\Facades\Log;useApp\Service\EmailService;class EmailJob implementsShouldQueue

{use Dispatchable, InteractsWithQueue, Queueable,SerializesModels;private $content,$to;public function __construct($content,$to){$this->content=$content;$this->to=$to;

}public functionhandle(){$res=false;$times=0;while($res!==true && $times<3){try{$times++;$res=EmailService::send($this->content,$this->to);

}catch (\Exception $e){Log::error(date('Y-m-d h:i:s',time()).' send email error:'.$e->getMessage());

}

}if($res===true){Log::info(date('Y-m-d h:i:s',time()).' send email success:');

}

}

}

View Code

3、新建/app/Service/EmailJobService.php服务,此文件为封装服务文件,可以不用,直接在使用的地方调用队列。

namespace App\Service;useApp\Job\EmailJob;classEmailJobService

{public static function add($content,$to){$job=new EmailJob($content,$to);

dispatch($job);

}

}

View Code

4、打开终端切换目录进入Laravel项目根目录,执行如下命令,创建队列任务需要的数据表。

php artisan queue:table

php artisan queue:failed-table

php artisan migrate

5、通过下面这条指令启动队列监听服务,它会自动处理 jobs 表中的队列任务。

php artisan queue:listen

监听指定队列:

php artisan queue:work --queue=default,mytask --tries=2

这是监听 default和mytask两个队列,区分先后顺序。

6、如果需要在linux中后台运行,有两种方法:

6.1 执行如下命令:

nohup php artisan queue:listen > /tmp/artisan.log 2>&1 &

6.2.1 安装Supervisor,我的服务器系统为CentOs7.5,所以使用yum安装。

yum install supervisor

6.2.2 在/etc/supervisord.d下新建ini文件,eg:laraver-worker.ini,设置自动运行命令等相关参数

[program:laravel-worker]

process_name=%(program_name)s_%(process_num)02d

command=php 这里需要写项目目录/artisan queue:work --sleep=3 --tries=3autostart=trueautorestart=trueuser=root

numprocs=8stdout_logfile=/root/queue/daily_english_queue.log

View Code

6.2.3 启动supervisor,laravel队列监听进程便在后台运行了。

supervisord -c /etc/supervisord.conf

6.2.4 配置supervisor开机启动(否则服务器重启后必须手动启动supervisor)

cd /usr/lib/systemd/system/ //切换目录

touch supervisord.service //新建文件

vim supervisord.service //编辑文件

文件内容:

[Unit]

Description=Supervisor daemon

[Service]

Type=forking

ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

ExecStop=/usr/bin/supervisorctl shutdown

ExecReload=/usr/bin/supervisorctl reload

KillMode=process

Restart=on-failure

RestartSec=42s

[Install]

WantedBy=multi-user.target

设置开机启动

systemctl enable supervisord

验证是否设置成功

systemctl is-enabled supervisord

7、注意:如果修改了job内的代码(包括job调用的方法类),需要重启queue。

php artisan queue:restart

Enjoy it !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值