Skip to content

Commit 1713b14

Browse files
committed
Refactor slightly how executions assume attributes from job
Just to test how it feels to do it in this way.
1 parent e1b5aef commit 1713b14

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
class SolidQueue::BlockedExecution < SolidQueue::Execution
2-
before_create :assume_attributes_from_job
3-
4-
private
5-
def assume_attributes_from_job
6-
super
7-
self.concurrency_limit ||= job.concurrency_limit
8-
self.concurrency_key ||= job.concurrency_key
9-
end
2+
assume_attributes_from_job :concurrency_limit, :concurrency_key
103
end

app/models/solid_queue/execution.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
class SolidQueue::Execution < SolidQueue::Record
2+
include JobAttributes
3+
24
self.abstract_class = true
35

46
belongs_to :job
57

68
alias_method :discard, :destroy
7-
8-
private
9-
def assume_attributes_from_job
10-
self.queue_name ||= job&.queue_name
11-
self.priority = job&.priority if job&.priority.to_i > priority
12-
end
139
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module SolidQueue
2+
class Execution
3+
module JobAttributes
4+
extend ActiveSupport::Concern
5+
6+
ASSUMIBLE_ATTRIBUTES_FROM_JOB = %i[ queue_name priority ]
7+
8+
class_methods do
9+
def assume_attributes_from_job(*attributes)
10+
before_create -> { assume_attributes_from_job(ASSUMIBLE_ATTRIBUTES_FROM_JOB | attributes) }
11+
end
12+
end
13+
14+
private
15+
def assume_attributes_from_job(attributes)
16+
attributes.each do |attribute|
17+
send("#{attribute}=", job.send(attribute))
18+
end
19+
end
20+
end
21+
end
22+
end

app/models/solid_queue/ready_execution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ReadyExecution < Execution
33
scope :ordered, -> { order(priority: :asc) }
44
scope :not_paused, -> { where.not(queue_name: Pause.all_queue_names) }
55

6-
before_create :assume_attributes_from_job
6+
assume_attributes_from_job
77

88
class << self
99
def claim(queues, limit, process_id)

app/models/solid_queue/scheduled_execution.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class SolidQueue::ScheduledExecution < SolidQueue::Execution
33
scope :ordered, -> { order(scheduled_at: :asc, priority: :asc) }
44
scope :next_batch, ->(batch_size) { due.ordered.limit(batch_size) }
55

6-
before_create :assume_attributes_from_job
6+
assume_attributes_from_job :scheduled_at
77

88
class << self
99
def prepare_batch(batch)
@@ -27,10 +27,4 @@ def prepare_batch(batch)
2727
def execution_ready_attributes
2828
attributes.slice("job_id", "queue_name", "priority")
2929
end
30-
31-
private
32-
def assume_attributes_from_job
33-
super
34-
self.scheduled_at ||= job&.scheduled_at
35-
end
3630
end

0 commit comments

Comments
 (0)