Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restructure how the options are handled and passed to resque

This includes adding the :vverbose option to provide
access to the standard VERBOSE resque option, instead of
all-or-nothing.
  • Loading branch information...
commit 053684f05fb66574b20ed19cfb1ab1ca2bff610f 1 parent 9626ae1
@rtlong rtlong authored
View
21 README.markdown
@@ -32,18 +32,19 @@ being active.
guard 'resque', :environment => 'development' do
watch(%r{^app/(.+)\.rb})
end
-
+
## Options
You can customize the resque task via the following options:
-* `environment`: the rails environment to run the workers in (defaults to `"development"`)
+* `environment`: the rails environment to run the workers in (defaults to `nil`)
* `task`: the name of the rake task to use (defaults to `"resque:work"`)
* `queue`: the resque queue to run (defaults to `"*"`)
-* `count`: the number of workers to include (defaults to `"1"`)
-* `verbose`: whether to use verbose logging (defaults to `false`)
-* `trace`: whether to include `--trace` on the rake command (defaults to `false`)
-* `stop_signal`: how to kill the process when restarting (defaults to `KILL`)
+* `count`: the number of workers to include (defaults to `1`)
+* `verbose`: whether to use verbose logging (defaults to `nil`)
+* `vverbose`: whether to use very verbose logging (defaults to `nil`)
+* `trace`: whether to include `--trace` on the rake command (defaults to `nil`)
+* `stop_signal`: how to kill the process when restarting (defaults to `QUIT`)
## Development
@@ -63,13 +64,13 @@ you make.
* Update the version number in `lib/guard/resque/version.rb`
* Update `CHANGELOG.md`
* Build the gem:
-
+
gem build guard-resque.gemspec
-
+
* Push to rubygems.org:
-
+
gem push guard-resque-0.x.x.gem
-
+
## Guard::Delayed Authors
View
62 lib/guard/resque.rb
@@ -4,26 +4,38 @@
module Guard
class Resque < Guard
+ DEFAULT_SIGNAL = :QUIT
+ DEFAULT_QUEUE = '*'.freeze
+ DEFAULT_COUNT = 1
+ DEFAULT_TASK_SINGLE = 'resque:work'.freeze
+ DEFAULT_TASK_MULTIPLE = 'resque:workers'.freeze
+
# Allowable options are:
- # :environment e.g. 'test'
- # :task .e.g 'resque:work'
- # :queue e.g. '*'
- # :count e.g. 3
- # :verbose e.g. true
- # :trace e.g. true
- # :stop_signal e.g. QUIT
+ # - :environment e.g. 'test'
+ # - :task .e.g 'resque:work'
+ # - :queue e.g. '*'
+ # - :count e.g. 3
+ # - :verbose e.g. true
+ # - :vverbose e.g. true
+ # - :trace e.g. true
+ # - :stop_signal e.g. :QUIT or :SIGQUIT
def initialize(watchers = [], options = {})
@options = options
@pid = nil
- @stop_signal = options[:stop_signal] || "QUIT"
+ @stop_signal = options[:stop_signal] || DEFAULT_SIGNAL
+ @options[:queue] ||= DEFAULT_QUEUE
+ @options[:count] ||= DEFAULT_COUNT
+ @options[:task] ||= (@options[:count].to_i == 1) ? DEFAULT_TASK_SINGLE : DEFAULT_TASK_MULTIPLE
super
end
def start
stop
- UI.info "Starting up resque..."
- UI.info cmd
- @pid = spawn(cmd)
+ UI.info 'Starting up resque...'
+ UI.info [ cmd, env.map{|v| v.join('=')} ].join(' ')
+
+ # launch Resque worker
+ @pid = spawn(env, cmd)
end
# Called on Ctrl-C signal (when Guard quits)
@@ -63,27 +75,25 @@ def restart
private
def cmd
- command = "bundle exec rake "
-
- # environment setting
- command << "RAILS_ENV=#{@options[:environment]} " if @options[:environment]
+ command = ['bundle exec rake', @options[:task].to_s]
- # queue setting
- command << "QUEUE=#{@options[:queue] || '*'} "
+ # trace setting
+ command << '--trace' if @options[:trace]
- #count setting
- command << "COUNT=#{@options[:count] || '1'} "
+ return command.join(' ')
+ end
- # verbose setting
- command << "VVERBOSE=1 " if @options[:verbose]
+ def env
+ var = Hash.new
- # task setting
- command << "#{@options[:task] || 'resque:work'} "
+ var['QUEUE'] = @options[:queue].to_s if @options[:queue]
+ var['COUNT'] = @options[:count].to_s if @options[:count]
+ var['RAILS_ENV'] = @options[:environment].to_s if @options[:environment]
- # trace setting
- command << "--trace " if @options[:trace]
+ var['VERBOSE'] = '1' if @options[:verbose]
+ var['VVERBOSE'] = '1' if @options[:vverbose]
- command
+ return var
end
end
end
View
18 lib/guard/resque/templates/Guardfile
@@ -1,10 +1,12 @@
-# other available options
-# :task (defaults to resque:work)
-# :verbose
-# :trace
-# :queue (defaults to "*")
-# :count (defaults to 1)
+### Guard::Resque
+# available options:
+# - :task (defaults to 'resque:work' if :count is 1; 'resque:workers', otherwise)
+# - :verbose / :vverbose (set them to anything but false to activate their respective modes)
+# - :trace
+# - :queue (defaults to "*")
+# - :count (defaults to 1)
+# - :environment (corresponds to RAILS_ENV for the Resque worker)
guard 'resque', :environment => 'development' do
- watch(%r{^app/(.+)\.rb})
- watch(%r{^lib/(.+)\.rb})
+ watch(%r{^app/(.+)\.rb$})
+ watch(%r{^lib/(.+)\.rb$})
end
View
72 spec/guard/resque_spec.rb
@@ -1,51 +1,67 @@
require 'spec_helper'
describe Guard::Resque do
- subject { Guard::Resque.new }
+ describe 'start' do
- describe "start" do
- before do
- subject = Guard::Resque.new
- end
+ it 'should accept :environment option' do
+ environment = :foo
- after do
- subject.stop
+ obj = Guard::Resque.new [], :environment => environment
+ obj.send(:env).should include 'RAILS_ENV' => environment.to_s
end
- it "should create a pid in /tmp/pids" do
- subject.start
- Dir.glob("tmp/pids/resque*.pid").should_not be_empty
- end
+ it 'should accept :queue option' do
+ queue = :foo
- it "should accept environment option" do
- obj = Guard::Resque.new [], :environment => 'foo'
- obj.send(:cmd).should match /RAILS_ENV=foo/
+ obj = Guard::Resque.new [], :queue => queue
+ obj.send(:env).should include 'QUEUE' => queue.to_s
end
- it "should accept queue option" do
- obj = Guard::Resque.new [], :queue => 'foo'
- obj.send(:cmd).should match /QUEUE=foo/
+ it 'should accept :count option' do
+ count = 2
+
+ obj = Guard::Resque.new [], :count => count
+ obj.send(:env).should include 'COUNT' => count.to_s
end
- it "should accept count option" do
- obj = Guard::Resque.new [], :count => 2
- obj.send(:cmd).should match /COUNT=2/
+ it 'should accept :vverbose option' do
+ obj = Guard::Resque.new [], :vverbose => true
+ obj.send(:env).should include 'VVERBOSE'
end
- it "should accept verbose option" do
+ it 'should accept :verbose option' do
obj = Guard::Resque.new [], :verbose => true
- obj.send(:cmd).should match /VVERBOSE=1/
+ obj.send(:env).should include 'VERBOSE'
end
- it "should accept trace option" do
+ it 'should accept :trace option' do
obj = Guard::Resque.new [], :trace => true
- obj.send(:cmd).should match /--trace/
+ obj.send(:cmd).should include '--trace'
+ end
+
+ it 'should accept :task option' do
+ task = 'environment foo'
+
+ obj = Guard::Resque.new [], :task => task
+ obj.send(:cmd).should include task
+ obj.send(:cmd).should_not include Guard::Resque::DEFAULT_TASK_SINGLE
end
- it "should accept task option" do
- obj = Guard::Resque.new [], :task => 'environment foo'
- obj.send(:cmd).should match /environment foo/
- obj.send(:cmd).should_not match /resque:work/
+ it 'should provide default options' do
+ obj = Guard::Resque.new []
+ obj.send(:env).should include 'QUEUE' => Guard::Resque::DEFAULT_QUEUE.to_s
+ obj.send(:env).should include 'COUNT' => Guard::Resque::DEFAULT_COUNT.to_s
+ obj.send(:cmd).should include Guard::Resque::DEFAULT_TASK_SINGLE
end
+
+ it 'should provide different default options when :count is multiple' do
+ count = 2
+
+ obj = Guard::Resque.new [], :count => count
+ obj.send(:env).should include 'QUEUE' => Guard::Resque::DEFAULT_QUEUE.to_s
+ obj.send(:env).should include 'COUNT' => count.to_s
+ obj.send(:cmd).should include Guard::Resque::DEFAULT_TASK_MULTIPLE
+ end
+
end
end
View
2  spec/spec_helper.rb
@@ -1,7 +1,7 @@
require 'rspec'
require 'guard/resque'
-ENV["GUARD_ENV"] = 'test'
+ENV['GUARD_ENV'] = 'test'
RSpec.configure do |config|
config.color_enabled = true
Please sign in to comment.
Something went wrong with that request. Please try again.