resque-scheduler 2.0.0.a → 2.0.0.b

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of resque-scheduler might be problematic. Click here for more details.

data/HISTORY.md CHANGED
@@ -3,6 +3,11 @@
3
3
  * Dynamic schedule support (brianjlandau, davidyang)
4
4
  * Now depends on redis >=1.3
5
5
 
6
+ ## 1.9.8 (???)
7
+
8
+ * Validates delayed jobs prior to insertion into the delayed queue (bogdan)
9
+ * Rescue exceptions that occur during queuing and log them (dgrijalva)
10
+
6
11
  ## 1.9.7 (2010-11-09)
7
12
 
8
13
  * Support for rufus-scheduler "every" syntax (fallwith)
@@ -62,6 +62,9 @@ module Resque
62
62
  # Pulls the schedule from Resque.schedule and loads it into the
63
63
  # rufus scheduler instance
64
64
  def load_schedule!
65
+ # Need to load the schedule from redis for the first time if dynamic
66
+ Resque.reload_schedule! if dynamic
67
+
65
68
  log! "Schedule empty! Set Resque.schedule" if Resque.schedule.empty?
66
69
 
67
70
  @@scheduled_jobs = {}
@@ -129,14 +132,7 @@ module Resque
129
132
  handle_shutdown do
130
133
  if item = Resque.next_item_for_timestamp(timestamp)
131
134
  log "queuing #{item['class']} [delayed]"
132
- queue = item['queue'] || Resque.queue_from_class(constantize(item['class']))
133
- # Support custom job classes like job with status
134
- if (job_klass = item['custom_job_class']) && (job_klass != 'Resque::Job')
135
- # custom job classes not supporting the same API calls must implement the #schedule method
136
- constantize(job_klass).scheduled(queue, item['class'], *item['args'])
137
- else
138
- Resque::Job.create(queue, item['class'], *item['args'])
139
- end
135
+ enqueue_from_config(item)
140
136
  end
141
137
  end
142
138
  # continue processing until there are no more ready items in this timestamp
@@ -150,18 +146,27 @@ module Resque
150
146
  end
151
147
 
152
148
  # Enqueues a job based on a config hash
153
- def enqueue_from_config(config)
154
- args = config['args'] || config[:args]
155
- klass_name = config['class'] || config[:class]
149
+ def enqueue_from_config(job_config)
150
+ args = job_config['args'] || job_config[:args]
151
+ klass_name = job_config['class'] || job_config[:class]
156
152
  params = args.is_a?(Hash) ? [args] : Array(args)
157
- queue = config['queue'] || config[:queue] || Resque.queue_from_class(constantize(klass_name))
158
- # Support custom job classes like job with status
159
- if (job_klass = config['custom_job_class']) && (job_klass != 'Resque::Job')
160
- # custom job classes not supporting the same API calls must implement the #schedule method
161
- constantize(job_klass).scheduled(queue, klass_name, *params)
153
+ queue = job_config['queue'] || job_config[:queue] || Resque.queue_from_class(constantize(klass_name))
154
+ # Support custom job classes like those that inherit from Resque::JobWithStatus (resque-status)
155
+ if (job_klass = job_config['custom_job_class']) && (job_klass != 'Resque::Job')
156
+ # The custom job class API must offer a static "scheduled" method. If the custom
157
+ # job class can not be constantized (via a requeue call from the web perhaps), fall
158
+ # back to enqueing normally via Resque::Job.create.
159
+ begin
160
+ constantize(job_klass).scheduled(queue, klass_name, *params)
161
+ rescue NameError
162
+ # Note that the custom job class (job_config['custom_job_class']) is the one enqueued
163
+ Resque::Job.create(queue, job_klass, *params)
164
+ end
162
165
  else
163
166
  Resque::Job.create(queue, klass_name, *params)
164
167
  end
168
+ rescue
169
+ log! "Failed to enqueue #{klass_name}:\n #{$!}"
165
170
  end
166
171
 
167
172
  def rufus_scheduler
@@ -180,7 +185,6 @@ module Resque
180
185
  def reload_schedule!
181
186
  procline "Reloading Schedule"
