Skip to content

Commit

Permalink
Extract Job::Schedulable concern from Job::Executable
Browse files Browse the repository at this point in the history
  • Loading branch information
rosa committed Dec 26, 2023
1 parent 03eb148 commit ea7e12c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
10 changes: 2 additions & 8 deletions app/models/solid_queue/job/executable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ module Executable
extend ActiveSupport::Concern

included do
include Clearable, ConcurrencyControls
include Clearable, ConcurrencyControls, Schedulable

has_one :ready_execution, dependent: :destroy
has_one :claimed_execution, dependent: :destroy
has_one :failed_execution, dependent: :destroy

has_one :scheduled_execution, dependent: :destroy

after_create :prepare_for_execution

scope :finished, -> { where.not(finished_at: nil) }
Expand Down Expand Up @@ -70,7 +68,7 @@ def successfully_scheduled(jobs)
end
end

%w[ ready claimed failed scheduled ].each do |status|
%w[ ready claimed failed ].each do |status|
define_method("#{status}?") { public_send("#{status}_execution").present? }
end

Expand Down Expand Up @@ -117,10 +115,6 @@ def failed_with(exception)
end

private
def schedule
ScheduledExecution.create_or_find_by!(job_id: id)
end

def ready
ReadyExecution.create_or_find_by!(job_id: id)
end
Expand Down
44 changes: 44 additions & 0 deletions app/models/solid_queue/job/schedulable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module SolidQueue
class Job
module Schedulable
extend ActiveSupport::Concern

included do
has_one :scheduled_execution, dependent: :destroy

scope :scheduled, -> { where.not(finished_at: nil) }
end

class_methods do
def schedule_all(jobs)
schedule_all_at_once(jobs)
successfully_scheduled(jobs)
end

private
def schedule_all_at_once(jobs)
ScheduledExecution.create_all_from_jobs(jobs)
end

def successfully_scheduled(jobs)
where(id: ScheduledExecution.where(job_id: jobs.map(&:id)).pluck(:job_id))
end
end

def scheduled?
scheduled_execution.present?
end

def due?
scheduled_at.nil? || scheduled_at <= Time.current
end

private
def schedule
ScheduledExecution.create_or_find_by!(job_id: id)
end
end
end
end

0 comments on commit ea7e12c

Please sign in to comment.