Skip to content

Commit 5642d01

Browse files
committed
Extract Job::Schedulable concern from Job::Executable
1 parent 03eb148 commit 5642d01

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

app/models/solid_queue/job/executable.rb

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ module Executable
66
extend ActiveSupport::Concern
77

88
included do
9-
include Clearable, ConcurrencyControls
9+
include Clearable, ConcurrencyControls, Schedulable
1010

1111
has_one :ready_execution, dependent: :destroy
1212
has_one :claimed_execution, dependent: :destroy
1313
has_one :failed_execution, dependent: :destroy
1414

15-
has_one :scheduled_execution, dependent: :destroy
16-
1715
after_create :prepare_for_execution
1816

1917
scope :finished, -> { where.not(finished_at: nil) }
@@ -35,20 +33,11 @@ def dispatch_all(jobs)
3533
successfully_dispatched(jobs)
3634
end
3735

38-
def schedule_all(jobs)
39-
schedule_all_at_once(jobs)
40-
successfully_scheduled(jobs)
41-
end
42-
4336
private
4437
def dispatch_all_at_once(jobs)
4538
ReadyExecution.create_all_from_jobs jobs
4639
end
4740

48-
def schedule_all_at_once(jobs)
49-
ScheduledExecution.create_all_from_jobs(jobs)
50-
end
51-
5241
def dispatch_all_one_by_one(jobs)
5342
jobs.each(&:dispatch)
5443
end
@@ -64,13 +53,9 @@ def dispatched_and_ready(jobs)
6453
def dispatched_and_blocked(jobs)
6554
where(id: BlockedExecution.where(job_id: jobs.map(&:id)).pluck(:job_id))
6655
end
67-
68-
def successfully_scheduled(jobs)
69-
where(id: ScheduledExecution.where(job_id: jobs.map(&:id)).pluck(:job_id))
70-
end
7156
end
7257

73-
%w[ ready claimed failed scheduled ].each do |status|
58+
%w[ ready claimed failed ].each do |status|
7459
define_method("#{status}?") { public_send("#{status}_execution").present? }
7560
end
7661

@@ -117,10 +102,6 @@ def failed_with(exception)
117102
end
118103

119104
private
120-
def schedule
121-
ScheduledExecution.create_or_find_by!(job_id: id)
122-
end
123-
124105
def ready
125106
ReadyExecution.create_or_find_by!(job_id: id)
126107
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module SolidQueue
4+
class Job
5+
module Schedulable
6+
extend ActiveSupport::Concern
7+
8+
included do
9+
has_one :scheduled_execution, dependent: :destroy
10+
11+
scope :scheduled, -> { where.not(finished_at: nil) }
12+
end
13+
14+
class_methods do
15+
def schedule_all(jobs)
16+
schedule_all_at_once(jobs)
17+
successfully_scheduled(jobs)
18+
end
19+
20+
private
21+
def schedule_all_at_once(jobs)
22+
ScheduledExecution.create_all_from_jobs(jobs)
23+
end
24+
25+
def successfully_scheduled(jobs)
26+
where(id: ScheduledExecution.where(job_id: jobs.map(&:id)).pluck(:job_id))
27+
end
28+
end
29+
30+
def due?
31+
scheduled_at.nil? || scheduled_at <= Time.current
32+
end
33+
34+
private
35+
def schedule
36+
ScheduledExecution.create_or_find_by!(job_id: id)
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)