Thursday, December 5, 2024
HomePHPDatabase Job Chains for Laravel

Database Job Chains for Laravel


Laravel Haystack is a bundle for fantastically easy however highly effective database-driven job chains. Here is an summary of what’s accessible, taken from the bundle’s readme:

1$haystack = Haystack::construct()

2 ->withName('Podcast Chain')

3 ->addJob(new RecordPodcast)

4 ->addJob(new ProcessPodcast)

5 ->addJob(new PublishPodcast)

6 ->then(perform () {

7 // Haystack accomplished

8 })

9 ->catch(perform () {

10 // Haystack failed

11 })

12 ->lastly(perform () {

13 // All the time run both on success or fail.

14 })

15 ->paused(perform () {

16 // Run if the haystack is paused

17 })

18 ->withMiddleware([

19 // Middleware to apply on every job

20 ])

21 ->withDelay(60) // Add a delay to each job

22 ->dispatch();

With this bundle, you retailer job chains within the database, which helps make reminiscence consumption low and helps all of Laravel’s queue varieties out of the field. A few of the different primary options embody:

  • Low reminiscence consumption as one job is processed at a time and the chain is saved within the database
  • You’ll be able to delay/launch jobs for so long as you need since it can use the scheduler to restart a series. Even when your queue driver is SQS!
  • It offers callback strategies like then, catch and lastly
  • World middleware that may be utilized to each single job within the chain
  • Delay that may be added to each job within the chain
  • You’ll be able to retailer and retrieve knowledge/state that’s accessible to each job within the chain.
  • You’ll be able to retailer the mannequin for later processing.

You are able to do fairly cool stuff, corresponding to pausing haystacks in case your job chain calls an API and hits fee limits. Whereas Laravel has job chains, I feel it’s best to think about this bundle for batched jobs. Take a look at the readme for the complete particulars of what differentiates this bundle from the built-in chain talents.

You’ll be able to study extra about this bundle, get full set up directions, and examine the supply code on GitHub.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments