resque-scheduler 1.9.3 → 1.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.9.4 (2010-07-29)
2
+
3
+ * Adding ability to remove jobs from delayed queue (joshsz)
4
+ * Fixing issue #23 (removing .present? reference)
5
+
1
6
  ## 1.9.3 (2010-07-07)
2
7
 
3
8
  * Bug fix (#19)
data/README.markdown CHANGED
@@ -86,6 +86,15 @@ 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
+ *Removing Delayed jobs*
90
+
91
+ If you have the need to cancel a delayed job, you can do so thusly:
92
+
93
+ # after you've enqueued a job like:
94
+ Resque.enqueue_at(5.days.from_now, SendFollowUpEmail, :user_id => current_user.id)
95
+ # remove the job with exactly the same parameters:
96
+ Resque.remove_delayed(SendFollowUpEmail, :user_id => current_user.id)
97
+
89
98
  ### Schedule jobs per environment
90
99
 
91
100
  Resque-Scheduler allows to create schedule jobs for specific envs. The arg
@@ -138,6 +147,28 @@ The Delayed tab:
138
147
 
139
148
  ![The Delayed Tab](http://img.skitch.com/20100111-ne4fcqtc5emkcuwc5qtais2kwx.jpg)
140
149
 
150
+ Get get these to show up you need to pass a file to `resque-web` to tell it to
151
+ include the `resque-scheduler` plugin. You probably already have a file somewhere
152
+ where you configure `resque`. It probably looks something like this:
153
+
154
+ require 'resque' # include resque so we can configure it
155
+ Resque.redis = "redis_server:6379" # tell Resque where redis lives
156
+
157
+ Now, you want to add the following:
158
+
159
+ require 'resque_scheduler' # include the resque_scheduler (this makes the tabs show up)
160
+
161
+ And if you have a schedule you want to set, add this:
162
+
163
+ Resque.schedule = YAML.load_file(File.join(RAILS_ROOT, 'config/resque_schedule.yml')) # load the schedule
164
+
165
+ Now make sure you're passing that file to resque-web like so:
166
+
167
+ resque-web ~/yourapp/config/resque_config.rb
168
+
169
+ That should make the scheduler tabs show up in `resque-web`.
170
+
171
+
141
172
 
142
173
  Installation and the Scheduler process
143
174
  --------------------------------------
@@ -39,7 +39,7 @@ module ResqueScheduler
39
39
  # for queueing. Until timestamp is in the past, the job will
40
40
  # sit in the schedule list.
41
41
  def enqueue_at(timestamp, klass, *args)
42
- delayed_push(timestamp, :class => klass.to_s, :args => args, :queue => queue_from_class(klass))
42
+ delayed_push(timestamp, job_to_hash(klass, args))
43
43
  end
44
44
 
45
45
  # Identical to enqueue_at but takes number_of_seconds_from_now
@@ -104,13 +104,50 @@ module ResqueScheduler
104
104
  item = decode redis.lpop(key)
105
105
 
106
106
  # If the list is empty, remove it.
107
- if 0 == redis.llen(key).to_i
108
- redis.del key
109
- redis.zrem :delayed_queue_schedule, timestamp.to_i
110
- end
107
+ clean_up_timestamp(key, timestamp)
111
108
  item
112
109
  end
113
110
 
111
+ # Clears all jobs created with enqueue_at or enqueue_in
112
+ def reset_delayed_queue
113
+ Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |item|
114
+ redis.del "delayed:#{item}"
115
+ end
116
+
117
+ redis.del :delayed_queue_schedule
118
+ end
119
+
120
+ # given an encoded item, remove it from the delayed_queue
121
+ def remove_delayed(klass, *args)
122
+ destroyed = 0
123
+ search = encode(job_to_hash(klass, args))
124
+ Array(redis.keys("delayed:*")).each do |key|
125
+ destroyed += redis.lrem key, 0, search
126
+ end
127
+ destroyed
128
+ end
129
+
130
+ def count_all_scheduled_jobs
131
+ total_jobs = 0
132
+ Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |timestamp|
133
+ total_jobs += redis.llen("delayed:#{timestamp}").to_i
134
+ end
135
+ total_jobs
136
+ end
137
+
138
+ private
139
+ def job_to_hash(klass, args)
140
+ {:class => klass.to_s, :args => args, :queue => queue_from_class(klass)}
141
+ end
142
+
143
+ def clean_up_timestamp(key, timestamp)
144
+ # If the list is empty, remove it.
145
+ if 0 == redis.llen(key).to_i
146
+ redis.del key
147
+ redis.zrem :delayed_queue_schedule, timestamp.to_i
148
+ end
149
+ end
150
+
114
151
  end
115
152
 
116
153
  Resque.extend ResqueScheduler
@@ -41,7 +41,7 @@ module ResqueScheduler
41
41
 
42
42
  post "/delayed/queue_now" do
43
43
  timestamp = params['timestamp']
44
- Resque::Scheduler.enqueue_delayed_items_for_timestamp(timestamp.to_i) if timestamp.present?
44
+ Resque::Scheduler.enqueue_delayed_items_for_timestamp(timestamp.to_i) if timestamp.to_i > 0
45
45
  redirect url("/overview")
46
46
  end
47
47
 
@@ -1,3 +1,3 @@
1
1
  module ResqueScheduler
2
- Version = '1.9.3'
2
+ Version = '1.9.4'
3
3
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{resque-scheduler}
8
- s.version = "1.9.3"
8
+ s.version = "1.9.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben VandenBos"]
12
- s.date = %q{2010-07-07}
12
+ s.date = %q{2010-07-29}
13
13
  s.description = %q{Light weight job scheduling on top of Resque.
14
14
  Adds methods enqueue_at/enqueue_in to schedule jobs in the future.
15
15
  Also supports queueing jobs on a fixed, cron-like schedule.}
@@ -148,5 +148,52 @@ class Resque::DelayedQueueTest < Test::Unit::TestCase
148
148
 
149
149
  Resque::Scheduler.handle_delayed_items
150
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
151
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
152
199
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-scheduler
3
3
  version: !ruby/object:Gem::Version
4
- hash: 53
4
+ hash: 59
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 9
9
- - 3
10
- version: 1.9.3
9
+ - 4
10
+ version: 1.9.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben VandenBos
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-07 00:00:00 -07:00
18
+ date: 2010-07-29 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency