proletarian 2026-01-10

Hi, beginner question here... I'm looking at the https://github.com/msolli/proletarian/tree/main?tab=readme-ov-file#job-handler section of the readme:

It's also useful to have system state available in this function. It should close over references to stateful objects and functions that you need for the job to do its work. Examples of this could be things like database and other (Elasticsearch, Redis) connections, and runtime configuration.
Would you have a simple example of how to achieve the above using a multimethod? Specifically how to make that stateful object available for the specific implementations? Thank you in advance.

I would have a handler function that closes over the stateful things, and call the multimethod from there, passing the stateful objects as required.

Does that make sense? On my phone right now, can't really come up with code to illustrate.

Makes sense! Function calling a multimethod is what I missed. Will try this out soon. Many thanks!

So I tried this out and ended up with the following (which works):

(defmulti handle-job!
 (fn [job-type _payload _system] job-type))

(defmethod handle-job! ::store-race
  [job-type {:keys [race-id]} system]
  (let [positions (d/get-race-positions (:db system) race-id)
        in (serialize-positions positions)]
    (store-in-file in "positions.json.gz")))

(defn job-handler [db]
  (fn [job-type payload]
    (handle-job! job-type payload {:db db})))

(defn new-worker [db]
  (worker/create-queue-worker db (job-handler db)))
with the above I need to pass the system map to all implementations, and retrieve whichever component I need. Did I get this right? I feel like the above stands in contradiction to passing the stateful objects as required statement in your response, as I have to pass the system map, even if I don't need it in a specific implementation. Am I missing something?

Yeah, you're right, that statement didn't really make sense. 😅 I think what you have done here is fine. In impls where you don't need the system map you just ignore it.

1