nulogy-resque-scheduler 1.10.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'resque_scheduler/tasks'
@@ -0,0 +1,220 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Resque::DelayedQueueTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ Resque::Scheduler.mute = true
7
+ Resque.redis.flushall
8
+ end
9
+
10
+ def test_enqueue_at_adds_correct_list_and_zset
11
+
12
+ timestamp = Time.now - 1 # 1 second ago (in the past, should come out right away)
13
+
14
+ assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
15
+
16
+ Resque.enqueue_at(timestamp, SomeIvarJob, "path")
17
+
18
+ # Confirm the correct keys were added
19
+ assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should have one entry now")
20
+ assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule), "The delayed_queue_schedule should have 1 entry now")
21
+
22
+ read_timestamp = Resque.next_delayed_timestamp
23
+
24
+ # Confirm the timestamp came out correctly
25
+ assert_equal(timestamp.to_i, read_timestamp, "The timestamp we pull out of redis should match the one we put in")
26
+ item = Resque.next_item_for_timestamp(read_timestamp)
27
+
28
+ # Confirm the item came out correctly
29
+ assert_equal('SomeIvarJob', item['class'], "Should be the same class that we queued")
30
+ assert_equal(["path"], item['args'], "Should have the same arguments that we queued")
31
+
32
+ # And now confirm the keys are gone
33
+ assert(!Resque.redis.exists("delayed:#{timestamp.to_i}"))
34
+ assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule), "delayed queue should be empty")
35
+ end
36
+
37
+ def test_something_in_the_future_doesnt_come_out
38
+ timestamp = Time.now + 600 # 10 minutes from now (in the future, shouldn't come out)
39
+
40
+ assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
41
+
42
+ Resque.enqueue_at(timestamp, SomeIvarJob, "path")
43
+
44
+ # Confirm the correct keys were added
45
+ assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should have one entry now")
46
+ assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule), "The delayed_queue_schedule should have 1 entry now")
47
+
48
+ read_timestamp = Resque.next_delayed_timestamp
49
+
50
+ assert_nil(read_timestamp, "No timestamps should be ready for queueing")
51
+ end
52
+
53
+ def test_something_in_the_future_comes_out_if_you_want_it_to
54
+ timestamp = Time.now + 600 # 10 minutes from now
55
+
56
+ Resque.enqueue_at(timestamp, SomeIvarJob, "path")
57
+
58
+ read_timestamp = Resque.next_delayed_timestamp(timestamp)
59
+
60
+ assert_equal(timestamp.to_i, read_timestamp, "The timestamp we pull out of redis should match the one we put in")
61
+ end
62
+
63
+ def test_enqueue_at_and_enqueue_in_are_equivelent
64
+ timestamp = Time.now + 60
65
+
66
+ Resque.enqueue_at(timestamp, SomeIvarJob, "path")
67
+ Resque.enqueue_in(timestamp - Time.now, SomeIvarJob, "path")
68
+
69
+ assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule), "should have one timestamp in the delayed queue")
70
+ assert_equal(2, Resque.redis.llen("delayed:#{timestamp.to_i}"), "should have 2 items in the timestamp queue")
71
+ end
72
+
73
+ def test_empty_delayed_queue_peek
74
+ assert_equal([], Resque.delayed_queue_peek(0,20))
75
+ end
76
+
77
+ def test_delayed_queue_peek
78
+ t = Time.now
79
+ expected_timestamps = (1..5).to_a.map do |i|
80
+ (t + 60 + i).to_i
81
+ end
82
+
83
+ expected_timestamps.each do |timestamp|
84
+ Resque.delayed_push(timestamp, {:class => SomeIvarJob, :args => 'blah1'})
85
+ end
86
+
87
+ timestamps = Resque.delayed_queue_peek(2,3)
88
+
89
+ assert_equal(expected_timestamps[2,3], timestamps)
90
+ end
91
+
92
+ def test_delayed_queue_schedule_size
93
+ assert_equal(0, Resque.delayed_queue_schedule_size)
94
+ Resque.enqueue_at(Time.now+60, SomeIvarJob)
95
+ assert_equal(1, Resque.delayed_queue_schedule_size)
96
+ end
97
+
98
+ def test_delayed_timestamp_size
99
+ t = Time.now + 60
100
+ assert_equal(0, Resque.delayed_timestamp_size(t))
101
+ Resque.enqueue_at(t, SomeIvarJob)
102
+ assert_equal(1, Resque.delayed_timestamp_size(t))
103
+ assert_equal(0, Resque.delayed_timestamp_size(t.to_i+1))
104
+ end
105
+
106
+ def test_delayed_timestamp_peek
107
+ t = Time.now + 60
108
+ assert_equal([], Resque.delayed_timestamp_peek(t, 0, 1), "make sure it's an empty array, not nil")
109
+ Resque.enqueue_at(t, SomeIvarJob)
110
+ assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 1).length)
111
+ Resque.enqueue_at(t, SomeIvarJob)
112
+ assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 1).length)
113
+ assert_equal(2, Resque.delayed_timestamp_peek(t, 0, 3).length)
114
+
115
+ assert_equal({'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar'}, Resque.delayed_timestamp_peek(t, 0, 1).first)
116
+ end
117
+
118
+ def test_handle_delayed_items_with_no_items
119
+ Resque::Scheduler.expects(:enqueue).never
120
+ Resque::Scheduler.handle_delayed_items
121
+ end
122
+
123
+ def test_handle_delayed_items_with_items
124
+ t = Time.now - 60 # in the past
125
+ Resque.enqueue_at(t, SomeIvarJob)
126
+ Resque.enqueue_at(t, SomeIvarJob)
127
+
128
+ # 2 SomeIvarJob jobs should be created in the "ivar" queue
129
+ Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
130
+ Resque.expects(:queue_from_class).never # Should NOT need to load the class
131
+ Resque::Scheduler.handle_delayed_items
132
+ end
133
+
134
+ def test_handle_delayed_items_with_items_in_the_future
135
+ t = Time.now + 60 # in the future
136
+ Resque.enqueue_at(t, SomeIvarJob)
137
+ Resque.enqueue_at(t, SomeIvarJob)
138
+
139
+ # 2 SomeIvarJob jobs should be created in the "ivar" queue
140
+ Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
141
+ Resque.expects(:queue_from_class).never # Should NOT need to load the class
142
+ Resque::Scheduler.handle_delayed_items(t)
143
+ end
144
+
145
+ def test_enqueue_delayed_items_for_timestamp
146
+ t = Time.now + 60
147
+
148
+ Resque.enqueue_at(t, SomeIvarJob)
149
+ Resque.enqueue_at(t, SomeIvarJob)
150
+
151
+ # 2 SomeIvarJob jobs should be created in the "ivar" queue
152
+ Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
153
+ Resque.expects(:queue_from_class).never # Should NOT need to load the class
154
+
155
+ Resque::Scheduler.enqueue_delayed_items_for_timestamp(t)
156
+
157
+ # delayed queue for timestamp should be empty
158
+ assert_equal(0, Resque.delayed_timestamp_peek(t, 0, 3).length)
159
+ end
160
+
161
+ def test_works_with_out_specifying_queue__upgrade_case
162
+ t = Time.now - 60
163
+ Resque.delayed_push(t, :class => 'SomeIvarJob')
164
+
165
+ # Since we didn't specify :queue when calling delayed_push, it will be forced
166
+ # to load the class to figure out the queue. This is the upgrade case from 1.0.4
167
+ # to 1.0.5.
168
+ Resque::Job.expects(:create).once.with(:ivar, SomeIvarJob, nil)
169
+
170
+ Resque::Scheduler.handle_delayed_items
171
+ end
172
+
173
+ def test_clearing_delayed_queue
174
+ t = Time.now + 120
175
+ 4.times { Resque.enqueue_at(t, SomeIvarJob) }
176
+ 4.times { Resque.enqueue_at(Time.now + rand(100), SomeIvarJob) }
177
+
178
+ Resque.reset_delayed_queue
179
+ assert_equal(0, Resque.delayed_queue_schedule_size)
180
+ end
181
+
182
+ def test_remove_specific_item
183
+ t = Time.now + 120
184
+ Resque.enqueue_at(t, SomeIvarJob)
185
+
186
+ assert_equal(1, Resque.remove_delayed(SomeIvarJob))
187
+ end
188
+
189
+ def test_remove_bogus_item_leaves_the_rest_alone
190
+ t = Time.now + 120
191
+ Resque.enqueue_at(t, SomeIvarJob, "foo")
192
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
193
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
194
+ Resque.enqueue_at(t, SomeIvarJob, "baz")
195
+
196
+ assert_equal(0, Resque.remove_delayed(SomeIvarJob))
197
+ end
198
+
199
+ def test_remove_specific_item_in_group_of_other_items_at_same_timestamp
200
+ t = Time.now + 120
201
+ Resque.enqueue_at(t, SomeIvarJob, "foo")
202
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
203
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
204
+ Resque.enqueue_at(t, SomeIvarJob, "baz")
205
+
206
+ assert_equal(2, Resque.remove_delayed(SomeIvarJob, "bar"))
207
+ assert_equal(1, Resque.delayed_queue_schedule_size)
208
+ end
209
+
210
+ def test_remove_specific_item_in_group_of_other_items_at_different_timestamps
211
+ t = Time.now + 120
212
+ Resque.enqueue_at(t, SomeIvarJob, "foo")
213
+ Resque.enqueue_at(t + 1, SomeIvarJob, "bar")
214
+ Resque.enqueue_at(t + 2, SomeIvarJob, "bar")
215
+ Resque.enqueue_at(t + 3, SomeIvarJob, "baz")
216
+
217
+ assert_equal(2, Resque.remove_delayed(SomeIvarJob, "bar"))
218
+ assert_equal(2, Resque.count_all_scheduled_jobs)
219
+ end
220
+ end
@@ -0,0 +1,115 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./test/redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir ./test/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
@@ -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,257 @@
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::Scheduler.dynamic = false
11
+ Resque.redis.del(:schedules)
12
+ Resque.redis.del(:schedules_changed)
13
+ Resque::Scheduler.mute = true
14
+ Resque::Scheduler.clear_schedule!
15
+ Resque::Scheduler.send(:class_variable_set, :@@scheduled_jobs, {})
16
+ end
17
+
18
+ def test_enqueue_from_config_with_every_syntax
19
+ Resque::Job.stubs(:create).once.returns(true).with('james_queue', SomeIvarJob, '/tmp')
20
+ Resque::Scheduler.enqueue_from_config('every' => '1m', 'class' => 'SomeIvarJob', 'args' => '/tmp', 'queue' => 'james_queue')
21
+ end
22
+
23
+ def test_enqueue_from_config_puts_stuff_in_the_resque_queue
24
+ Resque::Job.stubs(:create).once.returns(true).with(:ivar, SomeIvarJob, '/tmp')
25
+ Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
26
+ end
27
+
28
+ def test_enqueue_from_config_with_custom_class_job_in_the_resque_queue
29
+ FakeJob.stubs(:scheduled).once.returns(true).with(:ivar, 'SomeIvarJob', '/tmp')
30
+ Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'custom_job_class' => 'Resque::SchedulerTest::FakeJob', 'args' => "/tmp")
31
+ end
32
+
33
+ def test_enqueue_from_config_puts_stuff_in_the_resque_queue_when_env_match
34
+ # The job should be loaded : its rails_env config matches the RAILS_ENV variable:
35
+ ENV['RAILS_ENV'] = 'production'
36
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
37
+
38
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'production'}}
39
+ Resque::Scheduler.load_schedule!
40
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
41
+
42
+ # we allow multiple rails_env definition, it should work also:
43
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging, production'}}
44
+ Resque::Scheduler.load_schedule!
45
+ assert_equal(2, Resque::Scheduler.rufus_scheduler.all_jobs.size)
46
+ end
47
+
48
+ def test_enqueue_from_config_dont_puts_stuff_in_the_resque_queue_when_env_doesnt_match
49
+ # RAILS_ENV is not set:
50
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
51
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}}
52
+ Resque::Scheduler.load_schedule!
53
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
54
+
55
+ # SET RAILS_ENV to a common value:
56
+ ENV['RAILS_ENV'] = 'production'
57
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}}
58
+ Resque::Scheduler.load_schedule!
59
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
60
+ end
61
+
62
+ def test_enqueue_from_config_when_rails_env_arg_is_not_set
63
+ # The job should be loaded, since a missing rails_env means ALL envs.
64
+ ENV['RAILS_ENV'] = 'production'
65
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
66
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
67
+ Resque::Scheduler.load_schedule!
68
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
69
+ end
70
+
71
+ def test_config_makes_it_into_the_rufus_scheduler
72
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
73
+
74
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
75
+ Resque::Scheduler.load_schedule!
76
+
77
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
78
+ assert Resque::Scheduler.scheduled_jobs.include?(:some_ivar_job)
79
+ end
80
+
81
+ def test_can_reload_schedule
82
+ Resque::Scheduler.dynamic = true
83
+ Resque.schedule = {"some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
84
+
85
+ Resque::Scheduler.load_schedule!
86
+
87
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
88
+ assert Resque::Scheduler.scheduled_jobs.include?("some_ivar_job")
89
+
90
+ Resque.redis.del(:schedules)
91
+ Resque.redis.hset(:schedules, "some_ivar_job2", Resque.encode(
92
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/2"}
93
+ ))
94
+
95
+ Resque::Scheduler.reload_schedule!
96
+
97
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
98
+
99
+ assert_equal '/tmp/2', Resque.schedule["some_ivar_job2"]["args"]
100
+ assert Resque::Scheduler.scheduled_jobs.include?("some_ivar_job2")
101
+ end
102
+
103
+ def test_load_schedule_job
104
+ Resque::Scheduler.load_schedule_job("some_ivar_job", {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"})
105
+
106
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
107
+ assert_equal(1, Resque::Scheduler.scheduled_jobs.size)
108
+ assert Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job")
109
+ end
110
+
111
+ def test_load_schedule_job_with_no_cron
112
+ Resque::Scheduler.load_schedule_job("some_ivar_job", {'class' => 'SomeIvarJob', 'args' => "/tmp"})
113
+
114
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
115
+ assert_equal(0, Resque::Scheduler.scheduled_jobs.size)
116
+ assert !Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job")
117
+ end
118
+
119
+ def test_load_schedule_job_with_blank_cron
120
+ Resque::Scheduler.load_schedule_job("some_ivar_job", {'cron' => '', 'class' => 'SomeIvarJob', 'args' => "/tmp"})
121
+
122
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
123
+ assert_equal(0, Resque::Scheduler.scheduled_jobs.size)
124
+ assert !Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job")
125
+ end
126
+
127
+ def test_update_schedule
128
+ Resque::Scheduler.dynamic = true
129
+ Resque.schedule = {
130
+ "some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"},
131
+ "another_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/5"},
132
+ "stay_put_job" => {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
133
+ }
134
+
135
+ Resque::Scheduler.load_schedule!
136
+
137
+ Resque.set_schedule("some_ivar_job",
138
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/2"}
139
+ )
140
+ Resque.set_schedule("new_ivar_job",
141
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp/3"}
142
+ )
143
+ Resque.set_schedule("stay_put_job",
144
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
145
+ )
146
+ Resque.remove_schedule("another_ivar_job")
147
+
148
+ Resque::Scheduler.update_schedule
149
+
150
+ assert_equal(3, Resque::Scheduler.rufus_scheduler.all_jobs.size)
151
+ assert_equal(3, Resque::Scheduler.scheduled_jobs.size)
152
+ %w(some_ivar_job new_ivar_job stay_put_job).each do |job_name|
153
+ assert Resque::Scheduler.scheduled_jobs.keys.include?(job_name)
154
+ assert Resque.schedule.keys.include?(job_name)
155
+ end
156
+ assert !Resque::Scheduler.scheduled_jobs.keys.include?("another_ivar_job")
157
+ assert !Resque.schedule.keys.include?("another_ivar_job")
158
+ assert_equal 0, Resque.redis.scard(:schedules_changed)
159
+ end
160
+
161
+ def test_update_schedule_with_mocks
162
+ Resque::Scheduler.dynamic = true
163
+ Resque.schedule = {
164
+ "some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"},
165
+ "another_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/5"},
166
+ "stay_put_job" => {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
167
+ }
168
+
169
+ Resque::Scheduler.load_schedule!
170
+
171
+ Resque::Scheduler.rufus_scheduler.expects(:unschedule).with(Resque::Scheduler.scheduled_jobs["some_ivar_job"].job_id)
172
+ Resque::Scheduler.rufus_scheduler.expects(:unschedule).with(Resque::Scheduler.scheduled_jobs["another_ivar_job"].job_id)
173
+
174
+ Resque.set_schedule("some_ivar_job",
175
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/2"}
176
+ )
177
+ Resque.set_schedule("new_ivar_job",
178
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp/3"}
179
+ )
180
+ Resque.set_schedule("stay_put_job",
181
+ {'cron' => "* * * * *", 'class' => 'SomeJob', 'args' => "/tmp"}
182
+ )
183
+ Resque.remove_schedule("another_ivar_job")
184
+
185
+ Resque::Scheduler.update_schedule
186
+
187
+ assert_equal(3, Resque::Scheduler.scheduled_jobs.size)
188
+ %w(some_ivar_job new_ivar_job stay_put_job).each do |job_name|
189
+ assert Resque::Scheduler.scheduled_jobs.keys.include?(job_name)
190
+ assert Resque.schedule.keys.include?(job_name)
191
+ end
192
+ assert !Resque::Scheduler.scheduled_jobs.keys.include?("another_ivar_job")
193
+ assert !Resque.schedule.keys.include?("another_ivar_job")
194
+ assert_equal 0, Resque.redis.scard(:schedules_changed)
195
+ end
196
+
197
+ def test_set_schedules
198
+ Resque::Scheduler.dynamic = true
199
+ Resque.schedule = {"my_ivar_job" => {
200
+ 'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/75"
201
+ }}
202
+ assert_equal({'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/75"},
203
+ Resque.decode(Resque.redis.hget(:schedules, "my_ivar_job")))
204
+ end
205
+
206
+ def test_set_schedule
207
+ Resque.set_schedule("some_ivar_job", {
208
+ 'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/22"
209
+ })
210
+ assert_equal({'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/22"},
211
+ Resque.decode(Resque.redis.hget(:schedules, "some_ivar_job")))
212
+ assert Resque.redis.sismember(:schedules_changed, "some_ivar_job")
213
+ end
214
+
215
+ def test_get_schedule
216
+ Resque.redis.hset(:schedules, "some_ivar_job2", Resque.encode(
217
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/33"}
218
+ ))
219
+ assert_equal({'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/33"},
220
+ Resque.get_schedule("some_ivar_job2"))
221
+ end
222
+
223
+ def test_remove_schedule
224
+ Resque.redis.hset(:schedules, "some_ivar_job3", Resque.encode(
225
+ {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/44"}
226
+ ))
227
+ Resque.remove_schedule("some_ivar_job3")
228
+ assert_equal nil, Resque.redis.hget(:schedules, "some_ivar_job3")
229
+ assert Resque.redis.sismember(:schedules_changed, "some_ivar_job3")
230
+ end
231
+
232
+ def test_adheres_to_lint
233
+ assert_nothing_raised do
234
+ Resque::Plugin.lint(Resque::Scheduler)
235
+ Resque::Plugin.lint(ResqueScheduler)
236
+ end
237
+ end
238
+
239
+ def test_uses_rufus_airbrake_handler
240
+ backup_stdout = $stdout
241
+ begin
242
+ e = RuntimeError.new("Problem!")
243
+ Airbrake.expects(:notify).with(e)
244
+
245
+ Resque::Scheduler.airbrake = true
246
+ Resque::Scheduler.clear_schedule!
247
+
248
+ $stdout = StringIO.new #swallow up the output from the exception handler
249
+
250
+ # #handle_exception takes a job, but our custom handler doesn't care about it, so we're
251
+ # just going to pass nil for the test
252
+ Resque::Scheduler.rufus_scheduler.handle_exception(nil, e)
253
+ ensure
254
+ $stdout = backup_stdout
255
+ end
256
+ end
257
+ end