简体   繁体   English

从队列中存储已处理的作业 laravel

[英]Storing processed jobs from queue in laravel

I was working on queue trying to find a way to record processed jobs in a table 'processed_jobs' just like failed jobs are stored in table 'failed_jobs', which happens automatically.I am using 'database' for queue connection.我正在处理队列,试图找到一种方法将已处理的作业记录在表“processed_jobs”中,就像失败的作业存储在表“failed_jobs”中一样,这是自动发生的。我正在使用“数据库”进行队列连接。 I tried two different method.我尝试了两种不同的方法。 First I tried extending WorkCommand ('Illuminate\Queue\Console\WorkCommand') in a different command file 'WorkerCommand'首先,我尝试在不同的命令文件 'WorkerCommand' 中扩展 WorkCommand ('Illuminate\Queue\Console\WorkCommand')

<?php

namespace App\Console\Commands;

use Illuminate\Queue\Console\WorkCommand;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Queue\Worker;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;

class WorkerCommand extends WorkCommand
{
   protected function listenForEvents()
    {
        $this->laravel['events']->listen(JobProcessing::class, function ($event) {
            $this->writeOutput($event->job, 'starting');
        });

        $this->laravel['events']->listen(JobProcessed::class, function ($event) {
            $this->writeOutput($event->job, 'success');

            \DB::table('processed_jobs')->insert([
               'connection' => $event->connectionName,
               'queue' => $event->job->getQueue(),
               'payload' => $event->job->payload(),
               'processed_at' => \Carbon\Carbon::Now()
            ]);
        });

        $this->laravel['events']->listen(JobFailed::class, function ($event) {
            $this->writeOutput($event->job, 'failed');

            $this->logFailedJob($event);
        });
    }
}

It didn't work.它没有用。 And then I tried using job events.然后我尝试使用工作事件。 As suggested by this doc, in the boot function of my AppServiceProvider I tried:正如文档所建议的,在我的 AppServiceProvider 的启动 function 中,我尝试了:

    public function boot()
    {
        Queue::after(function (JobProcessed $event) {
            \DB::table('processed_jobs')->insert([
               'connection' => $event->connectionName,
               'queue' => $event->job->getQueue(),
               'payload' => $event->job->payload(),
               'processed_at' => \Carbon\Carbon::Now()
            ]);
        });
    }

which also didn't work.这也没有用。 I couldn't find anything else.我找不到其他任何东西。 Thanks in advance.提前致谢。 Here is the job file:这是作业文件:

<?php

namespace App\Jobs;

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

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

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        sleep(10);
    }
}

Function which adds the job to queue: Function 将作业添加到队列中:

    public function dispatchSleep(){
        ProcessSleep::dispatch();

        return response()->json(['message'=>'Process added to queue']);
    }

It is because 'payload' column in the 'processed_jobs' table is longtext, but $event->job->payload was returning object.这是因为 'processed_jobs' 表中的 'payload' 列是长文本,但$event->job->payload返回 object。 So, I fixed it using json_encode.所以,我使用 json_encode 修复了它。

public function boot()
{
    Queue::after(function (JobProcessed $event) {
        \DB::table('processed_jobs')->insert([
           'connection' => $event->connectionName,
           'queue' => $event->job->getQueue(),
           'payload' => json_encode($event->job->payload()),
           'processed_at' => \Carbon\Carbon::Now()
        ]);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM