bg_jobs

Types

Main type of the library, holds references queues, dispatcher, 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 = bg_jobs.new(db_adapter)
 ...
 |> bg_jobs.build()
 
 bg_jobs.new_job("example_job", "payload")
 |> bg_jobs.enqueue_job(bg);
pub type BgJobs {
  BgJobs(
    supervisor: process.Subject(supervisor.Message),
    queue_registry: registries.QueueRegistry,
    dispatcher_registry: registries.DispatcherRegistry,
    scheduled_jobs_registry: registries.ScheduledJobRegistry,
  )
}

Constructors

  • BgJobs(
      supervisor: process.Subject(supervisor.Message),
      queue_registry: registries.QueueRegistry,
      dispatcher_registry: registries.DispatcherRegistry,
      scheduled_jobs_registry: registries.ScheduledJobRegistry,
    )

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

Example

 let bg = 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 BgJobsSupervisorSpec {
  BgJobsSupervisorSpec(
    db_adapter: db_adapter.DbAdapter,
    event_listeners: List(events.EventListener),
    frequency_period: Int,
    max_frequency: Int,
    queues: List(queue.Spec),
    scheduled_jobs: List(scheduled_job.Spec),
  )
}

Constructors

  • BgJobsSupervisorSpec(
      db_adapter: db_adapter.DbAdapter,
      event_listeners: List(events.EventListener),
      frequency_period: Int,
      max_frequency: Int,
      queues: List(queue.Spec),
      scheduled_jobs: List(scheduled_job.Spec),
    )

Functions

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

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

Example

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

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

Enqueues a job for queues to consume when available

Example

let bg = bg_jobs.new() |> ... |> bg_jobs.build()
bg_jobs.new_job("example_job", "payload")
|> bg_jobs.enqueue_job(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(db_adapter: DbAdapter) -> BgJobsSupervisorSpec

Create a new default BgJobsSupervisorSpec

Example

 let bg = 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 start_processing_all(bg: BgJobs) -> Nil

Start the polling for all scheduled and queued jobs

pub fn stop_processing_all(bg: BgJobs) -> Nil

Stop the polling for all scheduled and queued jobs

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

Add an event_listener to all queues under the supervisor

Example

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

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

Example

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

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

Example

 let bg = bg_jobs.new(db_adapter)
 |> with_scheduled_job(scheduled_job.new(
   example_worker, 
   scheduled_job.interval_minutes(1)
 ))
pub fn with_supervisor_frequency_period(
  spec: BgJobsSupervisorSpec,
  frequency_period: Int,
) -> BgJobsSupervisorSpec

Set the supervisors frequency period

Example

 let bg = bg_jobs.new(db_adapter)
 |> with_supervisor_frequency_period(1)
pub fn with_supervisor_max_frequency(
  spec: BgJobsSupervisorSpec,
  max_frequency: Int,
) -> BgJobsSupervisorSpec

Set the supervisors max frequency

Example

 let bg = bg_jobs.new(db_adapter)
 |> with_supervisor_max_frequency(5)
Search Document