Module: Ductr::JobStatus

Included in:
Job
Defined in:
lib/ductr/job_status.rb

Overview

This module contains the job’s status tracking logic. It relies on Active Job’s callbacks to write status into the store.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(job_class) ⇒ void

This method returns an undefined value.

Registers the ActiveJob’s ‘before_enqueue`, `before_perform` and `after_perform` callbacks to write status in the Ductr’s store. Intercepts and re-raises job’s exceptions to write the ‘:failed` status.

Parameters:

  • job_class (Class<Job>)

    The job’s class



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ductr/job_status.rb', line 21

def included(job_class)
  job_class.before_enqueue { |job| job.status = :queued }
  job_class.before_perform { |job| job.status = :working }
  job_class.after_perform { |job| job.status = :completed }

  job_class.rescue_from(Exception) do |e|
    @error = e
    self.status = :failed

    raise e
  end
end

Instance Method Details

#status=(status) ⇒ void

This method returns an undefined value.

Writes the job’s status into the Ductr’s store.

Parameters:

  • status (Symbol)

    The status of the job



42
43
44
45
# File 'lib/ductr/job_status.rb', line 42

def status=(status)
  @status = status
  Store.write_job(self)
end

#stopped?Boolean

Determines whether the job has a ‘completed` or `failed` status.

Returns:

  • (Boolean)

    True when the status is ‘completed` or `failed`



52
53
54
# File 'lib/ductr/job_status.rb', line 52

def stopped?
  %i[completed failed].include? status
end