davidyang-resque-scheduler 1.10.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Pull in the server test_helper from resque
4
+ require 'resque/server/test_helper.rb'
5
+
6
+ context "on GET to /schedule" do
7
+ setup { get "/schedule" }
8
+
9
+ should_respond_with_success
10
+ end
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
+
27
+ context "on GET to /delayed" do
28
+ setup { get "/delayed" }
29
+
30
+ should_respond_with_success
31
+ end
@@ -0,0 +1,219 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Resque::SchedulerTest < Test::Unit::TestCase
4
+
5
+ class FakeJob
6
+ def self.scheduled(queue, klass, *args); end
7
+ end
8
+
9
+ def setup
10
+ Resque.redis.del(:schedules)
11
+ Resque::Scheduler.mute = true
12
+ Resque::Scheduler.clear_schedule!
13
+ Resque::Scheduler.send(:class_variable_set, :@@scheduled_jobs, {})
14
+ end
15
+
16
+ def test_enqueue_from_config_puts_stuff_in_the_resque_queue
17
+ Resque::Job.stubs(:create).once.returns(true).with(:ivar, SomeIvarJob, '/tmp')
18
+ Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
19
+ end
20
+
21
+ def test_enqueue_from_config_with_custom_class_job_in_the_resque_queue
22
+ FakeJob.stubs(:scheduled).once.returns(true).with(:ivar, 'SomeIvarJob', '/tmp')
23
+ Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'custom_job_class' => 'Resque::SchedulerTest::FakeJob', 'args' => "/tmp")
24
+ end
25
+
26
+ def test_enqueue_from_config_puts_stuff_in_the_resque_queue_when_env_match
27
+ # The job should be loaded : its rails_env config matches the RAILS_ENV variable:
28
+ ENV['RAILS_ENV'] = 'production'
29
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
30
+
31
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'production'}}
32
+ Resque::Scheduler.load_schedule!
33
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
34
+
35
+ # we allow multiple rails_env definition, it should work also:
36
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging, production'}}
37
+ Resque::Scheduler.load_schedule!
38
+ assert_equal(2, Resque::Scheduler.rufus_scheduler.all_jobs.size)
39
+ end
40
+
41
+ def test_enqueue_from_config_dont_puts_stuff_in_the_resque_queue_when_env_doesnt_match
42
+ # RAILS_ENV is not set:
43
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
44
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}}
45
+ Resque::Scheduler.load_schedule!
46
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
47
+
48
+ # SET RAILS_ENV to a common value:
49
+ ENV['RAILS_ENV'] = 'production'
50
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}}
51
+ Resque::Scheduler.load_schedule!
52
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
53
+ end
54
+
55
+ def test_enqueue_from_config_when_rails_env_arg_is_not_set
56
+ # The job should be loaded, since a missing rails_env means ALL envs.
57
+ ENV['RAILS_ENV'] = 'production'
58
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
59
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
60
+ Resque::Scheduler.load_schedule!
61
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
62
+ end
63
+
64
+ def test_config_makes_it_into_the_rufus_scheduler
65
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
66
+
67
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
68
+ Resque::Scheduler.load_schedule!
69
+
70
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
71
+ puts Resque::Scheduler.scheduled_jobs.inspect
72
+ # TODO: decide if we want to pull back as sym or as string
73
+ assert Resque::Scheduler.scheduled_jobs.include?('some_ivar_job')
74
+ end
75
+
76
+ def test_can_reload_schedule
77
+ Resque.schedule = {"some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
78
+ Resque.redis.hset(:schedules, "some_ivar_job", Resque.encode(
79
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}
80
+ ))
81
+
82
+ Resque::Scheduler.load_schedule!
83
+
84
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
85
+ assert Resque::Scheduler.scheduled_jobs.include?("some_ivar_job")
86
+
87
+ Resque.redis.del(:schedules)
88
+ Resque.redis.hset(:schedules, "some_ivar_job2", Resque.encode(
89
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/2"}
90
+ ))
91
+
92
+ Resque::Scheduler.reload_schedule!
93
+
94
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
95
+
96
+ assert_equal '/tmp/2', Resque.schedule["some_ivar_job2"]["args"]
97
+ assert Resque::Scheduler.scheduled_jobs.include?("some_ivar_job2")
98
+ end
99
+
100
+ def test_load_schedule_job
101
+ Resque::Scheduler.load_schedule_job("some_ivar_job", {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"})
102
+
103
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
104
+ assert_equal(1, Resque::Scheduler.scheduled_jobs.size)
105
+ assert Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job")
106
+ end
107
+
108
+ def test_load_schedule_job_with_no_cron
109
+ Resque::Scheduler.load_schedule_job("some_ivar_job", {'class' => 'SomeIvarJob', 'args' => "/tmp"})
110
+
111
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
112
+ assert_equal(0, Resque::Scheduler.scheduled_jobs.size)
113
+ assert !Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job")
114
+ end
115
+
116
+ def test_load_schedule_job_with_blank_cron
117
+ Resque::Scheduler.load_schedule_job("some_ivar_job", {'cron' => '', 'class' => 'SomeIvarJob', 'args' => "/tmp"})
118
+
119
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
120
+ assert_equal(0, Resque::Scheduler.scheduled_jobs.size)
121
+ assert !Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job")
122
+ end
123
+
124
+ def test_update_schedule
125
+ Resque.schedule = {
126
+ "some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"},
127
+ "another_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/5"},
128
+ "stay_put_job" => {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
129
+ }
130
+
131
+ Resque::Scheduler.load_schedule!
132
+
133
+ Resque.redis.hset(:schedules, "some_ivar_job", Resque.encode(
134
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/2"}
135
+ ))
136
+ Resque.redis.hset(:schedules, "new_ivar_job", Resque.encode(
137
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp/3"}
138
+ ))
139
+ Resque.redis.hset(:schedules, "stay_put_job", Resque.encode(
140
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
141
+ ))
142
+
143
+ Resque::Scheduler.update_schedule
144
+
145
+ assert_equal(4, Resque::Scheduler.rufus_scheduler.all_jobs.size)
146
+ assert_equal(4, Resque::Scheduler.scheduled_jobs.size)
147
+ %w(some_ivar_job new_ivar_job stay_put_job).each do |job_name|
148
+ assert Resque::Scheduler.scheduled_jobs.keys.include?(job_name)
149
+ assert Resque.schedule.keys.include?(job_name)
150
+ end
151
+ # We expect these two jobs to still be here since they were introduced into the Redis keys manually
152
+ assert Resque::Scheduler.scheduled_jobs.keys.include?("another_ivar_job")
153
+ assert Resque.schedule.keys.include?("another_ivar_job")
154
+ end
155
+
156
+ def test_update_schedule_with_mocks
157
+ Resque.schedule = {
158
+ "some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"},
159
+ "another_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/5"},
160
+ "stay_put_job" => {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
161
+ }
162
+
163
+ Resque::Scheduler.load_schedule!
164
+ # expect 3 executions from unloading the actual schedule loaded above
165
+ Resque::Scheduler.rufus_scheduler.expects(:unschedule).times(3)
166
+
167
+ Resque.redis.hset(:schedules, "some_ivar_job", Resque.encode(
168
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/2"}
169
+ ))
170
+ Resque.redis.hset(:schedules, "new_ivar_job", Resque.encode(
171
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp/3"}
172
+ ))
173
+ Resque.redis.hset(:schedules, "stay_put_job", Resque.encode(
174
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
175
+ ))
176
+
177
+ Resque::Scheduler.rufus_scheduler.expects(:cron).times(4)
178
+ Resque::Scheduler.update_schedule
179
+
180
+ assert_equal(4, Resque::Scheduler.scheduled_jobs.size)
181
+ %w(some_ivar_job another_ivar_job new_ivar_job stay_put_job).each do |job_name|
182
+ assert Resque::Scheduler.scheduled_jobs.keys.include?(job_name)
183
+ assert Resque.schedule.keys.include?(job_name)
184
+ end
185
+ assert Resque::Scheduler.scheduled_jobs.keys.include?("another_ivar_job")
186
+ assert Resque.schedule.keys.include?("another_ivar_job")
187
+ end
188
+
189
+ def test_set_schedule
190
+ Resque.set_schedule("some_ivar_job", {
191
+ 'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/22"
192
+ })
193
+ assert_equal({'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/22"},
194
+ Resque.decode(Resque.redis.hget(:schedules, "some_ivar_job")))
195
+ end
196
+
197
+ def test_get_schedule
198
+ Resque.redis.hset(:schedules, "some_ivar_job2", Resque.encode(
199
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/33"}
200
+ ))
201
+ assert_equal({'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/33"},
202
+ Resque.get_schedule("some_ivar_job2"))
203
+ end
204
+
205
+ def test_remove_schedule
206
+ Resque.redis.hset(:schedules, "some_ivar_job3", Resque.encode(
207
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/44"}
208
+ ))
209
+ Resque.remove_schedule("some_ivar_job3")
210
+ assert_equal nil, Resque.redis.hget(:schedules, "some_ivar_job3")
211
+ end
212
+
213
+ def test_adheres_to_lint
214
+ assert_nothing_raised do
215
+ Resque::Plugin.lint(Resque::Scheduler)
216
+ end
217
+ end
218
+
219
+ end
@@ -0,0 +1,78 @@
1
+
2
+ # Pretty much copied this file from the resque test_helper since we want
3
+ # to do all the same stuff
4
+
5
+ dir = File.dirname(File.expand_path(__FILE__))
6
+
7
+ require 'rubygems'
8
+ require 'test/unit'
9
+ require 'mocha'
10
+ require 'resque'
11
+ require File.join(dir, '../lib/resque_scheduler')
12
+ $LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__)) + '/../lib'
13
+
14
+
15
+ #
16
+ # make sure we can run redis
17
+ #
18
+
19
+ if !system("which redis-server")
20
+ puts '', "** can't find `redis-server` in your path"
21
+ puts "** try running `sudo rake install`"
22
+ abort ''
23
+ end
24
+
25
+
26
+ #
27
+ # start our own redis when the tests start,
28
+ # kill it when they end
29
+ #
30
+
31
+ at_exit do
32
+ next if $!
33
+
34
+ if defined?(MiniTest)
35
+ exit_code = MiniTest::Unit.new.run(ARGV)
36
+ else
37
+ exit_code = Test::Unit::AutoRunner.run
38
+ end
39
+
40
+ pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
41
+ puts "Killing test redis server..."
42
+ `rm -f #{dir}/dump.rdb`
43
+ Process.kill("KILL", pid.to_i)
44
+ exit exit_code
45
+ end
46
+
47
+ puts "Starting redis for testing at localhost:9736..."
48
+ `redis-server #{dir}/redis-test.conf`
49
+ Resque.redis = 'localhost:9736'
50
+
51
+ ##
52
+ # test/spec/mini 3
53
+ # http://gist.github.com/25455
54
+ # chris@ozmm.org
55
+ #
56
+ def context(*args, &block)
57
+ return super unless (name = args.first) && block
58
+ require 'test/unit'
59
+ klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
60
+ def self.test(name, &block)
61
+ define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
62
+ end
63
+ def self.xtest(*args) end
64
+ def self.setup(&block) define_method(:setup, &block) end
65
+ def self.teardown(&block) define_method(:teardown, &block) end
66
+ end
67
+ (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
68
+ klass.class_eval &block
69
+ end
70
+
71
+ class SomeJob
72
+ def self.perform(repo_id, path)
73
+ end
74
+ end
75
+
76
+ class SomeIvarJob < SomeJob
77
+ @queue = :ivar
78
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: davidyang-resque-scheduler
3
+ version: !ruby/object:Gem::Version
4
+ hash: 41
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 10
9
+ - 11
10
+ version: 1.10.11
11
+ platform: ruby
12
+ authors:
13
+ - Ben VandenBos
14
+ - Brian Landau
15
+ - David Yang
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2010-10-27 00:00:00 -04:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: redis
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 13
32
+ segments:
33
+ - 2
34
+ - 0
35
+ - 1
36
+ version: 2.0.1
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: resque
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 55
48
+ segments:
49
+ - 1
50
+ - 8
51
+ - 0
52
+ version: 1.8.0
53
+ type: :runtime
54
+ version_requirements: *id002
55
+ - !ruby/object:Gem::Dependency
56
+ name: rufus-scheduler
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ type: :development
96
+ version_requirements: *id005
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ prerelease: false
100
+ requirement: &id006 !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ type: :development
110
+ version_requirements: *id006
111
+ description: |-
112
+ Light weight job scheduling on top of Resque.
113
+ Adds methods enqueue_at/enqueue_in to schedule jobs in the future.
114
+ Also supports queueing jobs on a fixed, cron-like schedule.
115
+ email: davidyang@gmail.com
116
+ executables: []
117
+
118
+ extensions: []
119
+
120
+ extra_rdoc_files:
121
+ - LICENSE
122
+ - README.markdown
123
+ files:
124
+ - .gitignore
125
+ - HISTORY.md
126
+ - LICENSE
127
+ - README.markdown
128
+ - Rakefile
129
+ - davidyang-resque-scheduler.gemspec
130
+ - lib/resque/scheduler.rb
131
+ - lib/resque_scheduler.rb
132
+ - lib/resque_scheduler/server.rb
133
+ - lib/resque_scheduler/server/views/delayed.erb
134
+ - lib/resque_scheduler/server/views/delayed_timestamp.erb
135
+ - lib/resque_scheduler/server/views/scheduler.erb
136
+ - lib/resque_scheduler/tasks.rb
137
+ - lib/resque_scheduler/version.rb
138
+ - tasks/resque_scheduler.rake
139
+ - test/delayed_queue_test.rb
140
+ - test/redis-test.conf
141
+ - test/resque-web_test.rb
142
+ - test/scheduler_test.rb
143
+ - test/test_helper.rb
144
+ has_rdoc: true
145
+ homepage: http://github.com/davidyang/resque-scheduler
146
+ licenses: []
147
+
148
+ post_install_message:
149
+ rdoc_options:
150
+ - --charset=UTF-8
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ hash: 3
168
+ segments:
169
+ - 0
170
+ version: "0"
171
+ requirements: []
172
+
173
+ rubyforge_project:
174
+ rubygems_version: 1.3.7
175
+ signing_key:
176
+ specification_version: 3
177
+ summary: Light weight job scheduling on top of Resque
178
+ test_files:
179
+ - test/delayed_queue_test.rb
180
+ - test/resque-web_test.rb
181
+ - test/scheduler_test.rb
182
+ - test/test_helper.rb