-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reduce the number of threads used for reaping * Fix specs * Fix specs * Fix Gemfiles * Mandatory rubocop commit * Reduce performance impact of method as block * Smelly * Update README to a more sensible default * Reduce overhead of schedule_next_task as well
- Loading branch information
Showing
15 changed files
with
184 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# Trap interrupts to quit cleanly. See | ||
# https://twitter.com/mitchellh/status/283014103189053442 | ||
Signal.trap("INT") { abort } | ||
|
||
require "bundler/setup" | ||
require "ruby-prof" | ||
require "sidekiq-unique-jobs" | ||
|
||
SidekiqUniqueJobs.configure do |config| | ||
config.reaper_interval = 2 | ||
config.reaper_timeout = 1 | ||
config.reaper_count = 10_000 | ||
end | ||
|
||
TASK = SidekiqUniqueJobs::TimerTask.new(SidekiqUniqueJobs::Orphans::Manager.timer_task_options) do | ||
SidekiqUniqueJobs::Orphans::Manager.with_logging_context do | ||
SidekiqUniqueJobs::Orphans::Manager.redis do |_conn| | ||
SidekiqUniqueJobs::Orphans::Manager.refresh_reaper_mutex | ||
sleep(1) | ||
end | ||
end | ||
end | ||
|
||
counter = 0 | ||
result = RubyProf.profile do | ||
100.times do | ||
SidekiqUniqueJobs::Orphans::Manager.start(TASK) | ||
end | ||
|
||
while counter < 60 | ||
sleep(1) | ||
|
||
counter += 1 | ||
end | ||
end | ||
|
||
result.exclude_common_methods! | ||
printer = RubyProf::GraphPrinter.new(result) | ||
printer.print($stdout, min_percent: 2) |
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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module SidekiqUniqueJobs | ||
# @see [Concurrent::TimerTask] https://www.rubydoc.info/gems/concurrent-ruby/Concurrent/TimerTask | ||
# | ||
class TimerTask < ::Concurrent::TimerTask | ||
private | ||
|
||
def ns_initialize(opts, &task) | ||
set_deref_options(opts) | ||
|
||
self.execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL | ||
self.timeout_interval = opts[:timeout] || opts[:timeout_interval] || TIMEOUT_INTERVAL | ||
@run_now = opts[:now] || opts[:run_now] | ||
@executor = Concurrent::RubySingleThreadExecutor.new | ||
@running = Concurrent::AtomicBoolean.new(false) | ||
@task = task | ||
@value = nil | ||
|
||
self.observers = Concurrent::Collection::CopyOnNotifyObserverSet.new | ||
end | ||
|
||
def schedule_next_task(interval = execution_interval) | ||
exec_task = ->(completion) { execute_task(completion) } | ||
ScheduledTask.execute(interval, args: [Concurrent::Event.new], &exec_task) | ||
nil | ||
end | ||
|
||
# @!visibility private | ||
def execute_task(completion) # rubocop:disable Metrics/MethodLength | ||
return nil unless @running.true? | ||
|
||
timeout_task = -> { timeout_task(completion) } | ||
|
||
Concurrent::ScheduledTask.execute( | ||
timeout_interval, | ||
args: [completion], | ||
&timeout_task | ||
) | ||
@thread_completed = Concurrent::Event.new | ||
|
||
@value = @reason = nil | ||
@executor.post do | ||
@value = @task.call(self) | ||
rescue Exception => ex # rubocop:disable Lint/RescueException | ||
@reason = ex | ||
ensure | ||
@thread_completed.set | ||
end | ||
|
||
@thread_completed.wait | ||
|
||
if completion.try? | ||
schedule_next_task | ||
time = Time.now | ||
observers.notify_observers do | ||
[time, value, @reason] | ||
end | ||
end | ||
nil | ||
end | ||
|
||
# @!visibility private | ||
def timeout_task(completion) | ||
return unless @running.true? | ||
return unless completion.try? | ||
|
||
@executor.kill | ||
@executor.wait_for_termination | ||
@executor = Concurrent::RubySingleThreadExecutor.new | ||
|
||
@thread_completed.set | ||
|
||
schedule_next_task | ||
observers.notify_observers(Time.now, nil, Concurrent::TimeoutError.new) | ||
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
Oops, something went wrong.