Class: ActiveJob::Status::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/activejob-status/status.rb

Instance Method Summary collapse

Constructor Details

#initialize(job, options = {}) ⇒ Status

Returns a new instance of Status.



9
10
11
12
13
14
# File 'lib/activejob-status/status.rb', line 9

def initialize(job, options = {})
  options = ActiveJob::Status.options.merge(options)
  @defaults = options.fetch(:includes, [])
  @storage = ActiveJob::Status::Storage.new(options.without(:includes))
  @job = job
end

Instance Method Details

#[]=(key, value) ⇒ Object



16
17
18
# File 'lib/activejob-status/status.rb', line 16

def []=(key, value)
  update({key => value}, force: true)
end

#catch_exception(e) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/activejob-status/status.rb', line 71

def catch_exception(e)
  raise "cannot call #catch_exception when status is accessed from outside the job" if @job.is_a?(String)

  payload = {}
  payload[:status] = :failed if @defaults.include?(:status)
  payload[:serialized_job] = @job.serialize if @defaults.include?(:serialized_job)

  if @defaults.include?(:exception)
    message = e.message
    message = e.original_message if e.respond_to?(:original_message)
    payload[:exception] = {class: e.class.name, message: message}
  end

  update(payload, force: true)
end

#deleteObject



34
35
36
# File 'lib/activejob-status/status.rb', line 34

def delete
  @storage.delete(@job)
end

#job_idObject



38
39
40
# File 'lib/activejob-status/status.rb', line 38

def job_id
  @storage.job_id(@job)
end

#present?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/activejob-status/status.rb', line 52

def present?
  read.present?
end

#progressObject



46
47
48
49
50
# File 'lib/activejob-status/status.rb', line 46

def progress
  read.then do |hash|
    hash[:progress].to_f / hash[:total].to_f
  end
end

#readObject Also known as: to_h



20
21
22
# File 'lib/activejob-status/status.rb', line 20

def read
  @storage.read(@job)
end

#statusObject



42
43
44
# File 'lib/activejob-status/status.rb', line 42

def status
  read[:status]
end

#status_inquiryObject



56
57
58
# File 'lib/activejob-status/status.rb', line 56

def status_inquiry
  status.to_s.inquiry
end

#update(payload, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/activejob-status/status.rb', line 25

def update(payload, options = {})
  if @job.respond_to?(:progress)
    @job.progress.instance_variable_set(:@progress, payload[:progress]) if payload.include?(:progress)
    @job.progress.instance_variable_set(:@total, payload[:total]) if payload.include?(:total)
  end

  @storage.update(@job, payload, **options)
end

#update_defaults(status_key) ⇒ Object

Update default data



62
63
64
65
66
67
68
69
# File 'lib/activejob-status/status.rb', line 62

def update_defaults(status_key)
  raise "cannot call #update_defaults when status is accessed from outside the job" if @job.is_a?(String)

  payload = {}
  payload[:status] = status_key if @defaults.include?(:status)
  payload[:serialized_job] = @job.serialize if @defaults.include?(:serialized_job)
  update(payload, force: true)
end