resque 1.4.0 → 1.5.0

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

Potentially problematic release.


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

data/HISTORY.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 1.5.0 (2010-02-17)
2
+
3
+ * Version now included in procline, e.g. `resque-1.5.0: Message`
4
+ * Web bugfix: Ignore idle works in the "working" page
5
+ * Added `Resque::Job.destroy(queue, klass, *args)`
6
+ * Added `Resque.dequeue(klass, *args)`
7
+
1
8
  ## 1.4.0 (2010-02-11)
2
9
 
3
10
  * Fallback when unable to bind QUIT and USR1 for Windows and JRuby.
@@ -6,6 +6,8 @@ rescue LoadError
6
6
  require 'json'
7
7
  end
8
8
 
9
+ require 'resque/version'
10
+
9
11
  require 'resque/errors'
10
12
 
11
13
  require 'resque/failure'
@@ -131,13 +133,47 @@ module Resque
131
133
  # If either of those conditions are met, it will use the value obtained
132
134
  # from performing one of the above operations to determine the queue.
133
135
  #
134
- # If no queue can be inferred this method will return a non-true value.
136
+ # If no queue can be inferred this method will raise a `Resque::NoQueueError`
135
137
  #
136
138
  # This method is considered part of the `stable` API.
137
139
  def enqueue(klass, *args)
138
- queue = klass.instance_variable_get(:@queue)
139
- queue ||= klass.queue if klass.respond_to?(:queue)
140
- Job.create(queue, klass, *args)
140
+ Job.create(queue_from_class(klass), klass, *args)
141
+ end
142
+
143
+ # This method can be used to conveniently remove a job from a queue.
144
+ # It assumes the class you're passing it is a real Ruby class (not
145
+ # a string or reference) which either:
146
+ #
147
+ # a) has a @queue ivar set
148
+ # b) responds to `queue`
149
+ #
150
+ # If either of those conditions are met, it will use the value obtained
151
+ # from performing one of the above operations to determine the queue.
152
+ #
153
+ # If no queue can be inferred this method will raise a `Resque::NoQueueError`
154
+ #
155
+ # If no args are given, this method will dequeue *all* jobs matching
156
+ # the provided class. See `Resque::Job.destroy` for more
157
+ # information.
158
+ #
159
+ # Example:
160
+ #
161
+ # # Removes all jobs of class `UpdateNetworkGraph`
162
+ # Resque.dequeue(GitHub::Jobs::UpdateNetworkGraph)
163
+ #
164
+ # # Removes all jobs of class `UpdateNetworkGraph` with matching args.
165
+ # Resque.dequeue(GitHub::Jobs::UpdateNetworkGraph, 'repo:135325')
166
+ #
167
+ # This method is considered part of the `stable` API.
168
+ def dequeue(klass, *args)
169
+ Job.destroy(queue_from_class(klass), klass, *args)
170
+ end
171
+
172
+ # Given a class, try to extrapolate an appropriate queue based on a
173
+ # class instance variable or `queue` method.
174
+ def queue_from_class(klass)
175
+ klass.instance_variable_get(:@queue) ||
176
+ (klass.respond_to?(:queue) and klass.queue)
141
177
  end
142
178
 
143
179
  # This method will return a `Resque::Job` object or a non-true value
@@ -36,7 +36,7 @@ module Resque
36
36
  #
37
37
  # Raises an exception if no queue or class is given.
38
38
  def self.create(queue, klass, *args)
39
- if queue.to_s.empty?
39
+ if !queue
40
40
  raise NoQueueError.new("Jobs must be placed onto a queue.")
41
41
  end
42
42
 
@@ -47,6 +47,44 @@ module Resque
47
47
  Resque.push(queue, :class => klass.to_s, :args => args)
48
48
  end
49
49
 
50
+ # Removes a job from a queue. Expects a string queue name, a
51
+ # string class name, and, optionally, args.
52
+ #
53
+ # If no args are provided, it will remove all jobs of the class
54
+ # provided.
55
+ #
56
+ # That is, for these two jobs:
57
+ #
58
+ # { 'class' => 'UpdateGraph', 'args' => ['defunkt'] }
59
+ # { 'class' => 'UpdateGraph', 'args' => ['mojombo'] }
60
+ #
61
+ # The following call will remove both:
62
+ #
63
+ # Resque::Job.destroy(queue, 'UpdateGraph')
64
+ #
65
+ # Whereas specifying args will only remove the 2nd job:
66
+ #
67
+ # Resque::Job.destroy(queue, 'UpdateGraph', 'mojombo')
68
+ #
69
+ # This method can be potentially very slow and memory intensive,
70
+ # depending on the size of your queue, as it loads all jobs into
71
+ # a Ruby array before processing.
72
+ def self.destroy(queue, klass, *args)
73
+ klass = klass.to_s
74
+ queue = "queue:#{queue}"
75
+
76
+ redis.lrange(queue, 0, -1).each do |string|
77
+ json = decode(string)
78
+
79
+ match = json['class'] == klass
80
+ match &= json['args'] == args unless args.empty?
81
+
82
+ if match
83
+ redis.lrem(queue, 0, string)
84
+ end
85
+ end
86
+ end
87
+
50
88
  # Given a string queue name, returns an instance of Resque::Job
