roche
roche

Reputation: 155

Laravel run something when create a job

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 .

Upvotes: 0

Views: 694

Answers (2)

Mihai Matei
Mihai Matei

Reputation: 24276

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());
});

Upvotes: 1

Elvis
Elvis

Reputation: 73

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.

Upvotes: 0

Related Questions