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 24, 2024
1 parent 92237a2 commit 373033d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Main (unreleased)

- [464](https://github.com/Shopify/job-iteration/pull/464) - Add interruption adapter for [GoodJob](https://github.com/bensheldon/good_job).
- [505](https://github.com/Shopify/job-iteration/pull/505) - Add interruption adapter for [Solid Queue](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
14 changes: 8 additions & 6 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,23 +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.
addapters_to_test = JobIteration::InterruptionAdapters::BUNDLED_ADAPTERS - [:good_job]
addapters_to_test.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 373033d

Please sign in to comment.