-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer interruption handler from a job's queue adapter
...and allow Iteration to be used with multiple job backends simultaneously Removed test_mark_job_worker_as_interrupted since it was testing stubs Co-authored-by: Justin Morris <[email protected]>
- Loading branch information
1 parent
a8422fa
commit 7f9a996
Showing
12 changed files
with
99 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
module Integrations # @private | ||
IntegrationLoadError = Class.new(StandardError) | ||
|
||
autoload :Fallback, "job-iteration/integrations/fallback" | ||
autoload :Sidekiq, "job-iteration/integrations/sidekiq" | ||
autoload :Resque, "job-iteration/integrations/resque" | ||
|
||
extend self | ||
|
||
def lookup(queue_adapter) | ||
const_get(queue_adapter.to_s.camelize) | ||
rescue NameError | ||
Fallback | ||
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
module Integrations | ||
module Fallback | ||
extend self | ||
|
||
def call | ||
false | ||
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
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,55 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "open3" | ||
|
||
class IntegrationsTest < ActiveSupport::TestCase | ||
test "will prevent loading two integrations" do | ||
with_env("ITERATION_DISABLE_AUTOCONFIGURE", nil) do | ||
rubby = <<~RUBBY | ||
require 'bundler/setup' | ||
require 'job-iteration' | ||
RUBBY | ||
_stdout, stderr, status = run_ruby(rubby) | ||
|
||
assert_equal false, status.success? | ||
assert_match(/resque integration has already been loaded, but sidekiq is also available/, stderr) | ||
|
||
class IntegrationsTest < IterationUnitTest | ||
class IterationJob < ActiveJob::Base | ||
include JobIteration::Iteration | ||
|
||
def build_enumerator(cursor:) | ||
enumerator_builder.build_once_enumerator(cursor: cursor) | ||
end | ||
end | ||
|
||
test "successfully loads one (resque) integration" do | ||
with_env("ITERATION_DISABLE_AUTOCONFIGURE", nil) do | ||
rubby = <<~RUBBY | ||
require 'bundler/setup' | ||
# Remove sidekiq, only resque will be left | ||
$LOAD_PATH.delete_if { |p| p =~ /sidekiq/ } | ||
require 'job-iteration' | ||
RUBBY | ||
_stdout, _stderr, status = run_ruby(rubby) | ||
|
||
assert_equal true, status.success? | ||
def each_iteration(*) | ||
end | ||
end | ||
|
||
private | ||
class ResqueJob < IterationJob | ||
self.queue_adapter = :resque | ||
end | ||
|
||
def run_ruby(body) | ||
stdout, stderr, status = nil | ||
Tempfile.open do |f| | ||
f.write(body) | ||
f.close | ||
class SidekiqJob < IterationJob | ||
self.queue_adapter = :sidekiq | ||
end | ||
|
||
command = "ruby #{f.path}" | ||
stdout, stderr, status = Open3.capture3(command) | ||
end | ||
[stdout, stderr, status] | ||
test "will load two integrations" do | ||
resque_job = ResqueJob.new.serialize | ||
ActiveJob::Base.execute(resque_job) | ||
|
||
sidekiq_job = SidekiqJob.new.serialize | ||
ActiveJob::Base.execute(sidekiq_job) | ||
end | ||
|
||
def with_env(variable, value) | ||
original = ENV[variable] | ||
ENV[variable] = value | ||
yield | ||
ensure | ||
ENV[variable] = original | ||
test "handles unknown Active Job queue adapater names" do | ||
interruption_adapter = JobIteration::Integrations.lookup(:unknown) | ||
assert_equal(false, interruption_adapter.call) | ||
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 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