4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/lcp_ruby/background_jobs/executor_job.rb', line 4
def perform(execution_id)
execution = resolve_execution(execution_id)
return unless execution
if execution.respond_to?(:cancellation_requested?) && execution.cancellation_requested?
execution.update!(status: :cancelled, completed_at: Time.current)
return
end
now = Time.current
attrs = { status: :running, last_attempt_at: now }
attrs[:started_at] = now if execution.started_at.nil?
execution.update!(attrs)
execution.increment!(:attempt)
definition = Registry.definition_by_name(execution.job_type)
unless definition
execution.update!(
status: :failed,
error_message: I18n.t("lcp_ruby.background_jobs.definition_not_found",
job_type: execution.job_type,
default: "Job definition '%{job_type}' not found"),
completed_at: Time.current
)
return
end
handler = HandlerFactory.build(definition, execution)
if definition.timeout && definition.timeout > 0
handler.log!("Applying Timeout.timeout(#{definition.timeout}s) — unreliable for non-interruptible operations", level: :warn)
Timeout.timeout(definition.timeout) { handler.perform }
else
handler.perform
end
handler.flush_log!
execution.update!(status: :completed, progress: 100, completed_at: Time.current)
rescue CancellationError
execution.update!(status: :cancelled, completed_at: Time.current)
rescue => e
handle_error(execution, definition, e)
end
|