-
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.
Infer interruption adapter from queue adapter
- Loading branch information
Showing
16 changed files
with
216 additions
and
146 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "interruption_adapters/null_adapter" | ||
|
||
module JobIteration | ||
module InterruptionAdapters | ||
BUNDLED_ADAPTERS = [:resque, :sidekiq].freeze # @api private | ||
|
||
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::InterruptionAdapters.register(#{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 | ||
BUNDLED_ADAPTERS.each do |name| | ||
require_relative "interruption_adapters/#{name}_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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require "resque" | ||
rescue LoadError | ||
# Resque is not available, no need to load the adapter | ||
return | ||
end | ||
|
||
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 | ||
|
||
register(:resque, ResqueAdapter) | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require "sidekiq" | ||
rescue LoadError | ||
# Sidekiq is not available, no need to load the adapter | ||
return | ||
end | ||
|
||
module JobIteration | ||
module InterruptionAdapters | ||
module SidekiqAdapter | ||
class << self | ||
attr_accessor :stopping | ||
|
||
def call | ||
stopping | ||
end | ||
end | ||
|
||
::Sidekiq.configure_server do |config| | ||
config.on(:quiet) do | ||
SidekiqAdapter.stopping = true | ||
end | ||
end | ||
end | ||
|
||
register(:sidekiq, SidekiqAdapter) | ||
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.