Skip to content

Commit

Permalink
Add interruption adapter for Solid Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mangara committed Sep 23, 2024
1 parent 3fc1ba0 commit 181d490
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Main (unreleased)
Nil

- [???](???) Add interruption adapter for [SolidQueue](https://github.com/rails/solid_queue).

## v1.5.1 (May 29,2024)
- [483](https://github.com/Shopify/job-iteration/pull/483) Reverts [#456 Use Arel instead of String for AR Enumerator conditionals](https://github.com/Shopify/job-iteration/pull/456)
Expand Down
2 changes: 1 addition & 1 deletion lib/job-iteration/interruption_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module JobIteration
module InterruptionAdapters
BUNDLED_ADAPTERS = [:good_job, :resque, :sidekiq].freeze # @api private
BUNDLED_ADAPTERS = [:good_job, :resque, :sidekiq, :solid_queue].freeze # @api private

class << self
# Returns adapter for specified name.
Expand Down
36 changes: 36 additions & 0 deletions lib/job-iteration/interruption_adapters/solid_queue_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

begin
require "solid_queue"
rescue LoadError
# SolidQueue is not available, no need to load the adapter
return
end

begin
# SolidQueue.on_worker_stop was introduced in SolidQueue 0.7.1
gem("solid_queue", ">= 0.7.1")
rescue Gem::LoadError
warn("job-iteration's interruption adapter for SolidQueue requires SolidQueue 0.7.1 or newer")
return
end

module JobIteration
module InterruptionAdapters
module SolidQueueAdapter
class << self
attr_accessor :stopping

def call
stopping
end
end

SolidQueue.on_worker_stop do
SolidQueueAdapter.stopping = true
end
end

register(:solid_queue, SolidQueueAdapter)
end
end
13 changes: 8 additions & 5 deletions test/integration/interruption_adapters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class InterruptionAdaptersTest < ActiveSupport::TestCase
RUBY
_stdout, stderr, status = run_ruby(ruby)

assert_predicate(status, :success?)
assert_predicate(status, :success?, "Errors: #{stderr}")
refute_match(/No interruption adapter is registered for :resque/, stderr)
end

Expand All @@ -28,22 +28,25 @@ class InterruptionAdaptersTest < ActiveSupport::TestCase
RUBY
_stdout, stderr, status = run_ruby(ruby)

assert_predicate(status, :success?)
assert_predicate(status, :success?, "Errors: #{stderr}")
assert_match(/No interruption adapter is registered for :sidekiq/, stderr)
end

test "loads all available interruption adapters" do
ruby = <<~RUBY
require 'bundler/setup'
require 'job-iteration'
# The adapter for GoodJob cannot be easily tested at the moment.
JobIteration::InterruptionAdapters::BUNDLED_ADAPTERS.excluding(:good_job).each do |name|
adapters_to_exclude = [:good_job, :solid_queue] # These require a Rails app to be loaded
adapters_to_test = JobIteration::InterruptionAdapters::BUNDLED_ADAPTERS - adapters_to_exclude
adapters_to_test.each do |name|
JobIteration::InterruptionAdapters.lookup(name)
end
RUBY
_stdout, stderr, status = run_ruby(ruby)

assert_predicate(status, :success?)
assert_predicate(status, :success?, "Errors: #{stderr}")
refute_match(/No interruption adapter is registered for/, stderr)
end

Expand Down

0 comments on commit 181d490

Please sign in to comment.