Laravel 队列

编辑于 2023-02-27 00:49 阅读 658

创建任务

生成任务类

root@php-fpm:/var/www/laravel-demo# php artisan make:job PublishArticles
Job created successfully.

编辑一下

class PublishArticles implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public Article $article;
    public function __construct(Article $article)
    {
        $this->article=$article;
    }
    public function handle()
    {
        Log::info($this->article);
        //todo 发布文章
    }
}

调度任务,在web应用的控制器中,或控制台命令中都可以调用

$article=Article::query()->first();
\App\Jobs\PublishArticles::dispatch($article);

运行队列工作者

#效率高,代码更新时必须重启队列
php artisan queue:work

#效率低,代码更新时不必重启队列
php artisan queue:listen

php artisan queue:work redis

php artisan queue:work redis --queue=emails

php artisan queue:work --once

php artisan queue:work --max-jobs=1000

php artisan queue:work --stop-when-empty

# 处理进程一小时,然后退出...
php artisan queue:work --max-time=3600

php artisan queue:work --sleep=3

由于队列任务是长期存在的进程,因此如果不重新启动,他们不会注意到代码的更改。因此,使用队列任务部署应用程序的最简单方法是在部署过程中重新启动任务。您可以通过发出 queue:restart 命令优雅地重新启动所有进程:

php artisan queue:restart

队列驱动

null

丢弃排队任务

QUEUE_CONNECTION=null

sync

立即执行任务的同步驱动程序(用于本地开发期间)

QUEUE_CONNECTION=sync

redis

composer require predis/predis
QUEUE_CONNECTION=redis

database

php artisan queue:table

php artisan migrate
QUEUE_CONNECTION=database

beanstalkd

composer require pda/pheanstalk
QUEUE_CONNECTION=beanstalkd

处理失败的工作

创建 failed_jobs 表的迁移通常已经存在于新的 Laravel 应用程序中。但是,如果您的应用程序不包含此表的迁移,您可以使用 queue:failed-table 命令来创建迁移:

php artisan queue:failed-table

php artisan migrate

失败重试

#重试3次
#如果您没有为 --tries 选项指定值,则作业将仅尝试一次或与任务类的 $tries 属性指定的次数相同:
php artisan queue:work redis --tries=3

#重试3次,每次都3秒后重试
php artisan queue:work redis --tries=3 --backoff=3

#重试任务前等待的秒数。
public $backoff = 3;

#计算重试任务之前要等待的秒数。
public function backoff()
{
    return 3;
}

#计算重试任务之前要等待的秒数。
#第一次重试的重试延迟为 1 秒,第二次重试为 5 秒,第三次重试为 10 秒:
public function backoff()
{
    return [1, 5, 10];
}

任务失败后发送告警

class ProcessPodcast implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $podcast;
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }
    public function handle(AudioProcessor $processor)
    {
        // 处理上传的播客...
    }
    public function failed(Throwable $exception)
    {
        // 向用户发送失败通知等...
    }
}

手动重试

#查看失败的任务(failed_jobs表)
php artisan queue:failed

#任务 ID 可用于重试失败的任务
php artisan queue:retry ce7bb17c-cdd8-41f0-a8ec-7b4fef4e5ece

#如有必要,可以向命令传递多个 ID:
php artisan queue:retry ce7bb17c-cdd8-41f0-a8ec-7b4fef4e5ece 91401d2c-0784-4f43-824c-34f94a33c24d

#还可以重试指定队列的所有失败任务:
php artisan queue:retry --queue=name

#重试所有失败任务,可以执行 queue:retry 命令,并将 all 作为 ID 传递:
php artisan queue:retry all

#如果要删除指定的失败任务,可以使用 queue:forget 命令:
php artisan queue:forget 91401d2c-0784-4f43-824c-34f94a33c24d

#删除 failed_jobs 表中所有失败任务,可以使用 queue:flush 命令:
php artisan queue:flush

#删除失败的任务2(和queue:flush有何不同?)
php artisan queue:prune-failed
php artisan queue:prune-failed --hours=48

忽略缺失的模型

/**
 * 如果任务的模型不存在,则删除该任务。
 *
 * @var bool
 */
public $deleteWhenMissingModels = true;

丢弃失败的任务而不存储它们

QUEUE_FAILED_DRIVER=null

从队列中清除任务

php artisan queue:clear

php artisan queue:clear redis --queue=emails

注意:从队列中清除任务仅适用于 SQS、Redis 和数据库队列驱动程序。 此外,SQS 消息删除过程最多需要 60 秒,因此在你清除队列后 60 秒内发送到 SQS 队列的任务也可能会被删除。

监控你的队列 [新特性]

php artisan queue:monitor redis:default,redis:deployments --max=100

参考

https://learnku.com/docs/laravel/9.x/queues/12236

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验