Skip to content

Commit

Permalink
Wait for a random interval before starting the scheduler's tasks
Browse files Browse the repository at this point in the history
This comes from an idea from @djmb, so that, when we have multiple schedulers,
they don't start all at the same time after a deploy, enqueuing All The Things
and causing a thundering herd.
  • Loading branch information
rosa committed Nov 29, 2023
1 parent b7d65d8 commit a8adac9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/solid_queue/interruptible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def interrupt
end

def interruptible_sleep(time)
if self_pipe[:reader].wait_readable(time)
if time > 0 && self_pipe[:reader].wait_readable(time)
loop { self_pipe[:reader].read_nonblock(SELF_PIPE_BLOCK_SIZE) }
end
rescue Errno::EAGAIN, Errno::EINTR
Expand Down
9 changes: 9 additions & 0 deletions lib/solid_queue/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Runner

def start(mode: :supervised)
boot_in mode
observe_starting_delay

run_callbacks(:start) do
if mode == :async
Expand Down Expand Up @@ -44,6 +45,10 @@ def boot_in(mode)
SolidQueue.logger.info("[SolidQueue] Starting #{self}")
end

def observe_starting_delay
interruptible_sleep(initial_jitter)
end

def register_signal_handlers
%w[ INT TERM ].each do |signal|
trap(signal) do
Expand Down Expand Up @@ -81,6 +86,10 @@ def shutdown
procline "shutting down"
end

def initial_jitter
0
end

def stopping?
@stopping
end
Expand Down
4 changes: 4 additions & 0 deletions lib/solid_queue/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def unblock_blocked_executions
BlockedExecution.unblock(batch_size)
end

def initial_jitter
Kernel.rand(0...polling_interval)
end

def metadata
super.merge(batch_size: batch_size, polling_interval: polling_interval)
end
Expand Down

0 comments on commit a8adac9

Please sign in to comment.