byplay.core

job-state-done

Constant for a done job state.

job-state-failed

Constant for a failed job state.

job-state-new

Constant for a new job state.

migrate

(migrate jdbc-conn)

Creates Byplay’s table and other stuff in the database (if it doesn’t already exist).

new-worker

(new-worker dbspec config)

Creates several threads for infinitely calling work-once in them with the specified polling interval. Returns a WorkerProtocol instance which can be started when needed.

dbspec specifies the database connection parameters and has the same format as in funcool/clojure.jdbc.

Config keys:

  • :queues - vector of queues to poll (order matters), default is nil which means that worker can take a job from any queue
  • :threads-num - number of worker threads, default is 1
  • :polling-interval - in msec, default is 5000
  • :on-fail - a function with signature (on-fail worker exception job-row-map) executed on exception inside a job, by default it prints a message into stderr
  • :on-ack - a function (on-ack worker ack) will be executed in child threads with the result of every work-once call. By default does nothing and is mostly useful for testing. If on-fail is specified then in case of a failed job on-ack will be executed after on-fail. You can (.interrupt (Thread/currentThread)) in on-ack to stop working in the particular child thread.

queue-clj->sql

(queue-clj->sql q-keyword)

Converts the queue name from a Clojure keyword into a string for SQL. The keyword must not have a namespace.

queue-sql->clj

(queue-sql->clj q-str)

Converts the queue name from a string into a Clojure keyword.

rollback

(rollback jdbc-conn)

Removes all Byplay data from the database (if there’s any).

schedule

(schedule jdbc-conn job-var & args)

Puts the job into the queue specified in the job’s metadata at ::queue. If no queue is specified then job will be scheduled to :default queue.

schedule-to

(schedule-to jdbc-conn queue job-var & args)

Puts the job into the specified queue. If nil queue is specified then job will be scheduled to :default queue.

work-once

(work-once jdbc-conn)(work-once jdbc-conn {:keys [queues], :as _config})

Low-level API. Processes a single job from one of the queues. The order of the queues matters. If no queues are specified than a job from any queue will be taken.

Returns an ack:

  • nil - if job was not found
  • job row map - if job is done, e.g. {:id 123 :state job-state-done :job "app.jobs/some-job" :args "(1 2)" :queue :some-queue}
  • [exception, job row map] - if job has failed

Job exception is not rethrown but the job is marked failed instead of done.

Do not call this function inside an already created transaction because work-once will create a new transaction itself. Otherwise, your outer transaction will be prematurely committed because nested transactions are not supported by PostgreSQL.

WorkerProtocol

protocol

members

interrupt

(interrupt _)

Asks the worker to gracefully finish working. Worker will wait for finishing of all currently executed jobs. You can also call join after interrupt to block your thread until worker is stopped. You cannot start the worker again after interruption.

join

(join _)

Blocks the current thread until the worker is stopped.

start

(start _)

Starts working in background threads.

state

(state _)

Returns the current worker state:

  • :new
  • :running
  • :terminated