resque-status 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -65,7 +65,7 @@ Resque::Plugins::Status module. An example job would look something like:
65
65
 
66
66
  end
67
67
 
68
- One major difference is that intead of implementing <tt>perform</tt> as a
68
+ One major difference is that instead of implementing <tt>perform</tt> as a
69
69
  class method, we do our job implementation within instances of the job class.
70
70
 
71
71
  In order to queue a SleepJob up, we also won't use <tt>Resque.enqueue</tt>, instead
@@ -28,7 +28,7 @@ module Resque
28
28
  # This job would iterate num times updating the status as it goes. At the end
29
29
  # we update the status telling anyone listening to this job that its complete.
30
30
  module Status
31
- VERSION = '0.3.2'
31
+ VERSION = '0.3.3'
32
32
 
33
33
  autoload :Hash, 'resque/plugins/status/hash'
34
34
 
@@ -80,11 +80,17 @@ module Resque
80
80
  end
81
81
 
82
82
  # Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>.
83
- # Returns the UUID of the job
83
+ #
84
+ # Returns the UUID of the job if the job was queued, or nil if the job was
85
+ # rejected by a before_enqueue hook.
84
86
  def enqueue(klass, options = {})
85
- uuid = Resque::Plugins::Status::Hash.create :options => options
86
- Resque.enqueue(klass, uuid, options)
87
- uuid
87
+ uuid = Resque::Plugins::Status::Hash.generate_uuid
88
+ if Resque.enqueue(klass, uuid, options)
89
+ Resque::Plugins::Status::Hash.create uuid, :options => options
90
+ uuid
91
+ else
92
+ nil
93
+ end
88
94
  end
89
95
 
90
96
  # This is the method called by Resque::Worker when processing jobs. It
@@ -11,8 +11,7 @@ module Resque
11
11
 
12
12
  # Create a status, generating a new UUID, passing the message to the status
13
13
  # Returns the UUID of the new status.
14
- def self.create(*messages)
15
- uuid = generate_uuid
14
+ def self.create(uuid, *messages)
16
15
  set(uuid, *messages)
17
16
  redis.zadd(set_key, Time.now.to_i, uuid)
18
17
  redis.zremrangebyscore(set_key, 0, Time.now.to_i - @expire_in) if @expire_in
@@ -40,11 +39,22 @@ module Resque
40
39
  # about ranges
41
40
  def self.clear(range_start = nil, range_end = nil)
42
41
  status_ids(range_start, range_end).each do |id|
43
- redis.del(status_key(id))
44
- redis.zrem(set_key, id)
42
+ remove(id)
45
43
  end
46
44
  end
47
-
45
+
46
+ def self.clear_completed(range_start = nil, range_end = nil)
47
+ status_ids(range_start, range_end).select do |id|
48
+ get(id).completed?
49
+ end.each do |id|
50
+ remove(id)
51
+ end
52
+ end
53
+
54
+ def self.remove(uuid)
55
+ redis.del(status_key(uuid))
56
+ redis.zrem(set_key, uuid)
57
+ end
48
58
  # returns a Redisk::Logger scoped to the UUID. Any options passed are passed
49
59
  # to the logger initialization.
50
60
  #
@@ -10,6 +10,19 @@
10
10
  </div>
11
11
  <div class="status-message"><%= @status.message %></div>
12
12
  <div class="status-time"><%= @status.time? ? @status.time : 'Not started' %></div>
13
+ <h2>Details</h2>
14
+ <div class="status-details">
15
+ <table>
16
+ <thead>
17
+ <tr>
18
+ <th>Key</th>
19
+ <th>Value</th>
20
+ </tr>
21
+ </thead>
22
+ <tbody class="status-details-body">
23
+ </tbody>
24
+ </table>
25
+ </div>
13
26
  </div>
14
27
 
15
28
  <script type="text/javascript" charset="utf-8">
@@ -43,6 +56,15 @@
43
56
  if (json.time) {
44
57
  $status.find('.status-time').text(new Date(json.time * 1000).toString())
45
58
  }
59
+
60
+ var $details = $status.find('.status-details-body');
61
+ $details.empty();
62
+
63
+ for (key in json) {
64
+ var $row = $("<tr>").appendTo($details);
65
+ $("<td>").text(key).appendTo($row);
66
+ $("<td>").text(printValue(key, json[key])).appendTo($row);
67
+ }
46
68
  };
47
69
  var status = $status.attr('rel');
48
70
  if (status == 'working' || status == 'queued' || status == "") {
@@ -53,5 +75,15 @@
53
75
  });
54
76
  };
55
77
 
78
+ function printValue(key, value) {
79
+ if (/(^|_)time$/.test(key) && typeof value == 'number') {
80
+ var time = new Date();
81
+ time.setTime(value * 1000);
82
+ return time.toUTCString();
83
+ } else {
84
+ return JSON.stringify(value);
85
+ }
86
+ }
87
+
56
88
  });
57
89
  </script>
@@ -38,7 +38,7 @@ module Resque
38
38
  end
39
39
 
40
40
  app.post '/statuses/clear/completed' do
41
- Resque::Status.clear_completed
41
+ Resque::Plugins::Status::Hash.clear_completed
42
42
  redirect u(:statuses)
43
43
  end
44
44
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "resque-status"
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aaron Quint"]
12
- s.date = "2012-02-03"
12
+ s.date = "2012-04-09"
13
13
  s.description = "resque-status is an extension to the resque queue system that provides simple trackable jobs. It provides a Resque::Plugins::Status::Hash class which can set/get the statuses of jobs and a Resque::Plugins::Status class that when included provides easily trackable/killable jobs."
14
14
  s.email = "aaron@quirkey.com"
15
15
  s.extra_rdoc_files = [
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
43
43
  s.homepage = "http://github.com/quirkey/resque-status"
44
44
  s.require_paths = ["lib"]
45
45
  s.rubyforge_project = "quirkey"
46
- s.rubygems_version = "1.8.10"
46
+ s.rubygems_version = "1.8.16"
47
47
  s.summary = "resque-status is an extension to the resque queue system that provides simple trackable jobs."
48
48
 
49
49
  if s.respond_to? :specification_version then
@@ -98,3 +98,15 @@ class FailureJob
98
98
  failed("I'm such a failure")
99
99
  end
100
100
  end
101
+
102
+ class NeverQueuedJob
103
+ include Resque::Plugins::Status
104
+
105
+ def self.before_enqueue(*args)
106
+ false
107
+ end
108
+
109
+ def perform
110
+ # will never get called
111
+ end
112
+ end
@@ -32,6 +32,26 @@ class TestResquePluginsStatus < Test::Unit::TestCase
32
32
 
33
33
  end
34
34
 
35
+ context ".create with a failing before_enqueue hook" do
36
+ setup do
37
+ @size = Resque.size(:statused)
38
+ @status_ids_size = Resque::Plugins::Status::Hash.status_ids.length
39
+ @res = NeverQueuedJob.create(:num => 100)
40
+ end
41
+
42
+ should "return nil" do
43
+ assert_equal nil, @res
44
+ end
45
+
46
+ should "not create a status" do
47
+ assert_equal @size, Resque.size(:statused)
48
+ end
49
+
50
+ should "not add the uuid to the statuses" do
51
+ assert_equal @status_ids_size, Resque::Plugins::Status::Hash.status_ids.length
52
+ end
53
+ end
54
+
35
55
  context ".scheduled" do
36
56
  setup do
37
57
  @job_args = {'num' => 100}
@@ -6,9 +6,9 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
6
6
  setup do
7
7
  Resque.redis.flushall
8
8
  Resque::Plugins::Status::Hash.expire_in = nil
9
- @uuid = Resque::Plugins::Status::Hash.create
9
+ @uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
10
10
  Resque::Plugins::Status::Hash.set(@uuid, "my status")
11
- @uuid_with_json = Resque::Plugins::Status::Hash.create({"im" => "json"})
11
+ @uuid_with_json = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, {"im" => "json"})
12
12
  end
13
13
 
14
14
  context ".get" do
@@ -43,17 +43,17 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
43
43
  context ".create" do
44
44
  should "add an item to a key set" do
45
45
  before = Resque::Plugins::Status::Hash.status_ids.length
46
- Resque::Plugins::Status::Hash.create
46
+ Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
47
47
  after = Resque::Plugins::Status::Hash.status_ids.length
48
48
  assert_equal 1, after - before
49
49
  end
50
50
 
51
51
  should "return a uuid" do
52
- assert_match(/^\w{32}$/, Resque::Plugins::Status::Hash.create)
52
+ assert_match(/^\w{32}$/, Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid))
53
53
  end
54
54
 
55
55
  should "store any status passed" do
56
- uuid = Resque::Plugins::Status::Hash.create("initial status")
56
+ uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "initial status")
57
57
  status = Resque::Plugins::Status::Hash.get(uuid)
58
58
  assert status.is_a?(Resque::Plugins::Status::Hash)
59
59
  assert_equal "initial status", status.message
@@ -61,17 +61,17 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
61
61
 
62
62
  should "expire keys if expire_in is set" do
63
63
  Resque::Plugins::Status::Hash.expire_in = 1
64
- uuid = Resque::Plugins::Status::Hash.create("new status")
64
+ uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "new status")
65
65
  assert_contains Resque::Plugins::Status::Hash.status_ids, uuid
66
66
  assert_equal "new status", Resque::Plugins::Status::Hash.get(uuid).message
67
67
  sleep 2
68
- Resque::Plugins::Status::Hash.create
68
+ Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
69
69
  assert_does_not_contain Resque::Plugins::Status::Hash.status_ids, uuid
70
70
  assert_nil Resque::Plugins::Status::Hash.get(uuid)
71
71
  end
72
72
 
73
73
  should "store the options for the job created" do
74
- uuid = Resque::Plugins::Status::Hash.create("new", :options => {'test' => '123'})
74
+ uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "new", :options => {'test' => '123'})
75
75
  assert uuid
76
76
  status = Resque::Plugins::Status::Hash.get(uuid)
77
77
  assert status.is_a?(Resque::Plugins::Status::Hash)
@@ -94,11 +94,38 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
94
94
 
95
95
  end
96
96
 
97
+ context ".clear_completed" do
98
+ setup do
99
+ @completed_status_id = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, {'status' => "completed"})
100
+ @not_completed_status_id = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
101
+ Resque::Plugins::Status::Hash.clear_completed
102
+ end
103
+
104
+ should "clear completed status" do
105
+ assert_nil Resque::Plugins::Status::Hash.get(@completed_status_id)
106
+ end
107
+
108
+ should "not clear not-completed status" do
109
+ status = Resque::Plugins::Status::Hash.get(@not_completed_status_id)
110
+ assert status.is_a?(Resque::Plugins::Status::Hash)
111
+ end
112
+ end
113
+
114
+ context ".remove" do
115
+ setup do
116
+ Resque::Plugins::Status::Hash.remove(@uuid)
117
+ end
118
+
119
+ should "clear specify status" do
120
+ assert_nil Resque::Plugins::Status::Hash.get(@uuid)
121
+ end
122
+ end
123
+
97
124
  context ".status_ids" do
98
125
 
99
126
  setup do
100
127
  @uuids = []
101
- 30.times{ Resque::Plugins::Status::Hash.create }
128
+ 30.times{ Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid) }
102
129
  end
103
130
 
104
131
  should "return an array of job ids" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-status
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-03 00:00:00.000000000Z
12
+ date: 2012-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redisk
16
- requirement: &70279646686820 !ruby/object:Gem::Requirement
16
+ requirement: &70186170509920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.2.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70279646686820
24
+ version_requirements: *70186170509920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: resque
27
- requirement: &70279646686000 !ruby/object:Gem::Requirement
27
+ requirement: &70186170509260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.19'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70279646686000
35
+ version_requirements: *70186170509260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: uuid
38
- requirement: &70279646685180 !ruby/object:Gem::Requirement
38
+ requirement: &70186170508780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70279646685180
46
+ version_requirements: *70186170508780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &70279646684320 !ruby/object:Gem::Requirement
49
+ requirement: &70186170508220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70279646684320
57
+ version_requirements: *70186170508220
58
58
  description: resque-status is an extension to the resque queue system that provides
59
59
  simple trackable jobs. It provides a Resque::Plugins::Status::Hash class which can
60
60
  set/get the statuses of jobs and a Resque::Plugins::Status class that when included
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project: quirkey
111
- rubygems_version: 1.8.10
111
+ rubygems_version: 1.8.16
112
112
  signing_key:
113
113
  specification_version: 3
114
114
  summary: resque-status is an extension to the resque queue system that provides simple