Class: Ductwork::Job
- Defined in:
- app/models/ductwork/job.rb
Overview
rubocop:todo Metrics/ClassLength
Constant Summary collapse
- FAILED_EXECUTION_TIMEOUT =
10.seconds
Class Method Summary collapse
-
.claim_latest(klass) ⇒ Object
rubocop:todo Metrics/MethodLength.
- .enqueue(step, args) ⇒ Object
Instance Method Summary collapse
Methods inherited from Record
Class Method Details
.claim_latest(klass) ⇒ Object
rubocop:todo Metrics/MethodLength
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 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/ductwork/job.rb', line 14 def self.claim_latest(klass) # rubocop:todo Metrics/MethodLength process_id = ::Process.pid id = Ductwork::Availability .joins(execution: { job: { step: :pipeline } }) .where("ductwork_availabilities.started_at <= ?", Time.current) .where(completed_at: nil) .where(ductwork_pipelines: { klass: }) .order(:created_at) .limit(1) .pluck(:id) .first if id.present? # TODO: probably makes sense to use SQL here instead of relying # on ActiveRecord to construct the correct `UPDATE` query rows_updated = nil Ductwork::Record.transaction do rows_updated = Ductwork::Availability .where(id: id, completed_at: nil) .update_all(completed_at: Time.current, process_id: process_id) Ductwork::Execution .joins(:availability) .where(completed_at: nil) .where(ductwork_availabilities: { id: }) .update_all(process_id:) end if rows_updated == 1 Ductwork.logger.debug( msg: "Job claimed", role: :job_worker, process_id: process_id, availability_id: id ) Ductwork::Job .joins(executions: :availability) .find_by(ductwork_availabilities: { id:, process_id: }) else Ductwork.logger.debug( msg: "Did not claim job, avoided race condition", role: :job_worker, process_id: process_id, availability_id: id ) nil end end end |
.enqueue(step, args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/ductwork/job.rb', line 63 def self.enqueue(step, args) job = Ductwork::Record.transaction do j = step.create_job!( klass: step.klass, started_at: Time.current, input_args: JSON.dump({ args: }) ) execution = j.executions.create!( started_at: Time.current, retry_count: 0 ) execution.create_availability!( started_at: Time.current ) j end Ductwork.logger.info( msg: "Job enqueued", job_id: job.id, job_klass: job.klass ) job end |
Instance Method Details
#execute(pipeline) ⇒ Object
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 118 119 120 121 122 123 |
# File 'app/models/ductwork/job.rb', line 90 def execute(pipeline) # i don't _really_ like this, but it should be fine for now... execution = executions.order(:created_at).last Ductwork.logger.debug( msg: "Executing job", role: :job_worker, pipeline: pipeline, job_klass: klass ) args = JSON.parse(input_args)["args"] instance = Object.const_get(klass).new(args) run = execution.create_run!( started_at: Time.current ) result = nil begin output_payload = instance.execute execution_succeeded!(execution, run, output_payload) result = "success" rescue StandardError => e execution_failed!(execution, run, e) result = "failure" ensure Ductwork.logger.info( msg: "Job executed", pipeline: pipeline, job_id: id, job_klass: klass, result: result, role: :job_worker ) end end |
#return_value ⇒ Object
125 126 127 128 129 |
# File 'app/models/ductwork/job.rb', line 125 def return_value if output_payload.present? JSON.parse(output_payload).fetch("payload", nil) end end |