bg_jobs/jobs

Types

Represents data about a failed job.

pub type FailedJob {
  FailedJob(
    id: String,
    name: String,
    payload: String,
    attempts: Int,
    exception: String,
    created_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    available_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    failed_at: #(#(Int, Int, Int), #(Int, Int, Int)),
  )
}

Constructors

  • FailedJob(
      id: String,
      name: String,
      payload: String,
      attempts: Int,
      exception: String,
      created_at: #(#(Int, Int, Int), #(Int, Int, Int)),
      available_at: #(#(Int, Int, Int), #(Int, Int, Int)),
      failed_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    )

It’s the data representing a job in the queue.

It contains all the necessary information to process the job.

pub type Job {
  Job(
    id: String,
    name: String,
    payload: String,
    attempts: Int,
    created_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    available_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    reserved_at: option.Option(
      #(#(Int, Int, Int), #(Int, Int, Int)),
    ),
    reserved_by: option.Option(String),
  )
}

Constructors

  • Job(
      id: String,
      name: String,
      payload: String,
      attempts: Int,
      created_at: #(#(Int, Int, Int), #(Int, Int, Int)),
      available_at: #(#(Int, Int, Int), #(Int, Int, Int)),
      reserved_at: option.Option(
        #(#(Int, Int, Int), #(Int, Int, Int)),
      ),
      reserved_by: option.Option(String),
    )

Enum representing 3 ways of setting a jobs available_at

pub type JobAvailability {
  AvailableNow
  AvailableAt(#(#(Int, Int, Int), #(Int, Int, Int)))
  AvailableIn(Int)
}

Constructors

  • AvailableNow
  • AvailableAt(#(#(Int, Int, Int), #(Int, Int, Int)))
  • AvailableIn(Int)

Data that is needed to enqueue a new job

pub type JobEnqueueRequest {
  JobEnqueueRequest(
    name: String,
    payload: String,
    availability: JobAvailability,
  )
}

Constructors

  • JobEnqueueRequest(
      name: String,
      payload: String,
      availability: JobAvailability,
    )

Represents data about a succeeded job.

pub type SucceededJob {
  SucceededJob(
    id: String,
    name: String,
    payload: String,
    attempts: Int,
    created_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    available_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    succeeded_at: #(#(Int, Int, Int), #(Int, Int, Int)),
  )
}

Constructors

  • SucceededJob(
      id: String,
      name: String,
      payload: String,
      attempts: Int,
      created_at: #(#(Int, Int, Int), #(Int, Int, Int)),
      available_at: #(#(Int, Int, Int), #(Int, Int, Int)),
      succeeded_at: #(#(Int, Int, Int), #(Int, Int, Int)),
    )

Represents a job-worker responsible for executing specific job.

This is then what’s registered to a queue or scheduled job. Each job must implement this type, defining the job name and its execution logic.

Example

pub fn worker(email_service: SomeService) {
  Worker(job_name: "send_email", execute: fn(job) { 
    from_string(job) 
    |> email_service.send
  })
}
pub type Worker {
  Worker(
    job_name: String,
    handler: fn(Job) -> Result(Nil, String),
  )
}

Constructors

  • Worker(job_name: String, handler: fn(Job) -> Result(Nil, String))

Functions

pub fn new(name: String, payload: String) -> JobEnqueueRequest

Create a new job_request

pub fn with_available_at(
  job_request: JobEnqueueRequest,
  availabile_at: #(#(Int, Int, Int), #(Int, Int, Int)),
) -> JobEnqueueRequest

Set the availability of the job_request to a specific date-time

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

Set the availability of the job_request to a time in the future

Search Document