⚡
Laravel Haystack
View Code On GithubEdit Docs
  • 🎯Getting Started
    • Introduction
    • How It Works
    • Installation
    • Usage
    • Configuration
    • Callback Events
  • 🛰️Cool Features
    • Shared Data
    • Shared Models
    • Long Delays & Pauses
    • Appending & Prepending Jobs
    • Chunking Jobs
  • 🗺️Next Up
    • Allowing Failed Jobs
    • Global Middleware
    • Connection, Queue & Delay
    • Naming Haystacks
    • Before Saving Hook
    • Custom Options
  • 🧼Cleanup
    • Deleting Stale Haystacks
    • Deleting Specific Haystacks
    • Clearing All Haystacks
  • 🛣️Finish
    • Support
    • Star On Github
    • Edit Documentation
Powered by GitBook
On this page
  1. Next Up

Global Middleware

PreviousAllowing Failed JobsNextConnection, Queue & Delay

Last updated 2 years ago

You can also provide middleware that will be applied to every job in the haystack. It will accept either an array, a closure that returns an array or an invokable class that returns an array.

This is useful if you don’t want to manually add the middleware to every job, or if the middleware cannot belong to the job on its own. One example of this being useful is if you want to apply an API rate limiter to your jobs that are making requests to a third party API.

To add middleware to every job, use the addMiddleware method when building the haystack.

If you are unfamiliar with job middleware, .

Array

$haystack = Haystack::build()
   ->onQueue('podcasts')
   ->addJob(new RecordPodcast) 
   ->addJob(new ProcessPodcast)
   ->addMiddleware([
       (new RateLimited)->allows(30)->everyMinute(),
       new OtherMiddleware,
   ])
   ->dispatch();

Closure

You must return an array in the closure for it to work.

$haystack = Haystack::build()
   ->onQueue('podcasts')
   ->addJob(new RecordPodcast) 
   ->addJob(new ProcessPodcast)
   ->addMiddleware(function () {
        return [
           (new RateLimited)->allows(30)->everyMinute(),
           new OtherMiddleware,
        ];
   })
   ->dispatch();

Invokable class

You must return an array inside your invokable class for it to work.

$haystack = Haystack::build()
   ->onQueue('podcasts')
   ->addJob(new RecordPodcast) 
   ->addJob(new ProcessPodcast)
   ->addMiddleware(new PodcastMiddleware)
   ->dispatch();

// Invokable class...

class PodcastMiddleware {
    public function __invoke()
    {
       return [
          (new RateLimited)->allows(30)->everyMinute(),
           new OtherMiddleware,
       ];
    }
}

Chainable Middleware

You can also chain multiple middlewares together.

$haystack = Haystack::build()
   ->onQueue('podcasts')
   ->addJob(new RecordPodcast) 
   ->addJob(new ProcessPodcast)
   ->addMiddleware([
       (new RateLimited)->allows(30)->everyMinute(),
       new OtherMiddleware,
   ])
   ->addMiddleware([
       new AnotherMiddleware,
   ])
   ->dispatch();
🗺️
click here