Skip to content

Middlewares

akshat edited this page Jul 29, 2022 · 5 revisions

Goose has provision for middlewares which can run before/after execution of a Job.

How Middlewares work?

  • middleware-fn takes next as input
  • It returns a function of arity 2
  • Returned function takes opts & job as inputs
  • Function calls next with opts & job

Practical applications of Middlewares

  • Modify args of job pre-execution
  • Use returned value of job post-execution
  • Ignore exceptions thrown by job

Usage

(defn my-middleware
  [next]
  (fn [opts job]
    ; Be careful when modifying fields of opts/job.
    (let [result (next opts job)]
      (log/info result))))

Nuances

  • Middleware's time will be added to job execution time
  • For client-side middlewares, you can wrap enqueuing function around your middleware
Clone this wiki locally