182
187
  clear_schedule!
183
- Resque.reload_schedule!
184
188
  load_schedule!
185
189
  end
186
190
 
@@ -232,8 +236,8 @@ module Resque
232
236
  end
233
237
 
234
238
  def procline(string)
239
+ log! string
235
240
  $0 = "resque-scheduler-#{ResqueScheduler::Version}: #{string}"
236
- log! $0
237
241
  end
238
242
 
239
243
  end
@@ -85,6 +85,7 @@ module ResqueScheduler
85
85
  # for queueing. Until timestamp is in the past, the job will
86
86
  # sit in the schedule list.
87
87
  def enqueue_at(timestamp, klass, *args)
88
+ validate_job!(klass)
88
89
  delayed_push(timestamp, job_to_hash(klass, args))
89
90
  end
90
91
 
@@ -194,6 +195,16 @@ module ResqueScheduler
194
195
  end
195
196
  end
196
197
 
198
+ def validate_job!(klass)
199
+ if klass.to_s.empty?
200
+ raise Resque::NoClassError.new("Jobs must be given a class.")
201
+ end
202
+
203
+ unless queue_from_class(klass)
204
+ raise Resque::NoQueueError.new("Jobs must be placed onto a queue.")
205
+ end
206
+ end
207
+
197
208
  end
198
209
 
199
210
  Resque.extend ResqueScheduler
@@ -26,9 +26,13 @@
26
26
  </td>
27
27
  <td><%= h name %></td>
28
28
  <td><%= h config['description'] %></td>
29
- <td style="white-space:nowrap"><%= (config['cron'].nil? && !config['every'].nil?) ?
30
- h('every: ' + config['every']) :
31
- h('cron: ' + config['cron']) %></td>
29
+ <td style="white-space:nowrap"><%= if !config['every'].nil?
30
+ h('every: ' + config['every'])
31
+ elsif !config['cron'].nil?
32
+ h('cron: ' + config['cron'])
33
+ else
34
+ 'Not currently scheduled'
35
+ end %></td>
32
36
  <td><%= (config['class'].nil? && !config['custom_job_class'].nil?) ?
33
37
  h(config['custom_job_class']) :
34
38
  h(config['class']) %></td>
@@ -1,3 +1,3 @@
1
1
  module ResqueScheduler
2
- Version = '2.0.0.a'
2
+ Version = '2.0.0.b'
3
3
  end
@@ -217,4 +217,13 @@ class Resque::DelayedQueueTest < Test::Unit::TestCase
217
217
  assert_equal(2, Resque.remove_delayed(SomeIvarJob, "bar"))
218
218
  assert_equal(2, Resque.count_all_scheduled_jobs)
219
219
  end
220
+
221
+ def test_invalid_job_class
222
+ assert_raise Resque::NoClassError do
223
+ Resque.enqueue_in(10, nil)
224
+ end
225
+ assert_raise Resque::NoQueueError do
226
+ Resque.enqueue_in(10, String) # string serves as invalid Job class
227
+ end
228
+ end
220
229
  end
@@ -25,6 +25,11 @@ class Resque::SchedulerTest < Test::Unit::TestCase
25
25
  Resque::Scheduler.enqueue_from_config('every' => '1m', 'class' => 'JamesJob', 'args' => '/tmp', 'queue' => 'james_queue')
26
26
  end
27
27
 
28
+ def test_enqueue_from_config_doesnt_crash_on_exception_when_enqueueing
29
+ Resque::Job.stubs(:create).raises(Resque::NoQueueError, 'test exception').with(:ivar, 'SomeIvarJob', '/tmp')
30
+ Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
31
+ end
32
+
28
33
  def test_enqueue_from_config_puts_stuff_in_the_resque_queue
29
34
  Resque::Job.stubs(:create).once.returns(true).with(:ivar, 'SomeIvarJob', '/tmp')
30
35
  Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 2
7
7
  - 0
8
8
  - 0
9
- - a
10
- version: 2.0.0.a
9
+ - b
10
+ version: 2.0.0.b
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-12-10 00:00:00 -08:00
18
+ date: 2011-02-25 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency