Class: LcpRuby::BackgroundJobs::BaseHandler
- Inherits:
-
Object
- Object
- LcpRuby::BackgroundJobs::BaseHandler
show all
- Defined in:
- lib/lcp_ruby/background_jobs/base_handler.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(execution, definition) ⇒ BaseHandler
Returns a new instance of BaseHandler.
6
7
8
9
10
11
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 6
def initialize(execution, definition)
@execution = execution
@definition = definition
@last_progress_at = nil
@last_log_flush_at = nil
end
|
Instance Attribute Details
#definition ⇒ Object
Returns the value of attribute definition.
4
5
6
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 4
def definition
@definition
end
|
#execution ⇒ Object
Returns the value of attribute execution.
4
5
6
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 4
def execution
@execution
end
|
Instance Method Details
#attach_result!(data, filename:, content_type:) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 89
def attach_result!(data, filename:, content_type:)
unless execution.respond_to?(:result_file) && execution.respond_to?(:result_file=)
raise JobError, "JobExecution model does not have a result_file attachment"
end
io = data.is_a?(StringIO) ? data : StringIO.new(data.to_s)
execution.result_file.attach(
io: io,
filename: filename,
content_type: content_type
)
end
|
#check_cancellation! ⇒ Object
58
59
60
61
62
63
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 58
def check_cancellation!
execution.reload
if execution.respond_to?(:cancellation_requested?) && execution.cancellation_requested?
raise CancellationError, "Job cancelled by user"
end
end
|
#flush_log! ⇒ Object
84
85
86
87
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 84
def flush_log!
execution.update_columns(log: execution.log, updated_at: Time.current)
@last_log_flush_at = Time.current
end
|
#log!(message, level: :info) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 73
def log!(message, level: :info)
entry = { "at" => Time.current.iso8601, "level" => level.to_s, "message" => message.to_s }
entry["attempt"] = execution.attempt if level.to_s == "error" && execution.respond_to?(:attempt)
current_log = execution.log || []
current_log << entry
execution.log = current_log
flush_log! if should_flush_log?
end
|
#params ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 17
def params
@params ||= begin
raw = execution.respond_to?(:params) ? execution.params : nil
case raw
when Hash then raw
when String
JSON.parse(raw)
else
{}
end
rescue JSON::ParserError
{}
end
end
|
13
14
15
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 13
def perform
raise NotImplementedError, "#{self.class}#perform must be implemented"
end
|
#set_result_url!(url) ⇒ Object
102
103
104
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 102
def set_result_url!(url)
execution.update_columns(result_url: url, updated_at: Time.current)
end
|
#target_record ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 45
def target_record
@target_record ||= begin
model_name = execution.respond_to?(:target_model) ? execution.target_model : nil
record_id = execution.respond_to?(:target_id) ? execution.target_id : nil
return nil unless model_name.present? && record_id.present?
model_class = LcpRuby.registry.model_for(model_name)
model_class.find_by(id: record_id)
rescue LcpRuby::MetadataError
nil
end
end
|
#triggered_by ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 32
def triggered_by
@triggered_by ||= begin
snapshot = execution.respond_to?(:triggered_by_snapshot) ? execution.triggered_by_snapshot : nil
case snapshot
when Hash then snapshot
when String then JSON.parse(snapshot)
else {}
end
rescue JSON::ParserError
{}
end
end
|
#update_progress!(percentage) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/lcp_ruby/background_jobs/base_handler.rb', line 65
def update_progress!(percentage)
return if @last_progress_at && Time.current - @last_progress_at < 1.second
now = Time.current
execution.update_columns(progress: percentage.to_i.clamp(0, 100), updated_at: now)
@last_progress_at = now
end
|