Laravel 5.2 队列和作业 - 不推送到作业数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37734656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:58:24  来源:igfitidea点击:

Laravel 5.2 Queue and Jobs - Not Pushing to Jobs DB

phpmysqllaravelcurllaravel-5.2

提问by Brian Logan

EDIT 2:

编辑2:

Here are the steps we are going through:

以下是我们正在经历的步骤:

  1. Schedule is Run (creating CollectHistoricalDataJobs for each company)
  2. CollectHistoricalDatashould be pushed to Queue (jobstable)
  3. CollectHistoricalDatahas a function ApiDaemon::GetCompanyWithQuery($company, $query)which is run from a separate class that is referenced a couple of other places too.
  4. GetCompanyWithQuerycollects the data and inserts it into the database.
  1. Schedule is Run(CollectHistoricalData为每个公司创建工作)
  2. CollectHistoricalData应该被推送到队列(jobs表)
  3. CollectHistoricalData有一个ApiDaemon::GetCompanyWithQuery($company, $query)从一个单独的类运行的函数,该类也被引用到其他几个地方。
  4. GetCompanyWithQuery收集数据并将其插入到数据库中。

It runs all the way through fine, but the hang up is rather than inserting the job into the jobs table, it just runs it synchronously, one after another.

它一直运行良好,但挂断不是将作业插入到作业表中,它只是一个接一个地同步运行。



EDIT 1:

编辑 1:

The .envfile is set to use the databaseQUEUE_DRIVER, I have even tried hard coding it in the config/queue.phpfile.

.env文件设置为使用databaseQUEUE_DRIVER,我什至尝试在config/queue.php文件中对其进行硬编码。



We are using Laravel 5.2 for a project. In this project, we are needing to every hour cURL a url and save the data to the database. We at first were using Cron Jobs and basically firing off thousands of cURLs in about a minute, which would crash PHP due to the load.

我们在一个项目中使用 Laravel 5.2。在这个项目中,我们需要每小时 cURL 一个 url 并将数据保存到数据库中。我们一开始使用 Cron Jobs 并且基本上在一分钟内触发了数千个 cURL,这会导致 PHP 由于负载而崩溃。

We decided to move over to Laravel's Jobs and Queues, without success. We are using the Database driver for our jobs, and have tried numerous different approaches to getting the jobs into the database, so the daemon workers we have can process them.

我们决定转向 Laravel 的 Jobs and Queues,但没有成功。我们正在为我们的作业使用数据库驱动程序,并尝试了多种不同的方法将作业放入数据库,因此我们拥有的守护进程可以处理它们。

Here is our code right now, we are using the Kernel.php $scheduleto start the thing off, so we don't have hundreds of requests attempting to happen an hour, which results in tens of thousands of cURLs.

这是我们现在的代码,我们使用 Kernel.php$schedule来启动这件事,所以我们不会有数百个请求试图在一个小时内发生,这会导致数以万计的 cURL。

Kernel.php Schedule:

Kernel.php 时间表:

    $schedule
        ->call(function () {
            $items = DB::select('{selecting certain things to run}');
            foreach ($items as $q) {
                $this->dispatch(new CollectHistoricalData(Company::find($q->company_id), ApiQuery::find($q->query_id)));
            }
        })
        ->hourly()
        ->name('Historical Pulls')
        ->withoutOverlapping()
        ->before(function() {
            $this->startTime = Carbon::now();
        })
        ->after(function () {
            mail({mail us a report afterward});
        });

When this runs, it is sitting there running them all one by one, rather than pushing them to the Jobs table that was created.

当它运行时,它坐在那里一一运行它们,而不是将它们推送到创建的 Jobs 表。

CollectHistoricalData.php:

收集历史数据.php:

<?php

namespace App\Jobs;

use App\Helpers\Daemons\ApiDaemon;
use App\Jobs\Job;
use App\Models\Company;
use App\Models\ApiQuery;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    protected $company, $query;


    /**
     * CollectHistoricalData constructor.
     * @param Company $company
     * @param ApiQuery $query
     */
    public function __construct(Company $company, ApiQuery $query)
    {
        $this->company = $company;
        $this->query = $query;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        mail({let us know what started and when});
        QueryDaemon::GetCompanyWithQuery($this->company, $this->query);
    }

    public function failed()
    {
        mail({mail us letting us know it failed});
    }


}

The job is referencing another class with the function inside it (since that code is a heafty beast all on its own), plus there are about 20 of these, so it is easiest to reference the class rather than recreating all 20 classes into Jobs.

该作业正在引用另一个包含该函数的类(因为该代码本身就是一个巨大的野兽),此外还有大约 20 个这样的类,因此最容易引用该类而不是将所有 20 个类重新创建到 Jobs 中。

TL;DR

TL; 博士

We have a schedule that is supposed to push a job that references a function in another class, to the jobs table, but is rather running them one after another, slowly. What is causing this?

我们有一个计划,它应该将引用另一个类中的函数的作业推送到作业表,而是一个接一个地缓慢地运行它们。这是什么原因造成的?

回答by Brian Logan

Well... I am dumb....

嗯……我傻了……

php artisan config:clear

php artisan config:clear

I didn't clear the cache of the config.... wow...

我没有清除配置的缓存....哇...

回答by Cristian Deluxe

As

作为

php artisan config:clear

wasn't working for me i was able to make it work using:

对我不起作用,我能够使用以下方法使其工作:

php artisan config:cache

回答by Aqua Huang

In Laravel 6.x , edit this in /env : QUEUE_CONNECTION=sync => QUEUE_CONNECTION=database then handle data can insert jobs table

在 Laravel 6.x 中,在 /env 中编辑: QUEUE_CONNECTION=sync => QUEUE_CONNECTION=database 然后处理数据可以插入作业表