resque-scheduler 1.8.0 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/HISTORY.md +7 -0
- data/README.markdown +36 -0
- data/lib/resque/scheduler.rb +19 -7
- data/lib/resque_scheduler.rb +3 -2
- data/lib/resque_scheduler/version.rb +1 -1
- data/resque-scheduler.gemspec +80 -0
- data/test/delayed_queue_test.rb +2 -2
- data/test/resque-web_test.rb +15 -0
- data/test/scheduler_test.rb +39 -1
- metadata +3 -2
data/.gitignore
CHANGED
data/HISTORY.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## 1.8.1 (TBD)
|
2
|
+
|
3
|
+
* Adding rails_env for scheduled jobs to support scoping jobs by
|
4
|
+
RAILS_ENV (gravis).
|
5
|
+
* Fixing ruby 1.8.6 compatibility issue.
|
6
|
+
* Adding gemspec for bundler support.
|
7
|
+
|
1
8
|
## 1.8.0 (2010-04-14)
|
2
9
|
|
3
10
|
* Moving version to match corresponding resque version
|
data/README.markdown
CHANGED
@@ -86,6 +86,42 @@ since the jobs are stored in a redis sorted set (zset). I can't imagine this
|
|
86
86
|
being an issue for someone since redis is stupidly fast even at log(n), but full
|
87
87
|
disclosure is always best.
|
88
88
|
|
89
|
+
### Schedule jobs per environment
|
90
|
+
|
91
|
+
Resque-Scheduler allows to create schedule jobs for specific envs. The arg
|
92
|
+
`rails_env` (optional) can be used to determine which envs are concerned by the
|
93
|
+
job:
|
94
|
+
|
95
|
+
create_fake_leaderboards:
|
96
|
+
cron: "30 6 * * 1"
|
97
|
+
class: CreateFakeLeaderboards
|
98
|
+
queue: scoring
|
99
|
+
args:
|
100
|
+
rails_env: demo
|
101
|
+
description: "This job will auto-create leaderboards for our online demo"
|
102
|
+
|
103
|
+
The scheduled job create_fake_leaderboards will be created only if the
|
104
|
+
environment variable `RAILS_ENV` is set to demo:
|
105
|
+
|
106
|
+
$ RAILS_ENV=demo rake resque:scheduler
|
107
|
+
|
108
|
+
NOTE: If you have added the 2 lines bellow to your Rails Rakefile
|
109
|
+
(ie: lib/tasks/resque-scheduler.rake), the rails env is loaded automatically
|
110
|
+
and you don't have to specify RAILS_ENV if the var is correctly set in
|
111
|
+
environment.rb
|
112
|
+
|
113
|
+
Multiple envs are allowed, separated by commas:
|
114
|
+
|
115
|
+
create_fake_leaderboards:
|
116
|
+
cron: "30 6 * * 1"
|
117
|
+
class: CreateFakeLeaderboards
|
118
|
+
queue: scoring
|
119
|
+
args:
|
120
|
+
rails_env: demo, staging, production
|
121
|
+
description: "This job will auto-create leaderboards"
|
122
|
+
|
123
|
+
NOTE: If you specify the `rails_env` arg without setting RAILS_ENV as an
|
124
|
+
environment variable, the job won't be loaded.
|
89
125
|
|
90
126
|
Resque-web additions
|
91
127
|
--------------------
|
data/lib/resque/scheduler.rb
CHANGED
@@ -48,18 +48,30 @@ module Resque
|
|
48
48
|
log! "Schedule empty! Set Resque.schedule" if Resque.schedule.empty?
|
49
49
|
|
50
50
|
Resque.schedule.each do |name, config|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
# If rails_env is set in the config, enforce ENV['RAILS_ENV'] as
|
52
|
+
# required for the jobs to be scheduled. If rails_env is missing, the
|
53
|
+
# job should be scheduled regardless of what ENV['RAILS_ENV'] is set
|
54
|
+
# to.
|
55
|
+
if config['rails_env'].nil? || rails_env_matches?(config)
|
56
|
+
log! "Scheduling #{name} "
|
57
|
+
if !config['cron'].nil? && config['cron'].length > 0
|
58
|
+
rufus_scheduler.cron config['cron'] do
|
59
|
+
log! "queuing #{config['class']} (#{name})"
|
60
|
+
enqueue_from_config(config)
|
61
|
+
end
|
62
|
+
else
|
63
|
+
log! "no cron found for #{config['class']} (#{name}) - skipping"
|
56
64
|
end
|
57
|
-
else
|
58
|
-
log! "no cron found for #{config['class']} (#{name}) - skipping"
|
59
65
|
end
|
60
66
|
end
|
61
67
|
end
|
62
68
|
|
69
|
+
# Returns true if the given schedule config hash matches the current
|
70
|
+
# ENV['RAILS_ENV']
|
71
|
+
def rails_env_matches?(config)
|
72
|
+
config['rails_env'] && ENV['RAILS_ENV'] && config['rails_env'].gsub(/\s/,'').split(',').include?(ENV['RAILS_ENV'])
|
73
|
+
end
|
74
|
+
|
63
75
|
# Handles queueing delayed items
|
64
76
|
def handle_delayed_items
|
65
77
|
item = nil
|
data/lib/resque_scheduler.rb
CHANGED
@@ -21,6 +21,7 @@ module ResqueScheduler
|
|
21
21
|
# :class must be a resque worker class
|
22
22
|
# :args can be any yaml which will be converted to a ruby literal and passed
|
23
23
|
# in a params. (optional)
|
24
|
+
# :rails_envs is the list of envs where the job gets loaded. Envs are comma separated (optional)
|
24
25
|
# :description is just that, a description of the job (optional). If params is
|
25
26
|
# an array, each element in the array is passed as a separate param,
|
26
27
|
# otherwise params is passed in as the only parameter to perform.
|
@@ -63,7 +64,7 @@ module ResqueScheduler
|
|
63
64
|
|
64
65
|
# Returns an array of timestamps based on start and count
|
65
66
|
def delayed_queue_peek(start, count)
|
66
|
-
redis.zrange(:delayed_queue_schedule, start, start+count).collect
|
67
|
+
redis.zrange(:delayed_queue_schedule, start, start+count).collect{|x| x.to_i}
|
67
68
|
end
|
68
69
|
|
69
70
|
# Returns the size of the delayed queue schedule
|
@@ -114,4 +115,4 @@ end
|
|
114
115
|
Resque.extend ResqueScheduler
|
115
116
|
Resque::Server.class_eval do
|
116
117
|
include ResqueScheduler::Server
|
117
|
-
end
|
118
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{resque-scheduler}
|
8
|
+
s.version = "1.8.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ben VandenBos"]
|
12
|
+
s.date = %q{2010-05-19}
|
13
|
+
s.description = %q{Light weight job scheduling on top of Resque.
|
14
|
+
Adds methods enqueue_at/enqueue_in to schedule jobs in the future.
|
15
|
+
Also supports queueing jobs on a fixed, cron-like schedule.}
|
16
|
+
s.email = %q{bvandenbos@gmail.com}
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE",
|
19
|
+
"README.markdown"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".gitignore",
|
23
|
+
"HISTORY.md",
|
24
|
+
"LICENSE",
|
25
|
+
"README.markdown",
|
26
|
+
"Rakefile",
|
27
|
+
"lib/resque/scheduler.rb",
|
28
|
+
"lib/resque_scheduler.rb",
|
29
|
+
"lib/resque_scheduler/server.rb",
|
30
|
+
"lib/resque_scheduler/server/views/delayed.erb",
|
31
|
+
"lib/resque_scheduler/server/views/delayed_timestamp.erb",
|
32
|
+
"lib/resque_scheduler/server/views/scheduler.erb",
|
33
|
+
"lib/resque_scheduler/tasks.rb",
|
34
|
+
"lib/resque_scheduler/version.rb",
|
35
|
+
"resque-scheduler.gemspec",
|
36
|
+
"tasks/resque_scheduler.rake",
|
37
|
+
"test/delayed_queue_test.rb",
|
38
|
+
"test/redis-test.conf",
|
39
|
+
"test/resque-web_test.rb",
|
40
|
+
"test/scheduler_test.rb",
|
41
|
+
"test/test_helper.rb"
|
42
|
+
]
|
43
|
+
s.homepage = %q{http://github.com/bvandenbos/resque-scheduler}
|
44
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = %q{1.3.5}
|
47
|
+
s.summary = %q{Light weight job scheduling on top of Resque}
|
48
|
+
s.test_files = [
|
49
|
+
"test/delayed_queue_test.rb",
|
50
|
+
"test/resque-web_test.rb",
|
51
|
+
"test/scheduler_test.rb",
|
52
|
+
"test/test_helper.rb"
|
53
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_runtime_dependency(%q<resque>, [">= 1.8.0"])
|
61
|
+
s.add_runtime_dependency(%q<rufus-scheduler>, [">= 0"])
|
62
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
63
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
64
|
+
s.add_development_dependency(%q<rack-test>, [">= 0"])
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<resque>, [">= 1.8.0"])
|
67
|
+
s.add_dependency(%q<rufus-scheduler>, [">= 0"])
|
68
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
69
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
70
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
s.add_dependency(%q<resque>, [">= 1.8.0"])
|
74
|
+
s.add_dependency(%q<rufus-scheduler>, [">= 0"])
|
75
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
76
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
77
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
data/test/delayed_queue_test.rb
CHANGED
@@ -4,7 +4,7 @@ class Resque::DelayedQueueTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def setup
|
6
6
|
Resque::Scheduler.mute = true
|
7
|
-
Resque.redis.
|
7
|
+
Resque.redis.flushall
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_enqueue_at_adds_correct_list_and_zset
|
@@ -129,4 +129,4 @@ class Resque::DelayedQueueTest < Test::Unit::TestCase
|
|
129
129
|
Resque::Scheduler.handle_delayed_items
|
130
130
|
end
|
131
131
|
|
132
|
-
end
|
132
|
+
end
|
data/test/resque-web_test.rb
CHANGED
@@ -9,6 +9,21 @@ context "on GET to /schedule" do
|
|
9
9
|
should_respond_with_success
|
10
10
|
end
|
11
11
|
|
12
|
+
context "on GET to /schedule with scheduled jobs" do
|
13
|
+
setup do
|
14
|
+
ENV['rails_env'] = 'production'
|
15
|
+
Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'production'}}
|
16
|
+
Resque::Scheduler.load_schedule!
|
17
|
+
get "/schedule"
|
18
|
+
end
|
19
|
+
|
20
|
+
should_respond_with_success
|
21
|
+
|
22
|
+
test 'see the scheduled job' do
|
23
|
+
assert last_response.body.include?('SomeIvarJob')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
12
27
|
context "on GET to /delayed" do
|
13
28
|
setup { get "/delayed" }
|
14
29
|
|
data/test/scheduler_test.rb
CHANGED
@@ -16,6 +16,44 @@ class Resque::SchedulerTest < Test::Unit::TestCase
|
|
16
16
|
Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
|
17
17
|
end
|
18
18
|
|
19
|
+
def test_enqueue_from_config_puts_stuff_in_the_resque_queue_when_env_match
|
20
|
+
# The job should be loaded : its rails_env config matches the RAILS_ENV variable:
|
21
|
+
ENV['RAILS_ENV'] = 'production'
|
22
|
+
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
23
|
+
|
24
|
+
Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'production'}}
|
25
|
+
Resque::Scheduler.load_schedule!
|
26
|
+
assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
27
|
+
|
28
|
+
# we allow multiple rails_env definition, it should work also:
|
29
|
+
Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging, production'}}
|
30
|
+
Resque::Scheduler.load_schedule!
|
31
|
+
assert_equal(2, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_enqueue_from_config_dont_puts_stuff_in_the_resque_queue_when_env_doesnt_match
|
35
|
+
# RAILS_ENV is not set:
|
36
|
+
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
37
|
+
Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}}
|
38
|
+
Resque::Scheduler.load_schedule!
|
39
|
+
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
40
|
+
|
41
|
+
# SET RAILS_ENV to a common value:
|
42
|
+
ENV['RAILS_ENV'] = 'production'
|
43
|
+
Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}}
|
44
|
+
Resque::Scheduler.load_schedule!
|
45
|
+
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_enqueue_from_config_when_rails_env_arg_is_not_set
|
49
|
+
# The job should be loaded, since a missing rails_env means ALL envs.
|
50
|
+
ENV['RAILS_ENV'] = 'production'
|
51
|
+
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
52
|
+
Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
|
53
|
+
Resque::Scheduler.load_schedule!
|
54
|
+
assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
55
|
+
end
|
56
|
+
|
19
57
|
def test_config_makes_it_into_the_rufus_scheduler
|
20
58
|
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
21
59
|
|
@@ -25,4 +63,4 @@ class Resque::SchedulerTest < Test::Unit::TestCase
|
|
25
63
|
assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
|
26
64
|
end
|
27
65
|
|
28
|
-
end
|
66
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resque-scheduler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben VandenBos
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-05-19 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- lib/resque_scheduler/server/views/scheduler.erb
|
89
89
|
- lib/resque_scheduler/tasks.rb
|
90
90
|
- lib/resque_scheduler/version.rb
|
91
|
+
- resque-scheduler.gemspec
|
91
92
|
- tasks/resque_scheduler.rake
|
92
93
|
- test/delayed_queue_test.rb
|
93
94
|
- test/redis-test.conf
|