How to run a job after first job finished?

Viewed 448

I have two jobs inside a job. Now i need to start executing the second job(inside a job) after first one completed. and in my scenario job chaining is not possible. please help me find out the solution?

i am splitting a large file using SplitFile::dispatch($this->user_id, $counter);

and then iterating over those files and sending to job ProcessCustomerSync::dispatch($file_path, $user_id, $slices, $dispatch_counter, $last_sync_date, $totalSubscribers, $file); one by one.

The problem is second job is not waiting first one to complete.So the job is not getting all the data needed. i need to execute second one after first one is complete

 for($counter=1; $counter<=$slices; $counter++){
      // Splitting file to many for better performance
      SplitFile::dispatch($this->user_id, $counter);
 }

$files = Storage::disk('spaces')->allFiles('storage/klick_sync/'.$user_id);

// $files = glob($path);
Log::channel('command')->info('files'.$files);
$dispatch_counter = 1;

foreach($files as $file){
    $file_path = Storage::disk('spaces')->url($file);
    Log::channel('command')->info('path'.$file_path);
    ProcessCustomerSync::dispatch($file_path, $user_id, $slices, $dispatch_counter, $last_sync_date, $totalSubscribers, $file);
           $dispatch_counter++;
}
2 Answers

You can install supervisor and prepare a script to execute two independants jobs. You can organize it by priorities.

You can use Job Events to trigger a new job when your first one is completed:

        Queue::after(function (JobProcessed $event) {
            // $event->connectionName
            // $event->job
            // $event->job->payload()
        });
Related