Skip to content

Commit 21031c7

Browse files
committed
Extract polling_interval to scheduler configuration
1 parent ebd9629 commit 21031c7

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

lib/generators/solid_queue/install/templates/config/queue.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ default: &default
77
threads: 3
88
processes: <%%= ENV.fetch("JOB_CONCURRENCY", 1) %>
99
polling_interval: 0.1
10+
scheduler:
11+
polling_interval: 1
1012

1113
development:
1214
<<: *default

lib/solid_queue/configuration.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def instantiate
2828
concurrency_maintenance_interval: 600
2929
}
3030

31+
SCHEDULER_DEFAULTS = {
32+
polling_interval: 1
33+
}
34+
3135
DEFAULT_CONFIG_FILE_PATH = "config/queue.yml"
3236
DEFAULT_RECURRING_SCHEDULE_FILE_PATH = "config/recurring.yml"
3337

@@ -93,7 +97,7 @@ def default_options
9397
end
9498

9599
def invalid_tasks
96-
static_recurring_tasks.select(&:invalid?)
100+
recurring_tasks.select(&:invalid?)
97101
end
98102

99103
def only_work?
@@ -122,11 +126,9 @@ def dispatchers
122126
end
123127

124128
def schedulers
125-
if !skip_recurring_tasks?
126-
[ Process.new(:scheduler, recurring_tasks: static_recurring_tasks) ]
127-
else
128-
[]
129-
end
129+
return [] if skip_recurring_tasks?
130+
131+
[ Process.new(:scheduler, { recurring_tasks:, **scheduler_options.with_defaults(SCHEDULER_DEFAULTS) }) ]
130132
end
131133

132134
def workers_options
@@ -139,17 +141,25 @@ def dispatchers_options
139141
.map { |options| options.dup.symbolize_keys }
140142
end
141143

142-
def static_recurring_tasks
143-
@static_recurring_tasks ||= recurring_tasks_config.map do |id, options|
144+
def scheduler_options
145+
@scheduler_options ||= processes_config.fetch(:scheduler, {}).dup.symbolize_keys
146+
end
147+
148+
def recurring_tasks
149+
@recurring_tasks ||= recurring_tasks_config.map do |id, options|
144150
RecurringTask.from_configuration(id, **options) if options&.has_key?(:schedule)
145151
end.compact
146152
end
147153

148154
def processes_config
149155
@processes_config ||= config_from \
150-
options.slice(:workers, :dispatchers).presence || options[:config_file],
151-
keys: [ :workers, :dispatchers ],
152-
fallback: { workers: [ WORKER_DEFAULTS ], dispatchers: [ DISPATCHER_DEFAULTS ] }
156+
options.slice(:workers, :dispatchers, :scheduler).presence || options[:config_file],
157+
keys: [ :workers, :dispatchers, :scheduler ],
158+
fallback: {
159+
workers: [ WORKER_DEFAULTS ],
160+
dispatchers: [ DISPATCHER_DEFAULTS ],
161+
scheduler: SCHEDULER_DEFAULTS
162+
}
153163
end
154164

155165
def recurring_tasks_config

lib/solid_queue/scheduler.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Scheduler < Processes::Base
55
include Processes::Runnable
66
include LifecycleHooks
77

8-
attr_reader :recurring_schedule
8+
attr_reader :recurring_schedule, :polling_interval
99

1010
after_boot :run_start_hooks
1111
after_boot :schedule_recurring_tasks
@@ -15,6 +15,8 @@ class Scheduler < Processes::Base
1515

1616
def initialize(recurring_tasks:, **options)
1717
@recurring_schedule = RecurringSchedule.new(recurring_tasks)
18+
options = options.dup.with_defaults(SolidQueue::Configuration::SCHEDULER_DEFAULTS)
19+
@polling_interval = options[:polling_interval]
1820

1921
super(**options)
2022
end
@@ -24,8 +26,6 @@ def metadata
2426
end
2527

2628
private
27-
SLEEP_INTERVAL = 60 # Right now it doesn't matter, can be set to 1 in the future for dynamic tasks
28-
2929
def run
3030
loop do
3131
break if shutting_down?
@@ -36,7 +36,7 @@ def run
3636
end
3737
end
3838

39-
interruptible_sleep(SLEEP_INTERVAL)
39+
interruptible_sleep(polling_interval)
4040
end
4141
ensure
4242
SolidQueue.instrument(:shutdown_process, process: self) do

test/unit/scheduler_test.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ class SchedulerTest < ActiveSupport::TestCase
7070
end
7171

7272
test "updates metadata after adding dynamic task post-start" do
73-
scheduler = SolidQueue::Scheduler.new(recurring_tasks: {}).tap do |s|
74-
s.define_singleton_method(:interruptible_sleep) { |interval| sleep 0.1 }
75-
s.start
76-
end
73+
scheduler = SolidQueue::Scheduler.new(recurring_tasks: {}, polling_interval: 0.1).tap(&:start)
7774

7875
wait_for_registered_processes(1, timeout: 1.second)
7976

@@ -109,10 +106,7 @@ class SchedulerTest < ActiveSupport::TestCase
109106
arguments: [ 42 ]
110107
)
111108

112-
scheduler = SolidQueue::Scheduler.new(recurring_tasks: {}).tap do |s|
113-
s.define_singleton_method(:interruptible_sleep) { |interval| sleep 0.1 }
114-
s.start
115-
end
109+
scheduler = SolidQueue::Scheduler.new(recurring_tasks: {}, polling_interval: 0.1).tap(&:start)
116110

117111
wait_for_registered_processes(1, timeout: 1.second)
118112

0 commit comments

Comments
 (0)