diff --git a/README.md b/README.md index c264110..de5a8b8 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,9 @@ The above will start five workers in total: * one listening on the `search_index, cache_warming` queue * three listening on the `mailing` queue -### Multiple Servers/Roles +This can be useful for customizing Resque tasks in complex server environments. + +### Multiple Servers/roles You can also start up workers on multiple servers/roles: @@ -89,6 +91,19 @@ The above will start four workers in total: * one `search_index` on Server B * one `search_index` on Server C +### Env Dynamic Schedule +To set env variable `DYNAMIC_SCHEDULE when` start scheduler use: + +```ruby +set :resque_dynamic_schedule, true +``` + +When set this env, the command to start scheduler would + +```bash +DYNAMIC_SCHEDULE=true bundle exec rake resque:scheduler +``` + ### Rails Environment With Rails, Resque requires loading the Rails environment task to have access to your models, etc. (e.g. `QUEUE=* rake environment resque:work`). However, Resque is often used without Rails (and even if you are using Rails, you may not need/want to load the Rails environment). As such, the `environment` task is not automatically included. diff --git a/lib/capistrano-resque/capistrano_integration.rb b/lib/capistrano-resque/capistrano_integration.rb index 15ecad3..359a40d 100644 --- a/lib/capistrano-resque/capistrano_integration.rb +++ b/lib/capistrano-resque/capistrano_integration.rb @@ -9,6 +9,7 @@ def self.load_into(capistrano_config) _cset(:workers, {"*" => 1}) _cset(:resque_kill_signal, "QUIT") _cset(:interval, "5") + _cset(:resque_extra_env, "") _cset(:resque_environment_task, false) _cset(:resque_log_file, "/dev/null") _cset(:resque_verbose, true) @@ -47,10 +48,10 @@ def status_command def start_command(queue, pid) "cd #{current_path} && RACK_ENV=#{rails_env} RAILS_ENV=#{rails_env} QUEUE=\"#{queue}\" \ - PIDFILE=#{pid} BACKGROUND=yes \ + PIDFILE=#{pid} BACKGROUND=yes\ #{"VERBOSE=1 " if fetch(:resque_verbose)}\ - INTERVAL=#{interval} \ - nohup #{fetch(:bundle_cmd, "bundle")} exec rake \ + INTERVAL=#{interval} #{resque_extra_env}\ + #{fetch(:nohup_cmd, "")} #{fetch(:bundle_cmd, "bundle")} exec rake \ #{"environment " if fetch(:resque_environment_task)}\ resque:work #{output_redirection}" end @@ -77,12 +78,12 @@ def status_scheduler end def start_scheduler(pid) - "cd #{current_path} && RACK_ENV=#{rails_env} RAILS_ENV=#{rails_env} \ + "cd #{current_path} && RACK_ENV=#{rails_env} #{resque_extra_env} RAILS_ENV=#{rails_env} \ PIDFILE=#{pid} BACKGROUND=yes \ #{"VERBOSE=1 " if fetch(:resque_verbose)}\ MUTE=1 \ #{"DYNAMIC_SCHEDULE=yes " if fetch(:resque_dynamic_schedule)}\ - nohup #{fetch(:bundle_cmd, "bundle")} exec rake \ + #{fetch(:nohup_cmd, "")} #{fetch(:bundle_cmd, "bundle")} exec rake \ #{"environment " if fetch(:resque_environment_task)}\ resque:scheduler #{output_redirection}" end diff --git a/lib/capistrano-resque/tasks/capistrano-resque.rake b/lib/capistrano-resque/tasks/capistrano-resque.rake index b1f6490..b0c58d4 100644 --- a/lib/capistrano-resque/tasks/capistrano-resque.rake +++ b/lib/capistrano-resque/tasks/capistrano-resque.rake @@ -1,6 +1,7 @@ namespace :load do task :defaults do set :workers, {"*" => 1} + set :resque_extra_env, "" set :resque_kill_signal, "QUIT" set :interval, "5" set :resque_environment_task, false @@ -70,7 +71,7 @@ namespace :resque do number_of_workers.times do pid = "#{fetch(:resque_pid_path)}/resque_work_#{worker_id}.pid" within current_path do - execute :nohup, %{#{SSHKit.config.command_map[:rake]} RACK_ENV=#{rails_env} RAILS_ENV=#{rails_env} QUEUE="#{queue}" PIDFILE=#{pid} BACKGROUND=yes #{"VERBOSE=1 " if fetch(:resque_verbose)}INTERVAL=#{fetch(:interval)} #{"environment " if fetch(:resque_environment_task)}resque:work #{output_redirection}} + execute %{cd #{release_path} && #{SSHKit.config.command_map[:rake]} #{fetch(:resque_extra_env)} RACK_ENV=#{rails_env} RAILS_ENV=#{rails_env} QUEUE="#{queue}" PIDFILE=#{pid} BACKGROUND=yes #{"VERBOSE=1 " if fetch(:resque_verbose)}INTERVAL=#{fetch(:interval)} #{"environment " if fetch(:resque_environment_task)}resque:work #{output_redirection}} end worker_id += 1 end @@ -130,7 +131,7 @@ namespace :resque do create_pid_path pid = "#{fetch(:resque_pid_path)}/scheduler.pid" within current_path do - execute :nohup, %{#{SSHKit.config.command_map[:rake]} RACK_ENV=#{rails_env} RAILS_ENV=#{rails_env} PIDFILE=#{pid} BACKGROUND=yes #{"VERBOSE=1 " if fetch(:resque_verbose)}MUTE=1 #{"DYNAMIC_SCHEDULE=yes " if fetch(:resque_dynamic_schedule)}#{"environment " if fetch(:resque_environment_task)}resque:scheduler #{output_redirection}} + execute %{cd #{release_path} && #{SSHKit.config.command_map[:rake]} #{fetch(:resque_extra_env)} RACK_ENV=#{rails_env} RAILS_ENV=#{rails_env} PIDFILE=#{pid} BACKGROUND=yes #{"VERBOSE=1 " if fetch(:resque_verbose)}MUTE=1 #{"DYNAMIC_SCHEDULE=yes " if fetch(:resque_dynamic_schedule)}#{"environment " if fetch(:resque_environment_task)}resque:scheduler #{output_redirection}} end end end