bg_jobs

Types

Main type of the library, holds references queues, and scheduled jobs. This is then passed as an argument when you want to interact with the background jobs in some way, for example enqueueing

Example

 let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
  |> bg_jobs.new(db_adapter)
 ...
 |> bg_jobs.build()
 
 bg_jobs.new_job("example_job", "payload")
 |> bg_jobs.enqueue(bg);
pub type BgJobs {
  BgJobs(supervisor: process.Pid, enqueue_state: EnqueueState)
}

Constructors

  • BgJobs(supervisor: process.Pid, enqueue_state: EnqueueState)

Specification for how the supervisor, queues, scheduled_jobs and event_listeners should be setup. It’s built using the builder functions

Example

 let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
  bg_jobs.new(db_adapter)
  // Event listeners
  |> bg_jobs.with_event_listener(logger_event_listener.listner)
  // Queues
  |> bg_jobs.with_queue(queue.new("default_queue"))
  // Scheduled jobs 
  |> bg_jobs.with_scheduled_job(scheduled_job.new(
    cleanup_db_job.worker(),
    scheduled_job.interval_minutes(1),
  )) 
  |> bg_jobs.build()
pub type BgJobsBuilder {
  BgJobsBuilder(
    supervisor: sup.Builder,
    db_adapter: db_adapter.DbAdapter,
    event_listeners: List(events.EventListener),
    queues: List(queue.Spec),
    scheduled_jobs: List(scheduled_job.SpecBuilder),
  )
}

Constructors

  • BgJobsBuilder(
      supervisor: sup.Builder,
      db_adapter: db_adapter.DbAdapter,
      event_listeners: List(events.EventListener),
      queues: List(queue.Spec),
      scheduled_jobs: List(scheduled_job.SpecBuilder),
    )
pub opaque type EnqueueState

Functions

pub fn available_at_from_availability(
  availability: JobAvailability,
) -> #(#(Int, Int, Int), #(Int, Int, Int))

Convert JobAvailability to erlang date time

pub fn build(spec: BgJobsBuilder) -> Result(BgJobs, BgJobError)

Create the supervisor and all it’s queues based on the provided spec

Example

 let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
    |> bg_jobs.new(db_adapter)
    ...
    |> bg_jobs.build()

pub fn enqueue(
  job_request: JobEnqueueRequest,
  bg: BgJobs,
) -> Result(Job, BgJobError)

Enqueues a job for queues to consume when available

Example

let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
  |> bg_jobs.new() |> ... |> bg_jobs.build()
  |> bg_jobs.new_job("example_job", "payload")
  |> bg_jobs.enqueue(bg);
pub fn job_with_available_at(
  job_request: JobEnqueueRequest,
  availabile_at: #(#(Int, Int, Int), #(Int, Int, Int)),
) -> JobEnqueueRequest

Set the availability to a specific time in the future

pub fn job_with_available_in(
  job_request: JobEnqueueRequest,
  availabile_in: Int,
) -> JobEnqueueRequest

Set the availability to a relative time in the future

pub fn new(
  supervisor: Builder,
  db_adapter: DbAdapter,
) -> BgJobsBuilder

Create a new default BgJobsBuilder

Example

 let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
  bg_jobs.new(db_adapter)
pub fn new_job(
  name: String,
  payload: String,
) -> JobEnqueueRequest

Create a new job request that can be enqueued

pub fn with_event_listener(
  spec: BgJobsBuilder,
  event_listener: fn(Event) -> Nil,
) -> BgJobsBuilder

Add an event_listener to all queues under the supervisor

Example

 let bg = 
   static_supervisor.new(static_supervisor.OneForOne)
   |> bg_jobs.new(db_adapter)
   |> with_event_listener(logger_event_listener.listener)
pub fn with_queue(
  spec: BgJobsBuilder,
  queue: Spec,
) -> BgJobsBuilder

Add a queue-spec to create a new queue with the supervisor

Example

 let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
  |> bg_jobs.new(db_adapter)
  |> with_queue(queue.new("example_queue") |> queue.add_worker(example_worker))
pub fn with_scheduled_job(
  spec: BgJobsBuilder,
  scheduled_job: SpecBuilder,
) -> BgJobsBuilder

Add a scheduled job spec to create a new queue with the supervisor

Example

 let bg = 
  static_supervisor.new(static_supervisor.OneForOne)
  |> bg_jobs.new(db_adapter)
  |> with_scheduled_job(scheduled_job.new(
    example_worker, 
    scheduled_job.interval_minutes(1)
  ))
Search Document