Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Pass previous job in a chain to the next job #2641

Open
tobz-nz opened this issue Jun 16, 2021 · 3 comments
Open

Pass previous job in a chain to the next job #2641

tobz-nz opened this issue Jun 16, 2021 · 3 comments

Comments

@tobz-nz
Copy link

tobz-nz commented Jun 16, 2021

Queued job chains are super useful for processing in stages. What I think is missing is a way of each job in the chain knowing something from the previous job.

In my current use case I have a pipeline of file processing:

  • file gets decoded (via external API - takes time)
  • decoded file gets processed (another API)
  • processed file gets encoded (coding API)

Passing each file down the stages of the chain would be really useful. Receiving the returned result form one job in the handle() method of the next maybe? Something like:

// controller
Bus::chain([
    new ProcessPodcast($file),
    new OptimizePodcast,
    new ReleasePodcast,
])->dispatch();

// ProcessPodcast.php
public function handle() 
{
    ...
    return $file;
}

// OptimizePodcast.php
public function handle($file) 
{
   // do something  with $file
}

Another option would be receiving the previous job instance itself? Maybe on $this->getPreviousJob() or something (this a terrible method name but you know what I mean)

@rognales
Copy link

How bout this package by Spatie https://github.com/spatie/laravel-queueable-action#chaining-actions

@tobz-nz
Copy link
Author

tobz-nz commented Jun 16, 2021

@rognales It might just be too early in the morning, but I don't understand how that package adds over normal queueable jobs.
I'd still need to dispatch each job in the chain from within the current job (i.e. not really using a chain as such, but dispatching jobs within jobs - not what I want).

In my use case the number and type of jobs in the chain change depending on the type of file (e.g some do no need decoding, they use different coding or processing API's etc..) so having each job independent is important.
Just passing the result of each job onto the next in the chain would be ideal.

@tobz-nz
Copy link
Author

tobz-nz commented Jun 16, 2021

Another option would be to have access to the entire chain from within each job, and be able to update them.
Something like:

function handle( )
{
    // do some work

   $nextJobInChain = $this->getChain()->next();
   $nextJobInChain->setFile($file)->save();
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants