Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable hot-reload via adding ActiveSupport::Reloader delegate #756

Merged
merged 6 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/shoryuken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def self.healthy?
:on,
:cache_visibility_timeout?,
:cache_visibility_timeout=,
:reloader,
:reloader=,
:enable_reloading,
:enable_reloading=,
:delay
)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/shoryuken/environment_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def initialize_rails
::Rails.application.config.eager_load = true
end
end
::Rails::Application.initializer 'shoryuken.set_reloader_hook' do |app|
Shoryuken.reloader = proc do |&block|
app.reloader.wrap do
block.call
end
end
end
if Shoryuken.active_job?
require 'shoryuken/extensions/active_job_extensions'
require 'shoryuken/extensions/active_job_adapter'
Expand Down
4 changes: 3 additions & 1 deletion lib/shoryuken/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Options
}.freeze

attr_accessor :active_job_queue_name_prefixing, :cache_visibility_timeout, :groups,
:launcher_executor,
:launcher_executor, :reloader, :enable_reloading,
:start_callback, :stop_callback, :worker_executor, :worker_registry, :exception_handlers
attr_writer :default_worker_options, :sqs_client
attr_reader :sqs_client_receive_message_opts
Expand All @@ -29,6 +29,8 @@ def initialize
self.active_job_queue_name_prefixing = false
self.worker_executor = Worker::DefaultExecutor
self.cache_visibility_timeout = false
self.reloader = proc { |&block| block.call }
self.enable_reloading ||= false
# this is needed for keeping backward compatibility
@sqs_client_receive_message_opts ||= {}
end
Expand Down
17 changes: 13 additions & 4 deletions lib/shoryuken/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ def initialize(queue, sqs_msg)
end

def process
return logger.error { "No worker found for #{queue}" } unless worker
worker_perform = proc do
return logger.error { "No worker found for #{queue}" } unless worker
Shoryuken::Logging.with_context("#{worker_name(worker.class, sqs_msg, body)}/#{queue}/#{sqs_msg.message_id}") do
worker.class.server_middleware.invoke(worker, queue, sqs_msg, body) do
worker.perform(sqs_msg, body)
end
end
end

Shoryuken::Logging.with_context("#{worker_name(worker.class, sqs_msg, body)}/#{queue}/#{sqs_msg.message_id}") do
worker.class.server_middleware.invoke(worker, queue, sqs_msg, body) do
worker.perform(sqs_msg, body)
if Shoryuken.enable_reloading
Shoryuken.reloader.call do
worker_perform.call
end
else
worker_perform.call
end
rescue Exception => ex
Array(Shoryuken.exception_handlers).each { |handler| handler.call(ex, queue, sqs_msg) }
Expand Down
2 changes: 1 addition & 1 deletion lib/shoryuken/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Shoryuken
VERSION = '6.1.2'.freeze
VERSION = '6.2.0'.freeze
end
38 changes: 38 additions & 0 deletions spec/shoryuken/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,43 @@ def perform(sqs_msg, body); end
subject.process
end
end

context 'when specifying a reloader' do
before do
Shoryuken.reloader = proc do |_|
TestWorker.new.called
end
end

after do
Shoryuken.reloader = proc { |&block| block.call }
end

context 'when reloader is enabled' do
before do
Shoryuken.enable_reloading = true
end

it 'wraps execution in reloader' do
expect_any_instance_of(TestWorker).to receive(:called)
expect_any_instance_of(TestWorker).to_not receive(:perform)

subject.process
end

after do
Shoryuken.enable_reloading = false
end
end

context 'when reloader is disabled' do
it 'does not wrap execution in reloader' do
expect_any_instance_of(TestWorker).to_not receive(:called)
expect_any_instance_of(TestWorker).to receive(:perform).with(sqs_msg, sqs_msg.body)

subject.process
end
end
end
end
end
Loading