### Create and Start a Queue Worker Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Defines a queue worker in one namespace and starts it. The worker constructor requires a data-source and a job handler function. Use a component state library for managing worker state in production. ```clojure (ns your-app.workers "You'll probably want to use a component state library (Component, Integrant, Mount, or some such) for managing the worker state. For this example we're just def-ing the worker. The queue worker constructor function takes a javax.sql.DataSource as its first argument. You probably already have a data-source at hand in your application already. Here we'll use next.jdbc to get one from a JDBC connection URL. The second argument is the job handler function. Proletarian will invoke this whenever a job is ready for processing. It's a arity-2 function, with the job type (a keyword) as the first argument, and the job's payload as the second argument." (:require [next.jdbc :as jdbc] [proletarian.worker :as worker] [your-app.handlers :as handlers])) (def email-worker (let [ds (jdbc/get-datasource "jdbc:postgresql://...")] (worker/create-queue-worker ds handlers/handle-job!))) (worker/start! email-worker) ``` -------------------------------- ### Retry Strategy Example: 2 Retries Source: https://cljdoc.org/d/msolli/proletarian/CURRENT This retry strategy configuration specifies that a job should be retried two times. The first retry will occur after 1 second, and the second retry after 5 seconds. ```clojure {:retries 2 :delays [1000 5000]} ``` -------------------------------- ### Retry Strategy Example: 4 Retries Source: https://cljdoc.org/d/msolli/proletarian/CURRENT This retry strategy configuration specifies that a job should be retried four times. The delays between retries are 2 seconds for the first retry and 10 seconds for all subsequent retries. ```clojure {:retries 4 :delays [2000 10000]} ``` -------------------------------- ### Enqueue Job and Define Handler - Clojure Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Example of enqueuing a job with a specific type and payload, and a corresponding job handler function. The handler dispatches based on job type. Ensure necessary imports are present. ```clojure (require '[proletarian.job :as job]) (defn do-something! [db-conn foo] ;; Do stuff here ;; Enqueue a job: (job/enqueue! db-conn ::job-type-foo foo) ) ``` ```clojure ;; Pass this function as the second argument to ;; proletarian.worker/create-queue-worker (defn handle-job! [job-type payload] ;; Do the dispatch of job types here. This could maybe invoke a multimethod ;; that dispatches on `job-type`. See Example C. ;; The value of payload is whatever was passed as third argument to ;; job/enqueue! (the value of foo in do-something! in this case) ) ``` -------------------------------- ### Handle Web Requests and Enqueue Jobs Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Handles web requests, performs database operations within a transaction, and enqueues a job using `job/enqueue!`. This code resides in a namespace that handles web requests. ```clojure (ns your-app.handlers "Let's say this is a namespace where you handle web requests. We're going to handle the request, write something to the database, and enqueue a job. We'll do this in a transaction with a little bit of help from next.jdbc." (:require [next.jdbc :as jdbc] [proletarian.job :as job])) (defn some-route-handler [system request] (jdbc/with-transaction [tx (:db system)] ;; Do some business logic here ;; Write some result to the database ;; Enqueue the job: (job/enqueue! tx ::confirmation-email {:email email-address, :other-data-1 :foo, :other-data-2 :bar}) ;; Return a response response)) ``` -------------------------------- ### Add Proletarian to project.clj Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Include this dependency in your `project.clj` file for projects managed with Leiningen. ```clojure [msolli/proletarian "1.0.115"] ``` -------------------------------- ### Define Job Handler and Retry Strategy Function Source: https://cljdoc.org/d/msolli/proletarian/CURRENT This snippet shows how to define a job handler function and a custom retry strategy function. The retry strategy determines how many times a job will be retried and the delays between retries. ```clojure (require '[proletarian.job :as job]) (defn handle-job! [job-type payload] ;; Do stuff that might throw an exception here ) (defn my-retry-strategy [job throwable] {:retries 4 :delays [1000 5000]} ;; This retry strategy specifies that the job should be retried up to four ;; times, for a total of five attempts. The first retry should happen ;; one second after the first attempt failed. The remaining attempts should ;; happen five seconds after the previous attempt failed. ;; After four retries, if the job was still failing, it is not retried ;; anymore. It is moved to the archived-job-table with a failure status. ) ``` -------------------------------- ### Implement Custom Logging for Queue Worker Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Configures a custom logger function for the queue worker using `clojure.tools.logging`. The logger function maps event keywords to logging levels and logs event data. ```clojure (ns your-app.workers (:require [clojure.tools.logging :as log] [next.jdbc :as jdbc] [proletarian.worker :as worker] [your-app.handlers :as handlers])) (defn log-level [x] (case x ::worker/queue-worker-shutdown-error :error ::worker/handle-job-exception-with-interrupt :error ::worker/handle-job-exception :error ::worker/job-worker-error :error ::worker/polling-for-jobs :debug :proletarian.retry/not-retrying :error :info)) (defn logger [x data] (log/logp (log-level x) x data)) (def worker (let [ds (jdbc/get-datasource "jdbc:postgresql://...")] (worker/create-queue-worker ds handlers/handle-job! {:proletarian/log logger}))) ``` -------------------------------- ### Define Job Handler Multimethod Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Defines a multimethod `handle-job!` which is called by the Queue Worker for job execution. Implement this multimethod for specific job types. ```clojure (defmulti handle-job! "Since we passed this multimethod as the second argument to worker/create-queue-worker, it is called by the Proletarian Queue Worker when a job is ready for execution. Implement this multimethod for your job types." (fn [job-type _payload] job-type)) ;; Implement the handle-job! multimethod for the job type. (defmethod handle-job! ::confirmation-email [job-type {:keys [email-address other-data-1 other-data-2]}] ;; Send the mail and do other time-consuming work here. ) ``` -------------------------------- ### Add Proletarian to deps.edn Source: https://cljdoc.org/d/msolli/proletarian/CURRENT Include this dependency in your `deps.edn` file for Clojure projects managed with `clj` or `deps.edn`. ```clojure msolli/proletarian {:mvn/version "1.0.115"} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.