简体   繁体   中英

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. I tried two different method. First I tried extending WorkCommand ('Illuminate\Queue\Console\WorkCommand') in a different command file 'WorkerCommand'

<?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:

    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:

    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. So, I fixed it using 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()
        ]);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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