resque-uuid 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -16,3 +16,15 @@ Resque::Plugins::ResqueUUID.enable!
16
16
  UUIDs can be returned via:
17
17
  * Resque::Job#uuid
18
18
  * YourJobPayloadClass.uuid, if your payload class responds to .uuid= and .uuid
19
+
20
+ Additionally, your payload class can define an after_uuid_generated callback, which will be called with the newly-generated uuid and job args after the job is enqueued
21
+
22
+ ```ruby
23
+ class MyJobClass
24
+
25
+ def self.after_uuid_generated(uuid, *args)
26
+ # do some record keeping or something
27
+ end
28
+
29
+ end
30
+ ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/resque-uuid.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '/resque/plugins/resque_uuid'))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '/resque/plugins/resque_uuid/util'))
2
3
  require File.expand_path(File.join(File.dirname(__FILE__), '/resque/plugins/resque_uuid/resque_extensions'))
3
4
  require File.expand_path(File.join(File.dirname(__FILE__), '/resque/plugins/resque_uuid/job_extensions'))
@@ -10,7 +10,7 @@ module Resque
10
10
  old_after_fork = Resque.after_fork
11
11
 
12
12
  Resque.after_fork do |job|
13
- job.payload_class.uuid = job.payload['uuid'] if job.payload_class.respond_to?(:uuid=)
13
+ job.payload_class.uuid = job.uuid if job.payload_class.respond_to?(:uuid=)
14
14
  old_after_fork.call(job) if old_after_fork
15
15
  end
16
16
  end
@@ -4,7 +4,7 @@ module Resque
4
4
 
5
5
  module JobExtensions
6
6
  def uuid
7
- self.payload['uuid']
7
+ self.payload[:uuid] || self.payload['uuid']
8
8
  end
9
9
  end
10
10
 
@@ -5,14 +5,32 @@ module Resque
5
5
  module ResqueUUID
6
6
 
7
7
  module ResqueExtensions
8
+
8
9
  def self.extended(extender)
9
10
  extender.send(:alias_method, :push_without_uuid, :push)
10
11
  end
11
12
 
12
13
  def push(queue, item)
13
- item['uuid'] = UUIDTools::UUID.random_create.to_s if item.respond_to?(:[]=)
14
+ uuid_generated = false
15
+
16
+ old_uuid = (item[:uuid] || item['uuid']).to_s.strip
17
+
18
+ # don't overwrite existing uuids
19
+ if item.respond_to?(:[]=) && (old_uuid.size == 0)
20
+ item[:uuid] = UUIDTools::UUID.random_create.to_s
21
+ uuid_generated = true
22
+ end
23
+
24
+
25
+ ret = push_without_uuid(queue, item)
26
+
27
+ # TODO (davebenvenuti 6/17/2012) the resque push method has no knowledge of the payload class, so this seems a bit sloppy. However, I couldn't think of a better way to do it
28
+ if uuid_generated
29
+ payload_class = Util.constantize(item[:class] || item['class']) rescue nil
30
+ payload_class.after_uuid_generated(item[:uuid], *item[:args]) if payload_class && payload_class.respond_to?(:after_uuid_generated)
31
+ end
14
32
 
15
- push_without_uuid(queue, item)
33
+ ret
16
34
  end
17
35
 
18
36
  end
@@ -0,0 +1,67 @@
1
+ module Resque
2
+ module Plugins
3
+ module ResqueUUID
4
+
5
+ module Util
6
+ extend self
7
+
8
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
9
+ # #const_defined? and changes their default behavior.
10
+ if Module.method(:const_get).arity == 1
11
+ # Tries to find a constant with the name specified in the argument string:
12
+ #
13
+ # "Module".constantize # => Module
14
+ # "Test::Unit".constantize # => Test::Unit
15
+ #
16
+ # The name is assumed to be the one of a top-level constant, no matter whether
17
+ # it starts with "::" or not. No lexical context is taken into account:
18
+ #
19
+ # C = 'outside'
20
+ # module M
21
+ # C = 'inside'
22
+ # C # => 'inside'
23
+ # "C".constantize # => 'outside', same as ::C
24
+ # end
25
+ #
26
+ # NameError is raised when the name is not in CamelCase or the constant is
27
+ # unknown.
28
+ def constantize(camel_cased_word)
29
+ camel_cased_word = camel_cased_word.to_s
30
+
31
+ if camel_cased_word.include?('-')
32
+ camel_cased_word = classify(camel_cased_word)
33
+ end
34
+
35
+ names = camel_cased_word.split('::')
36
+ names.shift if names.empty? || names.first.empty?
37
+
38
+ constant = Object
39
+ names.each do |name|
40
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
41
+ end
42
+ constant
43
+ end
44
+ else
45
+ def constantize(camel_cased_word) #:nodoc:
46
+ camel_cased_word = camel_cased_word.to_s
47
+
48
+ if camel_cased_word.include?('-')
49
+ camel_cased_word = classify(camel_cased_word)
50
+ end
51
+
52
+ names = camel_cased_word.split('::')
53
+ names.shift if names.empty? || names.first.empty?
54
+
55
+ constant = Object
56
+ names.each do |name|
57
+ constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
58
+ end
59
+ constant
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
67
+ end
data/resque-uuid.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "resque-uuid"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["davebenvenuti"]
12
- s.date = "2012-06-17"
12
+ s.date = "2012-06-18"
13
13
  s.email = "davebenvenuti@gmail.com"
14
14
  s.extra_rdoc_files = [
15
15
  "README.md"
@@ -24,10 +24,12 @@ Gem::Specification.new do |s|
24
24
  "lib/resque/plugins/resque_uuid.rb",
25
25
  "lib/resque/plugins/resque_uuid/job_extensions.rb",
26
26
  "lib/resque/plugins/resque_uuid/resque_extensions.rb",
27
+ "lib/resque/plugins/resque_uuid/util.rb",
27
28
  "resque-uuid.gemspec",
28
29
  "test/redis-test.conf",
29
30
  "test/resque/plugins/resque_uuid/job_extensions_test.rb",
30
31
  "test/resque/plugins/resque_uuid/resque_extensions_test.rb",
32
+ "test/resque/plugins/resque_uuid/util_test.rb",
31
33
  "test/resque/plugins/resque_uuid_test.rb",
32
34
  "test/test_helper.rb"
33
35
  ]
@@ -1,5 +1,24 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "../../../test_helper"))
2
2
 
3
+ class FakeJobClass
4
+
5
+ def self.after_uuid_generated(uuid, *args)
6
+ @passed_uuid = uuid
7
+ @passed_args = args
8
+ end
9
+
10
+ def self.passed_uuid
11
+ @passed_uuid
12
+ end
13
+
14
+ def self.passed_args
15
+ @passed_args
16
+ end
17
+ end
18
+
19
+ class FakeJobClassNoUUIDCallback
20
+ end
21
+
3
22
  class ResqueExtensionsTest < Test::Unit::TestCase
4
23
  class << self
5
24
  # we need to startup and shutdown a redis server for this test case so we can enqueue resque push/pop still work properly
@@ -12,17 +31,55 @@ class ResqueExtensionsTest < Test::Unit::TestCase
12
31
  end
13
32
  end
14
33
 
34
+ setup do
35
+ # clear out resque queues each run
36
+ Resque.redis.flushall
37
+ end
38
+
15
39
  should "add a uuid to job payload when pushed" do
16
40
  test_uuid = UUIDTools::UUID.random_create
17
- UUIDTools::UUID.expects(:random_create).returns(test_uuid)
41
+ UUIDTools::UUID.stubs(:random_create).returns(test_uuid)
18
42
 
19
- fake_payload = { 'payload' => 'blah' }
43
+ Resque.push :my_queue, { :payload => 'blah' }
20
44
 
21
- Resque.push :my_queue, fake_payload
45
+ assert_equal({ 'payload' => 'blah', 'uuid' => test_uuid.to_s }, Resque.pop(:my_queue))
46
+ end
47
+
48
+ should "call after_uuid_generated method defined on payload class" do
49
+ test_uuid = UUIDTools::UUID.random_create
50
+ UUIDTools::UUID.stubs(:random_create).returns(test_uuid)
22
51
 
23
- popped = Resque.pop :my_queue
52
+ Resque.push :my_queue, { :class => FakeJobClass.to_s, :args => [1,2,3] }
53
+
54
+ assert_equal({ 'class' => FakeJobClass.to_s, 'args' => [1,2,3], 'uuid' => test_uuid.to_s }, Resque.pop(:my_queue))
55
+ assert_equal test_uuid.to_s, FakeJobClass.passed_uuid
56
+ assert_equal [1,2,3], FakeJobClass.passed_args
57
+ end
58
+
59
+ should "not call after_uuid_generated if payload class doesn't define one" do
60
+ test_uuid = UUIDTools::UUID.random_create
61
+ UUIDTools::UUID.stubs(:random_create).returns(test_uuid)
62
+
63
+ assert_nothing_raised do
64
+ Resque.push :my_queue, { :class => FakeJobClassNoUUIDCallback.to_s }
65
+ end
66
+
67
+ assert_equal({ 'class' => FakeJobClassNoUUIDCallback.to_s, 'uuid' => test_uuid.to_s }, Resque.pop(:my_queue))
68
+ end
69
+
70
+ should "not overwrite an existing uuid" do
71
+ UUIDTools::UUID.expects(:random_create).never
72
+
73
+ Resque.push :my_queue, { :class => FakeJobClassNoUUIDCallback.to_s, :uuid => 'my_unique_id' }
74
+
75
+ assert_equal({ 'class' => FakeJobClassNoUUIDCallback.to_s, 'uuid' => 'my_unique_id' }, Resque.pop(:my_queue))
76
+ end
77
+
78
+ should "not call after_uuid_generated if a new uuid wasn't generated" do
79
+ test_uuid = UUIDTools::UUID.random_create
80
+ FakeJobClass.expects(:after_uuid_generated).never
24
81
 
25
- assert_equal fake_payload.merge('uuid' => test_uuid.to_s), popped
82
+ Resque.push :my_queue, { :class => FakeJobClassNoUUIDCallback.to_s, 'uuid' => 'my_unique_id' }
26
83
  end
27
84
 
28
85
  end
@@ -0,0 +1,9 @@
1
+ class UtilTest < Test::Unit::TestCase
2
+
3
+ should "constantize" do
4
+
5
+ assert_equal Resque::Plugins::ResqueUUID::JobExtensions, Resque::Plugins::ResqueUUID::Util.constantize('Resque::Plugins::ResqueUUID::JobExtensions')
6
+
7
+ end
8
+
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-06-17 00:00:00.000000000 Z
12
+ date: 2012-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jeweler
16
- requirement: &25018580 !ruby/object:Gem::Requirement
16
+ requirement: &33074480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *25018580
24
+ version_requirements: *33074480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &25017980 !ruby/object:Gem::Requirement
27
+ requirement: &33073860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *25017980
35
+ version_requirements: *33073860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: resque
38
- requirement: &25017360 !ruby/object:Gem::Requirement
38
+ requirement: &33072900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *25017360
46
+ version_requirements: *33072900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: uuidtools
49
- requirement: &25016680 !ruby/object:Gem::Requirement
49
+ requirement: &33072260 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *25016680
57
+ version_requirements: *33072260
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: resque
60
- requirement: &25015720 !ruby/object:Gem::Requirement
60
+ requirement: &33071780 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - =
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 1.13.0
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *25015720
68
+ version_requirements: *33071780
69
69
  description:
70
70
  email: davebenvenuti@gmail.com
71
71
  executables: []
@@ -82,10 +82,12 @@ files:
82
82
  - lib/resque/plugins/resque_uuid.rb
83
83
  - lib/resque/plugins/resque_uuid/job_extensions.rb
84
84
  - lib/resque/plugins/resque_uuid/resque_extensions.rb
85
+ - lib/resque/plugins/resque_uuid/util.rb
85
86
  - resque-uuid.gemspec
86
87
  - test/redis-test.conf
87
88
  - test/resque/plugins/resque_uuid/job_extensions_test.rb
88
89
  - test/resque/plugins/resque_uuid/resque_extensions_test.rb
90
+ - test/resque/plugins/resque_uuid/util_test.rb
89
91
  - test/resque/plugins/resque_uuid_test.rb
90
92
  - test/test_helper.rb
91
93
  homepage: http://github.com/davebenvenuti/resque-uuid