Skip to content

Commit ca1f276

Browse files
committed
Generalize inserting a batch of executions from a batch of jobs
From now, used only when moving scheduled executions to ready executions. Also, change `assume_attributes_from_job` to `assumes_attributes_from_job`, 3rd person singular, to be consistent with other similar methods.
1 parent ad73d97 commit ca1f276

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

app/models/solid_queue/blocked_execution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module SolidQueue
44
class BlockedExecution < Execution
5-
assume_attributes_from_job :concurrency_key
5+
assumes_attributes_from_job :concurrency_key
66
before_create :set_expires_at
77

88
has_one :semaphore, foreign_key: :key, primary_key: :concurrency_key

app/models/solid_queue/execution.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,17 @@ class Execution < Record
1111
belongs_to :job
1212

1313
alias_method :discard, :destroy
14+
15+
class << self
16+
def create_all_from_jobs(jobs)
17+
insert_all execution_data_from_jobs(jobs)
18+
end
19+
20+
def execution_data_from_jobs(jobs)
21+
jobs.collect do |job|
22+
attributes_from_job(job).merge(job_id: job.id)
23+
end
24+
end
25+
end
1426
end
1527
end

app/models/solid_queue/execution/job_attributes.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ class Execution
55
module JobAttributes
66
extend ActiveSupport::Concern
77

8-
ASSUMIBLE_ATTRIBUTES_FROM_JOB = %i[ queue_name priority ]
8+
included do
9+
class_attribute :assumible_attributes_from_job, instance_accessor: false, default: %i[ queue_name priority ]
10+
end
911

1012
class_methods do
11-
def assume_attributes_from_job(*attributes)
12-
before_create -> { assume_attributes_from_job(ASSUMIBLE_ATTRIBUTES_FROM_JOB | attributes) }
13+
def assumes_attributes_from_job(*attribute_names)
14+
self.assumible_attributes_from_job |= attribute_names
15+
before_create -> { assume_attributes_from_job }
16+
end
17+
18+
def attributes_from_job(job)
19+
job.attributes.symbolize_keys.slice(*assumible_attributes_from_job)
1320
end
1421
end
1522

1623
private
17-
def assume_attributes_from_job(attributes)
18-
attributes.each do |attribute|
24+
def assume_attributes_from_job
25+
self.class.assumible_attributes_from_job.each do |attribute|
1926
send("#{attribute}=", job.send(attribute))
2027
end
2128
end

app/models/solid_queue/ready_execution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module SolidQueue
44
class ReadyExecution < Execution
55
scope :queued_as, ->(queue_name) { where(queue_name: queue_name) }
66

7-
assume_attributes_from_job
7+
assumes_attributes_from_job
88

99
class << self
1010
def claim(queue_list, limit, process_id)

app/models/solid_queue/scheduled_execution.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ScheduledExecution < Execution
66
scope :ordered, -> { order(scheduled_at: :asc, priority: :asc) }
77
scope :next_batch, ->(batch_size) { due.ordered.limit(batch_size) }
88

9-
assume_attributes_from_job :scheduled_at
9+
assumes_attributes_from_job :scheduled_at
1010

1111
class << self
1212
def dispatch_next_batch(batch_size)
@@ -34,19 +34,13 @@ def dispatch_batch(job_ids)
3434
end
3535

3636
def dispatch_at_once(jobs)
37-
ReadyExecution.insert_all ready_rows_from_batch(jobs)
37+
ReadyExecution.create_all_from_jobs jobs
3838
end
3939

4040
def dispatch_one_by_one(jobs)
4141
jobs.each(&:dispatch)
4242
end
4343

44-
def ready_rows_from_batch(jobs)
45-
jobs.map do |job|
46-
{ job_id: job.id, queue_name: job.queue_name, priority: job.priority }
47-
end
48-
end
49-
5044
def successfully_dispatched(job_ids)
5145
dispatched_and_ready(job_ids) + dispatched_and_blocked(job_ids)
5246
end

test/models/solid_queue/job_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class NonOverlappingGroupedJob2 < NonOverlappingJob
6565
assert_equal solid_queue_job, execution.job
6666
assert_equal "test", execution.queue_name
6767
assert_equal 8, execution.priority
68-
assert Time.now < execution.scheduled_at
68+
assert_equal solid_queue_job.scheduled_at, execution.scheduled_at
6969
end
7070

7171
test "enqueue jobs without concurrency controls" do

0 commit comments

Comments
 (0)