Class: Lev::BackgroundJob
Constant Summary
collapse
- STATE_QUEUED =
'queued'
- STATE_WORKING =
'working'
- STATE_COMPLETED =
'completed'
- STATE_FAILED =
'failed'
- STATE_KILLED =
'killed'
- STATE_UNKNOWN =
'unknown'
- STATES =
[
STATE_QUEUED,
STATE_WORKING,
STATE_COMPLETED,
STATE_FAILED,
STATE_KILLED,
STATE_UNKNOWN
].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of BackgroundJob.
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/lev/background_job.rb', line 23
def initialize(attrs = {})
@id = attrs[:id] || attrs['id'] || SecureRandom.uuid
@status = attrs[:status] || attrs['status'] || STATE_UNKNOWN
@progress = attrs[:progress] || attrs['progress'] || 0
@errors = attrs[:errors] || attrs['errors'] || []
set({ id: id,
status: status,
progress: progress,
errors: errors })
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
95
96
97
|
# File 'lib/lev/background_job.rb', line 95
def method_missing(method_name, *args)
instance_variable_get("@#{method_name}") || super
end
|
Instance Attribute Details
Returns the value of attribute errors.
5
6
7
|
# File 'lib/lev/background_job.rb', line 5
def errors
@errors
end
|
Returns the value of attribute id.
5
6
7
|
# File 'lib/lev/background_job.rb', line 5
def id
@id
end
|
Returns the value of attribute progress.
5
6
7
|
# File 'lib/lev/background_job.rb', line 5
def progress
@progress
end
|
Returns the value of attribute status.
5
6
7
|
# File 'lib/lev/background_job.rb', line 5
def status
@status
end
|
Class Method Details
47
48
49
|
# File 'lib/lev/background_job.rb', line 47
def self.all
job_ids.map { |id| find(id) }
end
|
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/lev/background_job.rb', line 35
def self.find(id)
attrs = { id: id }
if job = fetch_and_parse(job_key(id))
attrs.merge!(job)
else
attrs.merge!(status: STATE_UNKNOWN)
end
new(attrs)
end
|
Instance Method Details
#add_error(error, options = { }) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/lev/background_job.rb', line 72
def add_error(error, options = { })
options = { is_fatal: false }.merge(options)
@errors << { is_fatal: options[:is_fatal],
code: error.code,
message: error.message,
data: error.data }
set(errors: @errors)
end
|
#as_json(options = {}) ⇒ Object
Rails compatibility returns a Hash of all key-value pairs that have been #set()
83
84
85
|
# File 'lib/lev/background_job.rb', line 83
def as_json(options = {})
stored
end
|
#completed! ⇒ Object
68
69
70
|
# File 'lib/lev/background_job.rb', line 68
def completed!
set({status: STATE_COMPLETED, progress: 1.0})
end
|
#respond_to?(method_name) ⇒ Boolean
99
100
101
102
103
104
105
|
# File 'lib/lev/background_job.rb', line 99
def respond_to?(method_name)
if method_name.match /\?$/
super
else
instance_variable_get("@#{method_name}").present? || super
end
end
|
#save(incoming_hash) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/lev/background_job.rb', line 87
def save(incoming_hash)
if reserved = incoming_hash.select { |k, _| RESERVED_KEYS.include?(k) }.first
raise IllegalArgument, "Cannot set reserved key: #{reserved[0]}"
else
set(incoming_hash)
end
end
|
#set_progress(at, out_of = nil) ⇒ Object
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/lev/background_job.rb', line 51
def set_progress(at, out_of = nil)
progress = compute_fractional_progress(at, out_of)
data_to_set = { progress: progress }
data_to_set[:status] = STATE_COMPLETED if 1.0 == progress
set(data_to_set)
progress
end
|