排队的 Laravel 作业同时触发,并且不会显示在作业表中

2022-08-30 14:59:15

我正在使用Laravel队列来评论Facebook帖子。每当我从Facebook网络hook收到数据时,根据收到的详细信息,我会对帖子发表评论。为了同时处理来自Facebook webhooks的100个响应,我正在使用Laravel队列,以便它可以逐个执行。我使用了为什么Laravel队列很棒中提到的分步过程

public function webhooks(Request $request)
{
    $data = file_get_contents('php://input');
    Log::info("Request Cycle with Queues Begins");
    $job = (new webhookQueue($data)->delay(10);
    $this->dispatch($job);
    Log::info("Request Cycle with Queues Ends");
}

这是我的工作类结构

class webhookQueue extends Job implements ShouldQueue
{    
    use InteractsWithQueue, SerializesModels;

    private $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function handle()
    {
       //handling the data here 
    }
}

我连续命中该函数,并且所有作业同时工作,但不在队列中。作业表中未存储任何作业。我已经给了一个延迟,但它也不起作用。webhooks()

这是我的拉拉维尔.log:

[2017-02-08 14:18:42] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:44] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:47] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:48] local.INFO: Request Cycle with Queues Begins  
[2017-02-08 14:18:55] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:18:55] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:18:55] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:18:59] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:00] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:00] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:00] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends  
[2017-02-08 14:19:01] local.INFO: Request Cycle with Queues Ends

答案 1

对于使用队列,你应该做一些工作:

在.env文件中,您应该将queue_driver从同步更改为数据库,因此请打开.env并执行以下操作

queue_driver=database

在它之后,您应该使用artisan命令在数据库中创建队列表:

php artisan queue:table
php artisan migrate

并确保没有配置缓存

php artisan config:clear

最后,您应该使用 or 运行队列php artisan queue:listenphp artisan queue:work


答案 2

我遇到了同样的麻烦,如果您使用的是Laravel 5.7或更高版本,请在.env文件中使用它

QUEUE_CONNECTION=database

接下来,像这样清除配置缓存

php artisan config:clear

推荐