我可以在拉拉维尔(Laravel)派遣延迟工作

发布于 2025-01-23 21:54:57 字数 1068 浏览 2 评论 0原文

下面的代码不起作用。我认为我已经正确完成了所有事情,但是不知何故我不起作用。

这样的工作是这样派遣的:

MyJob::dispatch($job)->onQueue('processing')->delay(Carbon::now()->addSeconds(30));

myjob.php

<?php

namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    public function __construct($job)
    {
        // I described a logging code here and yes, there was change, but then...
    }

    public function handle()
    {
        // I described a logging code here, but there wasn't change
    }
}

问题是dispatchnow()确实有效,但是与延迟进行调度无效。

我还正确设置了.env(我猜)

.env文件


    QUEUE_CONNECTION=database

config/queue.php


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

The code below doesn't work. I think I have done all things correctly, but somehow I doesn't work.

The job is dispatched like this:

MyJob::dispatch($job)->onQueue('processing')->delay(Carbon::now()->addSeconds(30));

MyJob.php

<?php

namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    public function __construct($job)
    {
        // I described a logging code here and yes, there was change, but then...
    }

    public function handle()
    {
        // I described a logging code here, but there wasn't change
    }
}

The problem is that dispatchNow() did work, but dispatch with delay didn't work.

I also set .env correctly(I guess)

.env file


    QUEUE_CONNECTION=database

config/queue.php


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2025-01-30 21:54:57

诊断步骤;

  1. 打开修补程序并运行config('queue')并检查队列设置是否如预期

  2. 而无需运行队列工作,请将您的工作派遣到队列。打开您的数据库工具,并检查作业表是否包含一个新记录。

  3. 使用php工匠队列运行队列工人:工作延迟后,作业应运行。

  4. 检查作业是否从工作表中转到

Diagnostic steps;

  1. Open tinker and run config('queue') and check that the queue settings are as expected

  2. Without running a queue worker, dispatch your job to the queue. Open your database tools and check that the jobs table contains one new record.

  3. run the queue worker with php artisan queue:work after the delay, the job should run.

  4. Check that the job has gone from the jobs table, and that nothing is in the failed_jobs table.

爱你是孤单的心事 2025-01-30 21:54:57

您可以按照上述建议尝试清除缓存。此外,还要尝试重新启动队列

php artisan queue:restart

:工作问题。是的,如果您希望队列永久性主管运行是一个不错的选择。示例配置如下。

[program:my-laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/project/artisan queue:work --queue=QUEUE_NAME--sleep=1 --tries=1
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/log/file.log

You could try clearing your cache as advised above. Additionally, also try to restart the queue

php artisan queue:restart

As for the queue:work question. Yes if you want the queue to run permanently supervisor is a good option to use. A sample config is as below.

[program:my-laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/project/artisan queue:work --queue=QUEUE_NAME--sleep=1 --tries=1
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/log/file.log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文