Home > OS >  Laravel run something when create a job
Laravel run something when create a job

Time:03-17

when I create a job and before start it , I need run a function , i update the class DatabaseJob

<?php
namespace App\Queue\Jobs;

use App\Models\Tenancy;
use App\Http\DatabaseHelper;
use App\Http\Helper;

class DatabaseJob extends \Illuminate\Queue\Jobs\DatabaseJob
{
    public function fire()
    {

        
        Helper::createJobLog($this->job->id);

        parent::fire();
    }
}

but it seems the function createJobLog is fired only when the Job start ,I need it when the job created not started .

CodePudding user response:

You may call the function createJobLog() when the job is dispatched. Jobs can be set with a timestamp to delay its start time, if you don’t want the job started immediately after it is being dispatched.

CodePudding user response:

In a service provider you can listen for the Illuminate\Queue\Events\JobQueued event. Something similar to:

Event::listen(JobQueued::class, function ($event) {
    // Of course, if you need to log only the database jobs then you can check the job type
    if (!$event->job instanceOf DatabaseJob) {
        return;
    }

    Helper::createJobLog($event->job->getJobId());
});
  • Related