51
89
  # if any jobs are available. If not, returns nil.
52
90
  def self.reserve(queue)
@@ -45,6 +45,8 @@
45
45
 
46
46
  <% for worker in workers.sort_by { |w| w.job['run_at'] ? w.job['run_at'] : '' } %>
47
47
  <% job = worker.job %>
48
+ <% next if worker.idle? %>
49
+
48
50
  <tr>
49
51
  <td class='icon'><img src="<%=u state = worker.state %>.png" alt="<%= state %>" title="<%= state %>"></td>
50
52
  <% host, pid, queues = worker.to_s.split(':') %>
@@ -1,3 +1,3 @@
1
1
  module Resque
2
- Version = '1.4.0'
2
+ Version = '1.5.0'
3
3
  end
@@ -115,14 +115,10 @@ module Resque
115
115
 
116
116
  if @child = fork
117
117
  rand # Reseeding
118
- procline = "resque: Forked #{@child} at #{Time.now.to_i}"
119
- $0 = procline
120
- log! procline
118
+ procline "Forked #{@child} at #{Time.now.to_i}"
121
119
  Process.wait
122
120
  else
123
- procline = "resque: Processing #{job.queue} since #{Time.now.to_i}"
124
- $0 = procline
125
- log! procline
121
+ procline "Processing #{job.queue} since #{Time.now.to_i}"
126
122
  process(job, &block)
127
123
  exit! unless @cant_fork
128
124
  end
@@ -131,7 +127,7 @@ module Resque
131
127
  else
132
128
  break if interval.to_i == 0
133
129
  log! "Sleeping for #{interval.to_i}"
134
- $0 = @paused ? "resque: Paused" : "resque: Waiting for #{@queues.join(',')}"
130
+ procline @paused ? "Paused" : "Waiting for #{@queues.join(',')}"
135
131
  sleep interval.to_i
136
132
  end
137
133
  end
@@ -420,6 +416,14 @@ module Resque
420
416
  end
421
417
  end
422
418
 
419
+ # Given a string, sets the procline ($0) and logs.
420
+ # Procline is always in the format of:
421
+ # resque-VERSION: STRING
422
+ def procline(string)
423
+ $0 = "resque-#{Resque::Version}: #{string}"
424
+ log! $0
425
+ end
426
+
423
427
  # Log a message to STDOUT if we are verbose or very_verbose.
424
428
  def log(message)
425
429
  if verbose
@@ -50,12 +50,41 @@ context "Resque" do
50
50
  assert_equal nil, Resque.reserve(:ivar)
51
51
  end
52
52
 
53
+ test "can remove jobs from a queue by way of an ivar" do
54
+ assert_equal 0, Resque.size(:ivar)
55
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
56
+ assert Resque.enqueue(SomeIvarJob, 30, '/tmp')
57
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
58
+ assert Resque::Job.create(:ivar, 'blah-job', 20, '/tmp')
59
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
60
+ assert_equal 5, Resque.size(:ivar)
61
+
62
+ assert Resque.dequeue(SomeIvarJob, 30, '/tmp')
63
+ assert_equal 4, Resque.size(:ivar)
64
+ assert Resque.dequeue(SomeIvarJob)
65
+ assert_equal 1, Resque.size(:ivar)
66
+ end
67
+
53
68
  test "jobs have a nice #inspect" do
54
69
  assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
55
70
  job = Resque.reserve(:jobs)
56
71
  assert_equal '(Job{jobs} | SomeJob | [20, "/tmp"])', job.inspect
57
72
  end
58
73
 
74
+ test "jobs can be destroyed" do
75
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
76
+ assert Resque::Job.create(:jobs, 'BadJob', 20, '/tmp')
77
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
78
+ assert Resque::Job.create(:jobs, 'BadJob', 30, '/tmp')
79
+ assert Resque::Job.create(:jobs, 'BadJob', 20, '/tmp')
80
+
81
+ assert_equal 5, Resque.size(:jobs)
82
+ Resque::Job.destroy(:jobs, 'SomeJob')
83
+ assert_equal 3, Resque.size(:jobs)
84
+ Resque::Job.destroy(:jobs, 'BadJob', 30, '/tmp')
85
+ assert_equal 2, Resque.size(:jobs)
86
+ end
87
+
59
88
  test "jobs can test for equality" do
60
89
  assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
61
90
  assert Resque::Job.create(:jobs, 'some-job', 20, '/tmp')
@@ -197,7 +197,8 @@ context "Resque::Worker" do
197
197
 
198
198
  test "sets $0 while working" do
199
199
  @worker.work(0) do
200
- assert_equal "resque: Processing jobs since #{Time.now.to_i}", $0
200
+ ver = Resque::Version
201
+ assert_equal "resque-#{ver}: Processing jobs since #{Time.now.to_i}", $0
201
202
  end
202
203
  end
203
204
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-11 00:00:00 -08:00
12
+ date: 2010-02-17 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency