1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Merge pull request #2457 from mperham/rails5

Enable code reloading in development mode with Rails 5
This commit is contained in:
Mike Perham 2016-08-24 09:55:44 -07:00 committed by GitHub
commit be3073afc3
9 changed files with 70 additions and 53 deletions

View file

@ -7,12 +7,6 @@ before_install:
- gem install bundler - gem install bundler
- gem update bundler - gem update bundler
rvm: rvm:
- 2.0.0
- 2.1.8
- 2.2.4 - 2.2.4
- 2.3.0 - 2.3.0
- jruby-head - jruby-head
- rbx-2
matrix:
allow_failures:
- rvm: rbx-2

View file

@ -3,14 +3,13 @@
4.2.0 4.2.0
----------- -----------
- Remove Sinatra dependency. Sidekiq's Web UI now uses Rack directly. - Enable development-mode code reloading. **With Rails 5.0+, you don't need
to restart Sidekiq to pick up your Sidekiq::Worker changes anymore!** [#2457]
- **Remove Sinatra dependency**. Sidekiq's Web UI now uses Rack directly.
Thank you to Sidekiq's newest committer, **badosu**, for writing the code Thank you to Sidekiq's newest committer, **badosu**, for writing the code
and doing a lot of testing to ensure compatibility with many different and doing a lot of testing to ensure compatibility with many different
3rd party plugins. If your Web UI works with 4.1.4 but fails with 3rd party plugins. If your Web UI works with 4.1.4 but fails with
4.2.0, please open an issue. [#3075] 4.2.0, please open an issue. [#3075]
- Add support for development mode code reloading with Rails 5's new
thread-safe Interlock API. With Rails 5, you no longer need to
restart Sidekiq when making code changes locally! [#2457]
- Allow tuning of concurrency with the `RAILS_MAX_THREADS` env var. [#2985] - Allow tuning of concurrency with the `RAILS_MAX_THREADS` env var. [#2985]
This is the same var used by Puma so you can tune all of your systems This is the same var used by Puma so you can tune all of your systems
the same way: the same way:

10
Gemfile
View file

@ -1,8 +1,8 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gemspec gemspec
gem 'rails', '5.0.0'
gem "hiredis" gem "hiredis"
gem 'rails', '~> 4.2'
gem 'simplecov' gem 'simplecov'
gem 'minitest' gem 'minitest'
gem 'minitest-utils' gem 'minitest-utils'
@ -23,7 +23,7 @@ platforms :mri do
gem 'ruby-prof' gem 'ruby-prof'
end end
platforms :jruby do #platforms :jruby do
gem 'jruby-openssl' #gem 'jruby-openssl'
gem 'activerecord-jdbcsqlite3-adapter' #gem 'activerecord-jdbcsqlite3-adapter'
end #end

View file

@ -31,7 +31,8 @@ module Sidekiq
heartbeat: [], heartbeat: [],
}, },
dead_max_jobs: 10_000, dead_max_jobs: 10_000,
dead_timeout_in_seconds: 180 * 24 * 60 * 60 # 6 months dead_timeout_in_seconds: 180 * 24 * 60 * 60, # 6 months
reloader: proc { |&block| block.call },
} }
DEFAULT_WORKER_OPTIONS = { DEFAULT_WORKER_OPTIONS = {

View file

@ -229,7 +229,7 @@ module Sidekiq
require 'sidekiq/rails' require 'sidekiq/rails'
require File.expand_path("#{options[:require]}/config/environment.rb") require File.expand_path("#{options[:require]}/config/environment.rb")
::Rails.application.eager_load! ::Rails.application.eager_load!
else elsif ::Rails::VERSION::MAJOR == 4
# Painful contortions, see 1791 for discussion # Painful contortions, see 1791 for discussion
require File.expand_path("#{options[:require]}/config/application.rb") require File.expand_path("#{options[:require]}/config/application.rb")
::Rails::Application.initializer "sidekiq.eager_load" do ::Rails::Application.initializer "sidekiq.eager_load" do
@ -237,6 +237,10 @@ module Sidekiq
end end
require 'sidekiq/rails' require 'sidekiq/rails'
require File.expand_path("#{options[:require]}/config/environment.rb") require File.expand_path("#{options[:require]}/config/environment.rb")
else
require 'sidekiq/rails'
require File.expand_path("#{options[:require]}/config/environment.rb")
Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
end end
options[:tag] ||= default_tag options[:tag] ||= default_tag
else else

View file

@ -36,6 +36,7 @@ module Sidekiq
@job = nil @job = nil
@thread = nil @thread = nil
@strategy = (mgr.options[:fetch] || Sidekiq::BasicFetch).new(mgr.options) @strategy = (mgr.options[:fetch] || Sidekiq::BasicFetch).new(mgr.options)
@reloader = Sidekiq.options[:reloader]
end end
def terminate(wait=false) def terminate(wait=false)
@ -118,33 +119,35 @@ module Sidekiq
jobstr = work.job jobstr = work.job
queue = work.queue_name queue = work.queue_name
ack = false @reloader.call do
begin
job = Sidekiq.load_json(jobstr)
klass = job['class'.freeze].constantize
worker = klass.new
worker.jid = job['jid'.freeze]
stats(worker, job, queue) do
Sidekiq.server_middleware.invoke(worker, job, queue) do
# Only ack if we either attempted to start this job or
# successfully completed it. This prevents us from
# losing jobs if a middleware raises an exception before yielding
ack = true
execute_job(worker, cloned(job['args'.freeze]))
end
end
ack = true
rescue Sidekiq::Shutdown
# Had to force kill this job because it didn't finish
# within the timeout. Don't acknowledge the work since
# we didn't properly finish it.
ack = false ack = false
rescue Exception => ex begin
handle_exception(ex, job || { :job => jobstr }) job = Sidekiq.load_json(jobstr)
raise klass = job['class'.freeze].constantize
ensure worker = klass.new
work.acknowledge if ack worker.jid = job['jid'.freeze]
stats(worker, job, queue) do
Sidekiq.server_middleware.invoke(worker, job, queue) do
# Only ack if we either attempted to start this job or
# successfully completed it. This prevents us from
# losing jobs if a middleware raises an exception before yielding
ack = true
execute_job(worker, cloned(job['args'.freeze]))
end
end
ack = true
rescue Sidekiq::Shutdown
# Had to force kill this job because it didn't finish
# within the timeout. Don't acknowledge the work since
# we didn't properly finish it.
ack = false
rescue Exception => ex
handle_exception(ex, job || { :job => jobstr })
raise
ensure
work.acknowledge if ack
end
end end
end end

View file

@ -35,5 +35,22 @@ module Sidekiq
initializer 'sidekiq' do initializer 'sidekiq' do
Sidekiq.hook_rails! Sidekiq.hook_rails!
end end
class Reloader
def initialize(app = ::Rails.application)
Sidekiq.logger.debug "Enabling Rails 5+ live code reloading, so hot!" unless app.config.cache_classes
@app = app
end
def call
@app.reloader.wrap do
yield
end
end
def inspect
"#<Sidekiq::Rails::Reloader @app=#{@app.class.name}>"
end
end
end if defined?(::Rails) end if defined?(::Rails)
end end

View file

@ -1,21 +1,20 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'pry'
gem 'sidekiq', :path => '..'
gem 'rails', '5.0.0'
platforms :ruby do platforms :ruby do
gem 'sqlite3' gem 'sqlite3'
gem 'redis-namespace' gem 'redis-namespace'
end end
platforms :jruby do #platforms :jruby do
gem 'jruby-openssl' #gem 'jruby-openssl'
gem 'activerecord-jdbcsqlite3-adapter' #gem 'activerecord-jdbcsqlite3-adapter'
end #end
gem 'rails', '~> 4.2'
gem 'sidekiq', :path => '..'
#gem 'ruby-prof' #gem 'ruby-prof'
#de Does not work with jruby or rbx: #de Does not work with jruby or rbx:
#de gem 'pry-byebug' #de gem 'pry-byebug'
# sidekiq-web dependencies
gem 'rack-protection'

View file

@ -22,5 +22,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'redis-namespace', '~> 1.5', '>= 1.5.2' gem.add_development_dependency 'redis-namespace', '~> 1.5', '>= 1.5.2'
gem.add_development_dependency 'minitest', '~> 5.7', '>= 5.7.0' gem.add_development_dependency 'minitest', '~> 5.7', '>= 5.7.0'
gem.add_development_dependency 'rake', '~> 10.0' gem.add_development_dependency 'rake', '~> 10.0'
gem.add_development_dependency 'rails', '~> 4', '>= 3.2.0' gem.add_development_dependency 'rails', '>= 3.2.0'
end end