Database Job Chains for Laravel


September 8th, 2022

Database Job Chains for Laravel

Laravel Haystack is a package for beautifully simple but powerful database-driven job chains. Here's an overview of what is available, taken from the package's readme:

$haystack = Haystack::build()
->withName('Podcast Chain')
->addJob(new RecordPodcast)
->addJob(new ProcessPodcast)
->addJob(new PublishPodcast)
->then(function () {
// Haystack completed
->catch(function () {
// Haystack failed
->finally(function () {
// Always run either on success or fail.
->paused(function () {
// Run if the haystack is paused
// Middleware to apply on every job
->withDelay(60) // Add a delay to every job

With this package, you store job chains in the database, which helps make memory consumption low and supports all of Laravel's queue types out of the box. Some of the other main features include:

  • Low memory consumption as one job is processed at a time and the chain is stored in the database
  • You can delay/release jobs for as long as you want since it will use the scheduler to restart a chain. Even if your queue driver is SQS!
  • It provides callback methods like then, catch and finally
  • Global middleware that can be applied to every single job in the chain
  • Delay that can be added to every job in the chain
  • You can store and retrieve data/state that is accessible to every job in the chain.
  • You can store the model for later processing.

You can do pretty cool stuff, such as pausing haystacks if your job chain calls an API and hits rate limits. While Laravel has job chains, I think you should consider this package for batched jobs. Check out the readme for the full details of what differentiates this package from the built-in chain abilities.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.