diff --git a/app/models/solid_queue/job/executable.rb b/app/models/solid_queue/job/executable.rb index 2af0f04d..6fba2440 100644 --- a/app/models/solid_queue/job/executable.rb +++ b/app/models/solid_queue/job/executable.rb @@ -1,52 +1,54 @@ -module SolidQueue::Job::Executable - extend ActiveSupport::Concern +module SolidQueue + module Job::Executable + extend ActiveSupport::Concern - included do - has_one :ready_execution, dependent: :destroy - has_one :claimed_execution, dependent: :destroy - has_one :failed_execution, dependent: :destroy + included do + has_one :ready_execution, dependent: :destroy + has_one :claimed_execution, dependent: :destroy + has_one :failed_execution, dependent: :destroy - has_one :scheduled_execution, dependent: :destroy + has_one :scheduled_execution, dependent: :destroy - after_create :prepare_for_execution - end + after_create :prepare_for_execution + end - STATUSES = %w[ ready claimed failed scheduled ] + STATUSES = %w[ ready claimed failed scheduled ] - STATUSES.each do |status| - define_method("#{status}?") { public_send("#{status}_execution").present? } - end - - def prepare_for_execution - if due? - create_ready_execution! - else - create_scheduled_execution! + STATUSES.each do |status| + define_method("#{status}?") { public_send("#{status}_execution").present? } end - end - def finished - touch(:finished_at) - end + def prepare_for_execution + if due? + ReadyExecution.create_or_find_by!(job_id: id) + else + ScheduledExecution.create_or_find_by!(job_id: id) + end + end - def finished? - finished_at.present? - end + def finished + touch(:finished_at) + end - def failed_with(exception) - create_failed_execution!(exception: exception) - end + def finished? + finished_at.present? + end - def discard - destroy unless claimed? - end + def failed_with(exception) + FailedExecution.create_or_find_by!(job_id: id, exception: exception) + end - def retry - failed_execution&.retry - end + def discard + destroy unless claimed? + end - private - def due? - scheduled_at.nil? || scheduled_at <= Time.current + def retry + failed_execution&.retry end + + private + def due? + scheduled_at.nil? || scheduled_at <= Time.current + end + end end