Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into api-cleanup
Browse files Browse the repository at this point in the history
Conflicts:
	.rubocop.yml
	lib/resque_scheduler.rb
	lib/resque_scheduler/server.rb
	test/scheduler_task_test.rb
  • Loading branch information
meatballhat committed Feb 22, 2014
2 parents 0aba925 + 3e0c739 commit 4a11c9e
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .simplecov
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SimpleCov.start { add_filter '/test/' } if ENV['COVERAGE']
if ENV['COVERAGE'] && RUBY_PLATFORM !~ /java/
SimpleCov.start { add_filter '/test/' }
end
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ rvm:
env:
global:
- RESQUE_SCHEDULER_DISABLE_TEST_REDIS_SERVER=1
matrix:
allow_failures:
- rvm: jruby-19mode
- rvm: rbx
services:
- redis-server
notifications:
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Resque Scheduler authors
- Tim Liner
- Tony Lewis
- Vincent Zhu
- Vladislav Shub
- V Sreekanth
- andreas
- bbauer
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ require 'rake/testtask'
require 'rubocop/rake_task'
require 'yard'

task default: [:rubocop, :test]
task default: [:rubocop, :test] unless RUBY_PLATFORM =~ /java/
task default: [:test] if RUBY_PLATFORM =~ /java/

Rubocop::RakeTask.new

Expand Down
1 change: 1 addition & 0 deletions lib/resque/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def load_schedule_job(name, config)
job = rufus_scheduler.send(interval_type, *args) do
if master?
log! "queueing #{config['class']} (#{name})"
Resque.last_enqueued_at(name, Time.now.to_s)
handle_errors { enqueue_from_config(config) }
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/resque/scheduler/delaying_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ def scheduled_at(klass, *args)
end
end

def last_enqueued_at(job_name, date)
redis.hset('delayed:last_enqueued_at', job_name, date)
end

def get_last_enqueued_at(job_name)
redis.hget('delayed:last_enqueued_at', job_name)
end

private

def job_to_hash(klass, args)
Expand Down
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 @@ -181,6 +181,11 @@ def scheduler_template(name)
)
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

private

def working_jobs_for_worker(worker)
Expand Down
7 changes: 5 additions & 2 deletions lib/resque/scheduler/server/views/scheduler.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
a job immediately.
Server local time: <%= Time.now %>
</p>

<div style="overflow-y: auto; width:100%; padding: 0px 5px;">
<table>
<tr>
<th></th>
Expand All @@ -15,8 +15,9 @@
<th>Class</th>
<th>Queue</th>
<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 All @@ -31,6 +32,8 @@
<td><%= h schedule_class(config) %></td>
<td><%= h config['queue'] || queue_from_class_name(config['class']) %></td>
<td><%= h config['args'].inspect %></td>
<td><%= h Resque.get_last_enqueued_at(name) || 'Never' %></td>
</tr>
<% end %>
</table>
</div>
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
7 changes: 4 additions & 3 deletions test/scheduler_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

test 'sending TERM to scheduler breaks out of poll_sleep' do
Resque::Scheduler.expects(:release_master_lock!)
fork do

@pid = Process.pid
Thread.new do
sleep(0.05)
system("kill -TERM #{Process.ppid}")
exit!
Process.kill(:TERM, @pid)
end

assert_raises SystemExit do
Expand Down
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 4a11c9e

Please sign in to comment.