-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
146 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "interruption_adapters/null_adapter" | ||
|
||
module JobIteration | ||
module InterruptionAdapters | ||
class << self | ||
# Returns adapter for specified name. | ||
# | ||
# JobIteration::InterruptionAdapters.lookup(:sidekiq) | ||
# # => JobIteration::InterruptionAdapters::SidekiqAdapter | ||
def lookup(name) | ||
registry.fetch(name.to_sym) do | ||
Deprecation.warn(<<~DEPRECATION_MESSAGE, caller_locations(1)) | ||
No interruption adapter is registered for #{name.inspect}; falling back to `NullAdapter`, which never interrupts. | ||
Use `JobIteration.register_queue_adapter(#{name.to_sym.inspect}, <adapter>) to register one. | ||
This will raise starting in version #{Deprecation.deprecation_horizon} of #{Deprecation.gem_name}!" | ||
DEPRECATION_MESSAGE | ||
|
||
NullAdapter | ||
end | ||
end | ||
|
||
# Registers adapter for specified name. | ||
# | ||
# JobIteration::InterruptionAdapters.register(:sidekiq, MyCustomSidekiqAdapter) | ||
def register(name, adapter) | ||
raise ArgumentError, "adapter must be callable" unless adapter.respond_to?(:call) | ||
|
||
registry[name.to_sym] = adapter | ||
end | ||
|
||
private | ||
|
||
attr_reader :registry | ||
end | ||
|
||
@registry = {} | ||
|
||
# Built-in Rails adapters. It doesn't make sense to interrupt for these. | ||
register(:async, NullAdapter) | ||
register(:inline, NullAdapter) | ||
register(:test, NullAdapter) | ||
|
||
# External adapters | ||
begin | ||
require "resque" | ||
require_relative "interruption_adapters/resque_adapter" | ||
register(:resque, ResqueAdapter) | ||
rescue LoadError | ||
# Resque is not available, no need to load the adapter | ||
end | ||
|
||
begin | ||
require "sidekiq" | ||
require_relative "interruption_adapters/sidekiq_adapter" | ||
register(:sidekiq, SidekiqAdapter) | ||
rescue LoadError | ||
# Sidekiq is not available, no need to load the adapter | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# This adapter never interrupts. | ||
module JobIteration | ||
module InterruptionAdapters | ||
module NullAdapter | ||
class << self | ||
def call | ||
false | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
module InterruptionAdapters | ||
module ResqueAdapter | ||
# @private | ||
module IterationExtension | ||
def initialize(*) | ||
$resque_worker = self # rubocop:disable Style/GlobalVars | ||
super | ||
end | ||
end | ||
|
||
# @private | ||
module ::Resque | ||
class Worker | ||
# The patch is required in order to call shutdown? on a Resque::Worker instance | ||
prepend(IterationExtension) | ||
end | ||
end | ||
|
||
class << self | ||
def call | ||
$resque_worker.try!(:shutdown?) # rubocop:disable Style/GlobalVars | ||
end | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/job-iteration/interruption_adapters/sidekiq_adapter.rb
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
module InterruptionAdapters | ||
module SidekiqAdapter | ||
class << self | ||
def call | ||
if defined?(Sidekiq::CLI) && Sidekiq::CLI.instance | ||
Sidekiq::CLI.instance.launcher.stopping? | ||
else | ||
false | ||
end | ||
end | ||
end | ||
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
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