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,42 @@
1
+ <h1>Delayed Jobs</h1>
2
+
3
+ <p class='intro'>
4
+ This list below contains the timestamps for scheduled delayed jobs.
5
+ </p>
6
+
7
+ <p class='sub'>
8
+ Showing <%= start = params[:start].to_i %> to <%= start + 20 %> of <b><%=size = resque.delayed_queue_schedule_size %></b> timestamps
9
+ </p>
10
+
11
+ <table>
12
+ <tr>
13
+ <th></th>
14
+ <th>Timestamp</th>
15
+ <th>Job count</th>
16
+ <th>Class</th>
17
+ <th>Args</th>
18
+ </tr>
19
+ <% resque.delayed_queue_peek(start, start+20).each do |timestamp| %>
20
+ <tr>
21
+ <td>
22
+ <form action="<%= url "/delayed/queue_now" %>" method="post">
23
+ <input type="hidden" name="timestamp" value="<%= timestamp.to_i %>">
24
+ <input type="submit" value="Queue now">
25
+ </form>
26
+ </td>
27
+ <td><a href="<%= url "delayed/#{timestamp}" %>"><%= format_time(Time.at(timestamp)) %></a></td>
28
+ <td><%= delayed_timestamp_size = resque.delayed_timestamp_size(timestamp) %></td>
29
+ <% job = resque.delayed_timestamp_peek(timestamp, 0, 1).first %>
30
+ <td>
31
+ <% if job && delayed_timestamp_size == 1 %>
32
+ <%= h(job['class']) %>
33
+ <% else %>
34
+ <a href="<%= url "delayed/#{timestamp}" %>">see details</a>
35
+ <% end %>
36
+ </td>
37
+ <td><%= h(job['args'].inspect) if job && delayed_timestamp_size == 1 %></td>
38
+ </tr>
39
+ <% end %>
40
+ </table>
41
+
42
+ <%= partial :next_more, :start => start, :size => size %>
@@ -0,0 +1,26 @@
1
+ <% timestamp = params[:timestamp].to_i %>
2
+
3
+ <h1>Delayed jobs scheduled for <%= format_time(Time.at(timestamp)) %></h1>
4
+
5
+ <p class='sub'>Showing <%= start = params[:start].to_i %> to <%= start + 20 %> of <b><%=size = resque.delayed_timestamp_size(timestamp)%></b> jobs</p>
6
+
7
+ <table class='jobs'>
8
+ <tr>
9
+ <th>Class</th>
10
+ <th>Args</th>
11
+ </tr>
12
+ <% jobs = resque.delayed_timestamp_peek(timestamp, start, 20) %>
13
+ <% jobs.each do |job| %>
14
+ <tr>
15
+ <td class='class'><%= job['class'] %></td>
16
+ <td class='args'><%=h job['args'].inspect %></td>
17
+ </tr>
18
+ <% end %>
19
+ <% if jobs.empty? %>
20
+ <tr>
21
+ <td class='no-data' colspan='2'>There are no pending jobs scheduled for this time.</td>
22
+ </tr>
23
+ <% end %>
24
+ </table>
25
+
26
+ <%= partial :next_more, :start => start, :size => size %>
@@ -0,0 +1,39 @@
1
+ <h1>Schedule</h1>
2
+
3
+ <p class='intro'>
4
+ The list below contains all scheduled jobs. Click &quot;Queue now&quot; to queue
5
+ a job immediately.
6
+ </p>
7
+
8
+ <table>
9
+ <tr>
10
+ <th></th>
11
+ <th>Name</th>
12
+ <th>Description</th>
13
+ <th>Cron</th>
14
+ <th>Class</th>
15
+ <th>Queue</th>
16
+ <th>Arguments</th>
17
+ </tr>
18
+ <% Resque.schedule.keys.sort.each do |name| %>
19
+ <% config = Resque.schedule[name] %>
20
+ <tr>
21
+ <td>
22
+ <form action="<%= url "/schedule/requeue" %>" method="post">
23
+ <input type="hidden" name="job_name" value="<%= h name %>">
24
+ <input type="submit" value="Queue now">
25
+ </form>
26
+ <form action="<%= url "/schedule/delete" %>" method="post">
27
+ <input type="hidden" name="job_name" value="<%= h name %>">
28
+ <input type="submit" value="Delete">
29
+ </form>
30
+ </td>
31
+ <td><%= h name %></td>
32
+ <td><%= h config['description'] %></td>
33
+ <td style="white-space:nowrap"><%= h config['cron'] %></td>
34
+ <td><%= h config['class'] %></td>
35
+ <td><%= h config['queue'] || queue_from_class_name(config['class']) %></td>
36
+ <td><%= h config['args'].inspect %></td>
37
+ </tr>
38
+ <% end %>
39
+ </table>
@@ -0,0 +1,24 @@
1
+ # require 'resque/tasks'
2
+ # will give you the resque tasks
3
+
4
+ namespace :resque do
5
+ task :setup
6
+
7
+ desc "Start Resque Scheduler"
8
+ task :scheduler => :scheduler_setup do
9
+ require 'resque'
10
+ require 'resque_scheduler'
11
+
12
+ Resque::Scheduler.verbose = true if ENV['VERBOSE']
13
+ Resque::Scheduler.run
14
+ end
15
+
16
+ task :scheduler_setup do
17
+ if ENV['INITIALIZER_PATH']
18
+ load ENV['INITIALIZER_PATH'].to_s.strip
19
+ else
20
+ Rake::Task['resque:setup'].invoke
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,3 @@
1
+ module ResqueScheduler
2
+ Version = '1.10.10'
3
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'resque_scheduler/tasks'
@@ -0,0 +1,199 @@
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_enqueue_at_and_enqueue_in_are_equivelent
54
+ timestamp = Time.now + 60
55
+
56
+ Resque.enqueue_at(timestamp, SomeIvarJob, "path")
57
+ Resque.enqueue_in(timestamp - Time.now, SomeIvarJob, "path")
58
+
59
+ assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule), "should have one timestamp in the delayed queue")
60
+ assert_equal(2, Resque.redis.llen("delayed:#{timestamp.to_i}"), "should have 2 items in the timestamp queue")
61
+ end
62
+
63
+ def test_empty_delayed_queue_peek
64
+ assert_equal([], Resque.delayed_queue_peek(0,20))
65
+ end
66
+
67
+ def test_delayed_queue_peek
68
+ t = Time.now
69
+ expected_timestamps = (1..5).to_a.map do |i|
70
+ (t + 60 + i).to_i
71
+ end
72
+
73
+ expected_timestamps.each do |timestamp|
74
+ Resque.delayed_push(timestamp, {:class => SomeIvarJob, :args => 'blah1'})
75
+ end
76
+
77
+ timestamps = Resque.delayed_queue_peek(2,3)
78
+
79
+ assert_equal(expected_timestamps[2,3], timestamps)
80
+ end
81
+
82
+ def test_delayed_queue_schedule_size
83
+ assert_equal(0, Resque.delayed_queue_schedule_size)
84
+ Resque.enqueue_at(Time.now+60, SomeIvarJob)
85
+ assert_equal(1, Resque.delayed_queue_schedule_size)
86
+ end
87
+
88
+ def test_delayed_timestamp_size
89
+ t = Time.now + 60
90
+ assert_equal(0, Resque.delayed_timestamp_size(t))
91
+ Resque.enqueue_at(t, SomeIvarJob)
92
+ assert_equal(1, Resque.delayed_timestamp_size(t))
93
+ assert_equal(0, Resque.delayed_timestamp_size(t.to_i+1))
94
+ end
95
+
96
+ def test_delayed_timestamp_peek
97
+ t = Time.now + 60
98
+ assert_equal([], Resque.delayed_timestamp_peek(t, 0, 1), "make sure it's an empty array, not nil")
99
+ Resque.enqueue_at(t, SomeIvarJob)
100
+ assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 1).length)
101
+ Resque.enqueue_at(t, SomeIvarJob)
102
+ assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 1).length)
103
+ assert_equal(2, Resque.delayed_timestamp_peek(t, 0, 3).length)
104
+
105
+ assert_equal({'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar'}, Resque.delayed_timestamp_peek(t, 0, 1).first)
106
+ end
107
+
108
+ def test_handle_delayed_items_with_no_items
109
+ Resque::Scheduler.expects(:enqueue).never
110
+ Resque::Scheduler.handle_delayed_items
111
+ end
112
+
113
+ def test_handle_delayed_items_with_items
114
+ t = Time.now - 60 # in the past
115
+ Resque.enqueue_at(t, SomeIvarJob)
116
+ Resque.enqueue_at(t, SomeIvarJob)
117
+
118
+ # 2 SomeIvarJob jobs should be created in the "ivar" queue
119
+ Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
120
+ Resque.expects(:queue_from_class).never # Should NOT need to load the class
121
+ Resque::Scheduler.handle_delayed_items
122
+ end
123
+
124
+ def test_enqueue_delayed_items_for_timestamp
125
+ t = Time.now + 60
126
+
127
+ Resque.enqueue_at(t, SomeIvarJob)
128
+ Resque.enqueue_at(t, SomeIvarJob)
129
+
130
+ # 2 SomeIvarJob jobs should be created in the "ivar" queue
131
+ Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
132
+ Resque.expects(:queue_from_class).never # Should NOT need to load the class
133
+
134
+ Resque::Scheduler.enqueue_delayed_items_for_timestamp(t)
135
+
136
+ # delayed queue for timestamp should be empty
137
+ assert_equal(0, Resque.delayed_timestamp_peek(t, 0, 3).length)
138
+ end
139
+
140
+ def test_works_with_out_specifying_queue__upgrade_case
141
+ t = Time.now - 60
142
+ Resque.delayed_push(t, :class => 'SomeIvarJob')
143
+
144
+ # Since we didn't specify :queue when calling delayed_push, it will be forced
145
+ # to load the class to figure out the queue. This is the upgrade case from 1.0.4
146
+ # to 1.0.5.
147
+ Resque::Job.expects(:create).once.with(:ivar, SomeIvarJob, nil)
148
+
149
+ Resque::Scheduler.handle_delayed_items
150
+ end
151
+
152
+ def test_clearing_delayed_queue
153
+ t = Time.now + 120
154
+ 4.times { Resque.enqueue_at(t, SomeIvarJob) }
155
+ 4.times { Resque.enqueue_at(Time.now + rand(100), SomeIvarJob) }
156
+
157
+ Resque.reset_delayed_queue
158
+ assert_equal(0, Resque.delayed_queue_schedule_size)
159
+ end
160
+
161
+ def test_remove_specific_item
162
+ t = Time.now + 120
163
+ Resque.enqueue_at(t, SomeIvarJob)
164
+
165
+ assert_equal(1, Resque.remove_delayed(SomeIvarJob))
166
+ end
167
+
168
+ def test_remove_bogus_item_leaves_the_rest_alone
169
+ t = Time.now + 120
170
+ Resque.enqueue_at(t, SomeIvarJob, "foo")
171
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
172
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
173
+ Resque.enqueue_at(t, SomeIvarJob, "baz")
174
+
175
+ assert_equal(0, Resque.remove_delayed(SomeIvarJob))
176
+ end
177
+
178
+ def test_remove_specific_item_in_group_of_other_items_at_same_timestamp
179
+ t = Time.now + 120
180
+ Resque.enqueue_at(t, SomeIvarJob, "foo")
181
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
182
+ Resque.enqueue_at(t, SomeIvarJob, "bar")
183
+ Resque.enqueue_at(t, SomeIvarJob, "baz")
184
+
185
+ assert_equal(2, Resque.remove_delayed(SomeIvarJob, "bar"))
186
+ assert_equal(1, Resque.delayed_queue_schedule_size)
187
+ end
188
+
189
+ def test_remove_specific_item_in_group_of_other_items_at_different_timestamps
190
+ t = Time.now + 120
191
+ Resque.enqueue_at(t, SomeIvarJob, "foo")
192
+ Resque.enqueue_at(t + 1, SomeIvarJob, "bar")
193
+ Resque.enqueue_at(t + 2, SomeIvarJob, "bar")
194
+ Resque.enqueue_at(t + 3, SomeIvarJob, "baz")
195
+
196
+ assert_equal(2, Resque.remove_delayed(SomeIvarJob, "bar"))
197
+ assert_equal(2, Resque.count_all_scheduled_jobs)
198
+ end
199
+ 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