Skip to content

Commit

Permalink
Merge pull request resque#370 from resque/merging-v2.x-stable
Browse files Browse the repository at this point in the history
Merging v2.x stable
  • Loading branch information
meatballhat committed Feb 21, 2014
2 parents 04924e0 + 9f08593 commit 3e0c739
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib/resque/scheduler/lock/resilient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ def locked?
).to_i == 1
end

def timeout=(t)
@timeout = t if locked?
def timeout=(seconds)
if locked?
@timeout = seconds
@locked_sha = nil
@acquire_sha = nil
end
@timeout
end

private
Expand Down
5 changes: 5 additions & 0 deletions lib/resque_scheduler/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ def scheduler_template(name)
File.expand_path("../server/views/#{name}.erb", __FILE__)
)
end

def scheduled_in_this_env?(name)
return true if Resque.schedule[name]['rails_env'].nil?
Resque.schedule[name]['rails_env'] == Resque::Scheduler.env
end
end

get '/schedule' do
Expand Down
2 changes: 1 addition & 1 deletion lib/resque_scheduler/server/views/scheduler.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<th>Arguments</th>
<th>Last Enqueued</th>
</tr>
<% Resque.schedule.keys.sort.each do |name| %>
<% Resque.schedule.keys.sort.select { |n| scheduled_in_this_env?(n) }.each do |name| %>
<% config = Resque.schedule[name] %>
<tr>
<td style="padding-top: 12px; padding-bottom: 2px; width: 10px">
Expand Down
13 changes: 12 additions & 1 deletion test/resque-web_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

context 'on GET to /schedule with scheduled jobs' do
setup do
ENV['RAILS_ENV'] = 'production'
Resque::Scheduler.env = 'production'
Resque.schedule = {
'some_ivar_job' => {
'cron' => '* * * * *',
Expand All @@ -26,6 +26,13 @@
'args' => {
'b' => 'blah'
}
},
'some_fancy_job' => {
'every' => ['1m'],
'queue' => 'fancy',
'class' => 'SomeFancyJob',
'args' => 'sparkles',
'rails_env' => 'fancy'
}
}
Resque::Scheduler.load_schedule!
Expand All @@ -37,6 +44,10 @@
test 'see the scheduled job' do
assert last_response.body.include?('SomeIvarJob')
end

test 'excludes jobs for other envs' do
assert !last_response.body.include?('SomeFancyJob')
end
end

context 'on GET to /delayed' do
Expand Down
31 changes: 30 additions & 1 deletion test/scheduler_locking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,50 @@ def lock_is_not_held(lock)
test 'setting the lock timeout changes the key TTL if we hold it' do
@lock.acquire!

@lock.stubs(:locked?).returns(true)
@lock.timeout = 120
ttl = Resque.redis.ttl(@lock.key)
assert_send [ttl, :>, 100]

@lock.stubs(:locked?).returns(true)
@lock.timeout = 180
ttl = Resque.redis.ttl(@lock.key)
assert_send [ttl, :>, 120]
end

test 'setting the lock timeout is a noop if not held' do
test 'setting lock timeout is a noop if not held' do
@lock.acquire!
@lock.timeout = 100
@lock.stubs(:locked?).returns(false)
@lock.timeout = 120
assert_equal 100, @lock.timeout
end

test 'setting lock timeout nils out lock script' do
@lock.acquire!
@lock.timeout = 100
assert_equal nil, @lock.instance_variable_get(:@locked_sha)
end

test 'setting lock timeout does not nil out lock script if not held' do
@lock.acquire!
@lock.locked?
@lock.stubs(:locked?).returns(false)
@lock.timeout = 100
assert_not_nil @lock.instance_variable_get(:@locked_sha)
end

test 'setting lock timeout nils out acquire script' do
@lock.acquire!
@lock.timeout = 100
assert_equal nil, @lock.instance_variable_get(:@acquire_sha)
end

test 'setting lock timeout does not nil out acquire script if not held' do
@lock.acquire!
@lock.stubs(:locked?).returns(false)
@lock.timeout = 100
assert_not_nil @lock.instance_variable_get(:@acquire_sha)
end
end
end
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class SomeIvarJob < SomeJob
@queue = :ivar
end

class SomeFancyJob < SomeJob
def self.queue
:fancy
end
end

class SomeQuickJob < SomeJob
@queue = :quick
end
Expand Down

0 comments on commit 3e0c739

Please sign in to comment.