Job execution history: DB persistence and in-memory ring buffer of recent executions.
A HistoryStore wraps a database pool and an atom-backed ring buffer. Create one store per system and share it across components that record or query job history.
Typical usage:
;; Create the store (pool from the database component) (def store (job-history/create-store pool {:buffer-size 500}))
;; In the worker execution path (job-history/record-start! store job worker-id correlation-id) ;; ... job runs ... (job-history/record-completion! store (:id job) worker-id correlation-id elapsed-ms)
;; Query DB history (job-history/get-by-job-id store job-id) (job-history/query store {:from (java.util.Date.) :status "failed" :limit 50})
;; Inspect recent executions from the ring buffer (no DB) (job-history/recent store 20)
;; Wire up to monitoring events so the ring buffer is populated without ;; routing writes through this component: (monitoring/on emitter :job/completed (fn [{:keys [data]}] (job-history/observe! store data)))
;; Scheduled maintenance (job-history/expire! store)
Ring buffer notes: record-start! writes to the DB only - started records are not buffered. record-completion!, record-failure!, and record-partial-success! write to the DB and push the terminal record into the ring buffer. observe! adds directly to the buffer without any DB interaction.
Job execution history: DB persistence and in-memory ring buffer of recent executions.
A HistoryStore wraps a database pool and an atom-backed ring buffer. Create one
store per system and share it across components that record or query job history.
Typical usage:
;; Create the store (pool from the database component)
(def store (job-history/create-store pool {:buffer-size 500}))
;; In the worker execution path
(job-history/record-start! store job worker-id correlation-id)
;; ... job runs ...
(job-history/record-completion! store (:id job) worker-id correlation-id elapsed-ms)
;; Query DB history
(job-history/get-by-job-id store job-id)
(job-history/query store {:from (java.util.Date.) :status "failed" :limit 50})
;; Inspect recent executions from the ring buffer (no DB)
(job-history/recent store 20)
;; Wire up to monitoring events so the ring buffer is populated without
;; routing writes through this component:
(monitoring/on emitter :job/completed
(fn [{:keys [data]}] (job-history/observe! store data)))
;; Scheduled maintenance
(job-history/expire! store)
Ring buffer notes:
record-start! writes to the DB only - started records are not buffered.
record-completion!, record-failure!, and record-partial-success! write to
the DB and push the terminal record into the ring buffer.
observe! adds directly to the buffer without any DB interaction.(create-store pool)(create-store pool config)Creates a history store backed by pool with optional config. pool may be nil for pure in-memory (ring buffer) use. config keys: :buffer-size (default 1000), :history-retention (default '30 days').
Creates a history store backed by pool with optional config. pool may be nil for pure in-memory (ring buffer) use. config keys: :buffer-size (default 1000), :history-retention (default '30 days').
(expire! store)Deletes history records whose expires_at has passed. Returns count deleted.
Deletes history records whose expires_at has passed. Returns count deleted.
(get-by-correlation-id store correlation-id)Returns all history records for correlation-id from the database.
Returns all history records for correlation-id from the database.
(get-by-job-id store job-id)Returns all history records for job-id from the database, newest first.
Returns all history records for job-id from the database, newest first.
(observe! store history-record)Adds history-record directly to the ring buffer. No DB interaction. Use for testing, or to populate the buffer from monitoring event subscribers.
Adds history-record directly to the ring buffer. No DB interaction. Use for testing, or to populate the buffer from monitoring event subscribers.
(query store criteria)Returns history records matching criteria map from the database. Criteria keys (all optional): :from, :to (inst?), :task-identifier, :status ('started' | 'completed' | 'failed' | 'partial_success'), :limit (default 100). Results ordered by started_at descending.
Returns history records matching criteria map from the database.
Criteria keys (all optional): :from, :to (inst?), :task-identifier,
:status ('started' | 'completed' | 'failed' | 'partial_success'),
:limit (default 100). Results ordered by started_at descending.(recent store)(recent store n)Returns records from the in-memory ring buffer in insertion order. With n, returns the last n records. No database access.
Returns records from the in-memory ring buffer in insertion order. With n, returns the last n records. No database access.
(record-completion! store job-id worker-id correlation-id execution-time-ms)Writes job completion to the database and adds the record to the ring buffer. Returns the updated HistoryRecord.
Writes job completion to the database and adds the record to the ring buffer. Returns the updated HistoryRecord.
(record-failure! store job-id worker-id correlation-id execution-time-ms error)Writes job failure to the database and adds the record to the ring buffer. error may be a Throwable or a string. Returns the updated HistoryRecord.
Writes job failure to the database and adds the record to the ring buffer. error may be a Throwable or a string. Returns the updated HistoryRecord.
(record-partial-success! store
job-id
worker-id
correlation-id
execution-time-ms
partial-results)Writes partial success to the database and adds the record to the ring buffer. Returns the updated HistoryRecord.
Writes partial success to the database and adds the record to the ring buffer. Returns the updated HistoryRecord.
(record-start! store job worker-id correlation-id)(record-start! store job worker-id correlation-id opts)Writes job execution start to the database. Does not add to ring buffer. Returns the created HistoryRecord. opts key: :history-retention overrides the store's configured retention.
Writes job execution start to the database. Does not add to ring buffer. Returns the created HistoryRecord. opts key: :history-retention overrides the store's configured retention.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |