Dakait
Dakait

Reputation: 2620

Keep track of laravel queued jobs

I'm trying to get department details from an API which supports pagination, so if I spawn one job per page like following

/departments?id=1&page=1 -> job1
/departments?id=1page=2 -> job2

How can I keep track of these jobs for a particular department as I have to write the responses to txt file.

The jobs are instantiated via controller class like:

class ParseAllDeptsJob implements ShouldQueue
{
  public function handle()
  {
    foreach (Departments::all() as $dept) {
      ParseDeptJob::dispatch($dept);
    }
  }
}

Upvotes: 1

Views: 557

Answers (1)

Mozammil
Mozammil

Reputation: 8750

You can chain a job, using withChain(). This job will not run if the jobs higher up the chain fail.

From the documentation:

Job chaining allows you to specify a list of queued jobs that should be run in sequence. If one job in the sequence fails, the rest of the jobs will not be run. To execute a queued job chain, you may use the withChain method on any of your dispatchable jobs:

In your case, this is how you'd do it:

ParseAllDeptsJob::withChain([
    new SendEmailNotification
])->dispatch();

SendEmailNotification won't be dispatched if an error occurs while processing ParseAllDeptsJob.

Upvotes: 3

Related Questions