resque-meta 1.0.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -1
- data/lib/resque/plugins/meta.rb +16 -2
- data/lib/resque/plugins/meta/metadata.rb +7 -1
- data/lib/resque/plugins/meta/version.rb +1 -1
- data/test/meta_test.rb +23 -1
- data/test/redis-test.conf +0 -5
- metadata +39 -11
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
Resque Meta
|
2
2
|
===========
|
3
3
|
|
4
|
-
|
4
|
+
[![Build Status](https://secure.travis-ci.org/lmarlow/resque-meta.png)](http://travis-ci.org/lmarlow/resque-meta)
|
5
|
+
|
6
|
+
A [Resque][rq] plugin. Requires Resque 1.8 and Redis 2+.
|
5
7
|
|
6
8
|
If you want to be able to add metadata for a job
|
7
9
|
to track anything you want, extend it with this module.
|
data/lib/resque/plugins/meta.rb
CHANGED
@@ -47,6 +47,17 @@ module Resque
|
|
47
47
|
24 * 60 * 60
|
48
48
|
end
|
49
49
|
|
50
|
+
# Override in your job to control how many seconds a job's
|
51
|
+
# metadata will live after it is enqueued and *before* it finishes.
|
52
|
+
# This includes the condition
|
53
|
+
# where the job and metadata are never finished because of a
|
54
|
+
# shutdown or server crash. Defaults to 0 (i.e.
|
55
|
+
# forever). Return nil or 0 to set them to never expire.
|
56
|
+
# Use caution: this value should be longer than your jobs take to run!
|
57
|
+
def before_finish_expire_in
|
58
|
+
0
|
59
|
+
end
|
60
|
+
|
50
61
|
# Enqueues a job in Resque and return the association metadata.
|
51
62
|
# The meta_id in the returned object can be used to fetch the
|
52
63
|
# metadata again in the future.
|
@@ -60,8 +71,11 @@ module Resque
|
|
60
71
|
def store_meta(meta)
|
61
72
|
key = "meta:#{meta.meta_id}"
|
62
73
|
json = Resque.encode(meta.data)
|
63
|
-
|
64
|
-
|
74
|
+
if meta.expire_at > 0
|
75
|
+
Resque.redis.setex(key, meta.expire_at - Time.now.to_i, json)
|
76
|
+
else
|
77
|
+
Resque.redis.set(key, json)
|
78
|
+
end
|
65
79
|
meta
|
66
80
|
end
|
67
81
|
|
@@ -4,7 +4,7 @@ module Resque
|
|
4
4
|
module Plugins
|
5
5
|
module Meta
|
6
6
|
class Metadata
|
7
|
-
attr_reader :job_class, :meta_id, :data, :enqueued_at, :expire_in
|
7
|
+
attr_reader :job_class, :meta_id, :data, :enqueued_at, :expire_in, :before_finish_expire_in
|
8
8
|
|
9
9
|
def initialize(data_hash)
|
10
10
|
data_hash['enqueued_at'] ||= to_time_format_str(Time.now)
|
@@ -18,6 +18,7 @@ module Resque
|
|
18
18
|
data_hash['job_class'] = @job_class.to_s
|
19
19
|
end
|
20
20
|
@expire_in = @job_class.expire_meta_in || 0
|
21
|
+
@before_finish_expire_in = @job_class.before_finish_expire_in || 0
|
21
22
|
end
|
22
23
|
|
23
24
|
# Reload the metadata from the store
|
@@ -64,8 +65,13 @@ module Resque
|
|
64
65
|
end
|
65
66
|
|
66
67
|
def expire_at
|
68
|
+
# expiry after finished
|
67
69
|
if finished? && expire_in > 0
|
68
70
|
finished_at.to_i + expire_in
|
71
|
+
# expiry after enqueued
|
72
|
+
elsif before_finish_expire_in > 0
|
73
|
+
enqueued_at.to_i + before_finish_expire_in
|
74
|
+
# default ttl is forever
|
69
75
|
else
|
70
76
|
0
|
71
77
|
end
|
data/test/meta_test.rb
CHANGED
@@ -49,13 +49,25 @@ class FailingJob
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
+
class TransientJob
|
53
|
+
extend Resque::Plugins::Meta
|
54
|
+
@queue = :test
|
55
|
+
|
56
|
+
def self.perform(meta_id)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.before_finish_expire_in
|
60
|
+
1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
52
64
|
class MetaTest < Test::Unit::TestCase
|
53
65
|
def setup
|
54
66
|
Resque.redis.flushall
|
55
67
|
end
|
56
68
|
|
57
69
|
def test_meta_version
|
58
|
-
assert_equal '
|
70
|
+
assert_equal '2.0.0', Resque::Plugins::Meta::Version
|
59
71
|
end
|
60
72
|
|
61
73
|
def test_lint
|
@@ -120,6 +132,16 @@ class MetaTest < Test::Unit::TestCase
|
|
120
132
|
assert_nil meta
|
121
133
|
end
|
122
134
|
|
135
|
+
def test_expired_metadata_before_finished
|
136
|
+
meta = TransientJob.enqueue()
|
137
|
+
worker = Resque::Worker.new(:test)
|
138
|
+
assert meta = TransientJob.get_meta(meta.meta_id)
|
139
|
+
|
140
|
+
sleep 2
|
141
|
+
meta = TransientJob.get_meta(meta.meta_id)
|
142
|
+
assert_equal nil, meta
|
143
|
+
end
|
144
|
+
|
123
145
|
def test_slow_job
|
124
146
|
meta = SlowJob.enqueue('foo', 'bar')
|
125
147
|
worker = Resque::Worker.new(:test)
|
data/test/redis-test.conf
CHANGED
@@ -109,11 +109,6 @@ databases 16
|
|
109
109
|
|
110
110
|
############################### ADVANCED CONFIG ###############################
|
111
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
|
116
|
-
|
117
112
|
# Use object sharing. Can save a lot of memory if you have many common
|
118
113
|
# string in your dataset, but performs lookups against the shared objects
|
119
114
|
# pool so it uses more CPU and can be a bit slower. Usually it's a good
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resque-meta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version:
|
9
|
+
- 0
|
10
|
+
version: 2.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lee Marlow
|
@@ -15,13 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2012-12-04 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: resque
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
23
|
none: false
|
26
24
|
requirements:
|
27
25
|
- - ~>
|
@@ -32,7 +30,38 @@ dependencies:
|
|
32
30
|
- 8
|
33
31
|
version: "1.8"
|
34
32
|
type: :runtime
|
35
|
-
|
33
|
+
requirement: *id001
|
34
|
+
prerelease: false
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rake
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 63
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 9
|
46
|
+
- 2
|
47
|
+
version: 0.9.2
|
48
|
+
type: :development
|
49
|
+
requirement: *id002
|
50
|
+
prerelease: false
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: json
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
type: :development
|
63
|
+
requirement: *id003
|
64
|
+
prerelease: false
|
36
65
|
description: |
|
37
66
|
A Resque plugin. If you want to be able to add metadata for a job
|
38
67
|
to track anything you want, extend it with this module.
|
@@ -79,7 +108,6 @@ files:
|
|
79
108
|
- test/meta_test.rb
|
80
109
|
- test/redis-test.conf
|
81
110
|
- test/test_helper.rb
|
82
|
-
has_rdoc: false
|
83
111
|
homepage: http://github.com/lmarlow/resque-meta
|
84
112
|
licenses: []
|
85
113
|
|
@@ -109,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
137
|
requirements: []
|
110
138
|
|
111
139
|
rubyforge_project:
|
112
|
-
rubygems_version: 1.
|
140
|
+
rubygems_version: 1.8.24
|
113
141
|
signing_key:
|
114
142
|
specification_version: 3
|
115
143
|
summary: A Resque plugin for storing job metadata.
|