-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a concurrent timer task to expire semaphores and unblock executions
This task is managed by the scheduler and uses its same polling interval to run.
- Loading branch information
Showing
5 changed files
with
55 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidQueue::Scheduler | ||
include SolidQueue::Runner | ||
module SolidQueue | ||
class Scheduler | ||
include Runner | ||
|
||
attr_accessor :batch_size, :polling_interval | ||
attr_accessor :batch_size, :polling_interval | ||
|
||
def initialize(**options) | ||
options = options.dup.with_defaults(SolidQueue::Configuration::SCHEDULER_DEFAULTS) | ||
set_callback :start, :before, :launch_concurrency_maintenance | ||
|
||
@batch_size = options[:batch_size] | ||
@polling_interval = options[:polling_interval] | ||
end | ||
def initialize(**options) | ||
options = options.dup.with_defaults(SolidQueue::Configuration::SCHEDULER_DEFAULTS) | ||
|
||
@batch_size = options[:batch_size] | ||
@polling_interval = options[:polling_interval] | ||
end | ||
|
||
private | ||
def run | ||
with_polling_volume do | ||
batch = SolidQueue::ScheduledExecution.next_batch(batch_size) | ||
private | ||
def run | ||
with_polling_volume do | ||
batch = SolidQueue::ScheduledExecution.next_batch(batch_size).tap(&:load) | ||
|
||
if batch.size > 0 | ||
procline "preparing #{batch.size} jobs for execution" | ||
if batch.size > 0 | ||
procline "preparing #{batch.size} jobs for execution" | ||
|
||
SolidQueue::ScheduledExecution.prepare_batch(batch) | ||
else | ||
procline "waiting" | ||
interruptible_sleep(polling_interval) | ||
SolidQueue::ScheduledExecution.prepare_batch(batch) | ||
else | ||
procline "waiting" | ||
interruptible_sleep(polling_interval) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def metadata | ||
super.merge(batch_size: batch_size, polling_interval: polling_interval) | ||
end | ||
def launch_concurrency_maintenance | ||
@concurrency_maintenance_task = Concurrent::TimerTask.new(run_now: true, execution_interval: polling_interval) do | ||
expire_semaphores | ||
unblock_blocked_executions | ||
end | ||
|
||
@concurrency_maintenance_task.execute | ||
end | ||
|
||
def expire_semaphores | ||
Semaphore.expired.in_batches(of: batch_size, &:delete_all) | ||
end | ||
|
||
def unblock_blocked_executions | ||
BlockedExecution.unblock(batch_size) | ||
end | ||
|
||
def metadata | ||
super.merge(batch_size: batch_size, polling_interval: polling_interval) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters