Skip to content

Commit 06c2a49

Browse files
authored
Merge pull request rails#52990 from zzak/deprecate-sucker_punch
Turn SuckerPunch removal into deprecation and point to async adapter
2 parents bcf730e + 61cadf6 commit 06c2a49

File tree

12 files changed

+108
-4
lines changed

12 files changed

+108
-4
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ group :job do
106106
gem "resque", require: false
107107
gem "resque-scheduler", require: false
108108
gem "sidekiq", require: false
109+
gem "sucker_punch", require: false
109110
gem "delayed_job", require: false
110111
gem "queue_classic", ">= 4.0.0", require: false, platforms: :ruby
111112
gem "sneakers", require: false

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ GEM
592592
stimulus-rails (1.3.0)
593593
railties (>= 6.0.0)
594594
stringio (3.1.1)
595+
sucker_punch (3.2.0)
596+
concurrent-ruby (~> 1.0)
595597
syntax_tree (6.1.1)
596598
prettier_print (>= 1.2.0)
597599
tailwindcss-rails (2.1.0)
@@ -720,6 +722,7 @@ DEPENDENCIES
720722
sqlite3 (>= 2.1)
721723
stackprof
722724
stimulus-rails
725+
sucker_punch
723726
syntax_tree (= 6.1.1)
724727
tailwindcss-rails
725728
terser (>= 1.1.4)

activejob/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
* Remove `sucker_punch` as an adapter option [since author himself recommends using AJ's own AsyncAdapter](https://github.com/brandonhilkert/sucker_punch?tab=readme-ov-file#faq).
1+
* Deprecate `sucker_punch` as an adapter option.
2+
23
If you're using this adapter, change to `adapter: async` for the same functionality.
34

4-
*Dino Maric*
5+
*Dino Maric, zzak*
56

67
* Use `RAILS_MAX_THREADS` in `ActiveJob::AsyncAdapter`. If it is not set, use 5 as default.
78

activejob/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "rake/testtask"
44

5-
ACTIVEJOB_ADAPTERS = %w(async inline delayed_job queue_classic resque sidekiq sneakers backburner test)
5+
ACTIVEJOB_ADAPTERS = %w(async inline delayed_job queue_classic resque sidekiq sneakers sucker_punch backburner test)
66
ACTIVEJOB_ADAPTERS.delete("queue_classic") if defined?(JRUBY_VERSION)
77

88
task default: :test

activejob/lib/active_job/queue_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def queue_adapter=(name_or_adapter)
5050
case name_or_adapter
5151
when Symbol, String
5252
queue_adapter = ActiveJob::QueueAdapters.lookup(name_or_adapter).new
53+
queue_adapter.try(:check_adapter)
5354
assign_adapter(name_or_adapter.to_s, queue_adapter)
5455
else
5556
if queue_adapter?(name_or_adapter)

activejob/lib/active_job/queue_adapters.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ module QueueAdapters
121121
autoload :ResqueAdapter
122122
autoload :SidekiqAdapter
123123
autoload :SneakersAdapter
124+
autoload :SuckerPunchAdapter
124125
autoload :TestAdapter
125126

126127
ADAPTER = "Adapter"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
require "sucker_punch"
4+
5+
module ActiveJob
6+
module QueueAdapters
7+
# = Sucker Punch adapter for Active Job
8+
#
9+
# Sucker Punch is a single-process Ruby asynchronous processing library.
10+
# This reduces the cost of hosting on a service like Heroku along
11+
# with the memory footprint of having to maintain additional jobs if
12+
# hosting on a dedicated server. All queues can run within a
13+
# single application (e.g. \Rails, Sinatra, etc.) process.
14+
#
15+
# Read more about Sucker Punch {here}[https://github.com/brandonhilkert/sucker_punch].
16+
#
17+
# To use Sucker Punch set the queue_adapter config to +:sucker_punch+.
18+
#
19+
# Rails.application.config.active_job.queue_adapter = :sucker_punch
20+
class SuckerPunchAdapter < AbstractAdapter
21+
def check_adapter
22+
ActiveJob.deprecator.warn <<~MSG.squish
23+
The `sucker_punch` adapter is deprecated and will be removed in Rails 8.1.
24+
Please use the `async` adapter instead.
25+
MSG
26+
end
27+
28+
def enqueue(job) # :nodoc:
29+
if JobWrapper.respond_to?(:perform_async)
30+
# sucker_punch 2.0 API
31+
JobWrapper.perform_async job.serialize
32+
else
33+
# sucker_punch 1.0 API
34+
JobWrapper.new.async.perform job.serialize
35+
end
36+
end
37+
38+
def enqueue_at(job, timestamp) # :nodoc:
39+
if JobWrapper.respond_to?(:perform_in)
40+
delay = timestamp - Time.current.to_f
41+
JobWrapper.perform_in delay, job.serialize
42+
else
43+
raise NotImplementedError, "sucker_punch 1.0 does not support `enqueue_at`. Please upgrade to version ~> 2.0.0 to enable this behavior."
44+
end
45+
end
46+
47+
class JobWrapper # :nodoc:
48+
include SuckerPunch::Job
49+
50+
def perform(job_data)
51+
Base.execute job_data
52+
end
53+
end
54+
end
55+
end
56+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require "sucker_punch/testing/inline"
4+
ActiveJob::Base.queue_adapter = :sucker_punch

activejob/test/cases/adapter_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,31 @@ class AdapterTest < ActiveSupport::TestCase
66
test "should load #{ENV['AJ_ADAPTER']} adapter" do
77
assert_equal "active_job/queue_adapters/#{ENV['AJ_ADAPTER']}_adapter".classify, ActiveJob::Base.queue_adapter.class.name
88
end
9+
10+
if adapter_is?(:sucker_punch)
11+
test "sucker_punch adapter should be deprecated" do
12+
before_adapter = ActiveJob::Base.queue_adapter
13+
14+
msg = <<~MSG.squish
15+
The `sucker_punch` adapter is deprecated and will be removed in Rails 8.1.
16+
Please use the `async` adapter instead.
17+
MSG
18+
assert_deprecated(msg, ActiveJob.deprecator) do
19+
ActiveJob::Base.queue_adapter = :sucker_punch
20+
end
21+
22+
ensure
23+
ActiveJob::Base.queue_adapter = before_adapter
24+
end
25+
26+
test "sucker_punch check_adapter should warn" do
27+
msg = <<~MSG.squish
28+
The `sucker_punch` adapter is deprecated and will be removed in Rails 8.1.
29+
Please use the `async` adapter instead.
30+
MSG
31+
assert_deprecated(msg, ActiveJob.deprecator) do
32+
ActiveJob::Base.queue_adapter.check_adapter
33+
end
34+
end
35+
end
936
end

activejob/test/cases/test_case_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def test_set_test_adapter
4141
ActiveJob::QueueAdapters::SidekiqAdapter
4242
when :sneakers
4343
ActiveJob::QueueAdapters::SneakersAdapter
44+
when :sucker_punch
45+
ActiveJob::QueueAdapters::SuckerPunchAdapter
4446
else
4547
raise NotImplementedError.new
4648
end

0 commit comments

Comments
 (0)