Class: Ductwork::Execution

Inherits:
Record
  • Object
show all
Defined in:
lib/ductwork/models/execution.rb

Overview

rubocop:todo Metrics/ClassLength

Defined Under Namespace

Classes: CommitFailed

Constant Summary collapse

FAILED_EXECUTION_TIMEOUT =
10.seconds

Instance Method Summary collapse

Instance Method Details

#call(pipeline, owner_process_id) ⇒ Object



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
49
50
51
52
# File 'lib/ductwork/models/execution.rb', line 18

def call(pipeline, owner_process_id)
  Ductwork.logger.debug(
    msg: "Executing job",
    role: :job_worker,
    pipeline: pipeline,
    job_klass: job.klass
  )
  args = JSON.parse(job.input_args)["args"]
  instance = Object.const_get(job.klass).build_for_execution(job.step.run_id, *args)
  create_attempt!(started_at: Time.current)
  output_payload = nil

  begin
    output_payload = instance.execute
    Ductwork::FaultInjection.checkpoint(:during_job_execution)
  rescue StandardError => e
    errored!(e, owner_process_id)
    log_job_executed(pipeline, "error")

    return
  end

  # AT-LEAST-ONCE CONTRACT: `instance.execute` has already run and any
  # side effects it performed are now durable. The commit below can still
  # fail (CommitFailed) if the reaper clobbered this claim, in which case
  # `crashed!` creates a fresh availability and the job runs AGAIN.
  # Ductwork guarantees at-least-once, never exactly-once, execution for
  # the forking worker model: a job body may be re-run after a successful
  # side effect. Jobs with non-idempotent effects MUST guard them at the
  # application layer (use `Step#idempotency_key`). The reuse window is
  # widened by transient stale claims; narrowing that is a separate fix,
  # but it cannot eliminate this contract.
  succeeded!(output_payload, owner_process_id)
  log_job_executed(pipeline, "succeeded")
end

#crashed!Object

rubocop:todo Metrics



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ductwork/models/execution.rb', line 74

def crashed! # rubocop:todo Metrics
  run = job.step.run
  max_crash = Ductwork.configuration.job_worker_max_crash(
    pipeline: run.pipeline_klass,
    step: job.klass
  )

  Ductwork::Record.transaction do # rubocop:todo Metrics/BlockLength
    rows_updated = Ductwork::Execution
                   .where(id: id, process_id: process_id, completed_at: nil)
                   .update_all(completed_at: Time.current)

    return if rows_updated.zero?

    reload
    attempt&.update!(completed_at: Time.current)
    create_result!(result_type: "process_crashed")

    if crash_count < max_crash
      new_crash_count = crash_count + 1
      started_at = crash_backoff_at(new_crash_count, max_crash)

      new_execution = job.executions.create!(
        retry_count: retry_count,
        crash_count: new_crash_count,
        started_at: started_at
      )
      new_execution.create_availability!(
        started_at: started_at,
        pipeline_klass: run.pipeline_klass
      )
    else
      job.step.update!(status: :failed)

      Ductwork.logger.error(
        msg: "Job exceeded crash limit and failed",
        job_id: job.id,
        job_klass: job.klass,
        run_id: run.id,
        role: :job_worker
      )
    end
  end
end

#errored!(error, owner_process_id) ⇒ Object

rubocop:todo Metrics



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ductwork/models/execution.rb', line 119

def errored!(error, owner_process_id) # rubocop:todo Metrics
  run = job.step.run
  completed_at = Time.current
  max_retry = Ductwork.configuration.job_worker_max_retry(
    pipeline: run.pipeline_klass,
    step: job.klass
  )

  Ductwork::Record.transaction do # rubocop:todo Metrics/BlockLength
    rows_updated = Ductwork::Execution
                   .where(id: id, completed_at: nil, process_id: owner_process_id)
                   .update_all(completed_at:)

    if rows_updated.zero?
      raise Ductwork::Execution::CommitFailed, "Reaper clobbered claimed job execution"
    end

    attempt.update!(completed_at: Time.current)
    create_result!(
      result_type: "failure",
      error_klass: error.class.to_s,
      error_message: error.message,
      error_backtrace: error.backtrace.join("\n")
    )

    if retry_count < max_retry
      retry_at = Ductwork::DatabaseClock.now + FAILED_EXECUTION_TIMEOUT
      new_execution = job.executions.create!(
        retry_count: retry_count + 1,
        crash_count: crash_count,
        started_at: retry_at
      )

      new_execution.create_availability!(
        started_at: retry_at,
        pipeline_klass: run.pipeline_klass
      )

      Ductwork.logger.warn(
        msg: "Job errored",
        error_klass: error.class.name,
        error_message: error.message,
        job_id: job.id,
        job_klass: job.klass,
        run_id: run.id,
        role: :job_worker
      )
    elsif retry_count >= max_retry
      job.step.update!(status: :failed)

      Ductwork.logger.error(
        msg: "Job exhausted retries and failed",
        error_klass: error.class.name,
        error_message: error.message,
        job_id: job.id,
        job_klass: job.klass,
        run_id: run.id,
        role: :job_worker
      )
    end
  end
end

#succeeded!(output_payload, owner_process_id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ductwork/models/execution.rb', line 54

def succeeded!(output_payload, owner_process_id)
  completed_at = Time.current
  payload = JSON.dump({ payload: output_payload })

  Ductwork::Record.transaction do
    rows_updated = Ductwork::Execution
                   .where(id: id, completed_at: nil, process_id: owner_process_id)
                   .update_all(completed_at:)

    if rows_updated.zero?
      raise Ductwork::Execution::CommitFailed, "Reaper clobbered claimed job execution"
    end

    job.update!(output_payload: payload, completed_at: Time.current)
    attempt.update!(completed_at: Time.current)
    create_result!(result_type: "success")
    job.step.update!(status: :advancing)
  end
end