resque-uuid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'jeweler'
4
+ gem 'rake'
5
+ gem 'resque'
6
+ gem 'uuidtools'
7
+
8
+ group :test do
9
+ gem 'test-unit'
10
+ gem 'shoulda'
11
+ gem 'mocha'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.3)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.7.3)
11
+ metaclass (0.0.1)
12
+ mocha (0.11.4)
13
+ metaclass (~> 0.0.1)
14
+ multi_json (1.3.6)
15
+ rack (1.4.1)
16
+ rack-protection (1.2.0)
17
+ rack
18
+ rake (0.9.2.2)
19
+ rdoc (3.12)
20
+ json (~> 1.4)
21
+ redis (2.2.2)
22
+ redis-namespace (1.0.3)
23
+ redis (< 3.0.0)
24
+ resque (1.20.0)
25
+ multi_json (~> 1.0)
26
+ redis-namespace (~> 1.0.2)
27
+ sinatra (>= 0.9.2)
28
+ vegas (~> 0.1.2)
29
+ shoulda (3.0.1)
30
+ shoulda-context (~> 1.0.0)
31
+ shoulda-matchers (~> 1.0.0)
32
+ shoulda-context (1.0.0)
33
+ shoulda-matchers (1.0.0)
34
+ sinatra (1.3.2)
35
+ rack (~> 1.3, >= 1.3.6)
36
+ rack-protection (~> 1.2)
37
+ tilt (~> 1.3, >= 1.3.3)
38
+ test-unit (2.5.0)
39
+ tilt (1.3.3)
40
+ uuidtools (2.1.2)
41
+ vegas (0.1.11)
42
+ rack (>= 1.0.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ jeweler
49
+ mocha
50
+ rake
51
+ resque
52
+ shoulda
53
+ test-unit
54
+ uuidtools
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ resque-uuid
2
+ ===========
3
+
4
+ ResqueUUID will assign random UUIDs to your Resque jobs so that they can be uniquely identified.
5
+
6
+ Usage
7
+ -----
8
+
9
+ ```ruby
10
+ require 'resque'
11
+ require 'resque-uuid'
12
+
13
+ Resque::Plugins::ResqueUUID.enable!
14
+ ```
15
+
16
+ UUIDs can be returned via:
17
+ * Resque::Job#uuid
18
+ * YourJobPayloadClass.uuid, if your payload class responds to .uuid= and .uuid
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "resque-uuid"
8
+ gem.summary = "Generates a UUID for Resque jobs as they are enqueued"
9
+ gem.email = "davebenvenuti@gmail.com"
10
+ gem.homepage = "http://github.com/davebenvenuti/resque-uuid"
11
+ gem.authors = ["davebenvenuti"]
12
+ gem.add_dependency "resque", "1.13.0"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,13 @@
1
+ module Resque
2
+ module Plugins
3
+ module ResqueUUID
4
+
5
+ module JobExtensions
6
+ def uuid
7
+ self.payload['uuid']
8
+ end
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ require 'uuidtools'
2
+
3
+ module Resque
4
+ module Plugins
5
+ module ResqueUUID
6
+
7
+ module ResqueExtensions
8
+ def self.extended(extender)
9
+ extender.send(:alias_method, :push_without_uuid, :push)
10
+ end
11
+
12
+ def push(queue, item)
13
+ item['uuid'] = UUIDTools::UUID.random_create.to_s if item.respond_to?(:[]=)
14
+
15
+ push_without_uuid(queue, item)
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Resque
2
+ module Plugins
3
+
4
+ module ResqueUUID
5
+
6
+ def self.enable!
7
+ Resque.send(:extend, Resque::Plugins::ResqueUUID::ResqueExtensions)
8
+ Resque::Job.send(:include, Resque::Plugins::ResqueUUID::JobExtensions)
9
+
10
+ old_after_fork = Resque.after_fork
11
+
12
+ Resque.after_fork do |job|
13
+ job.payload_class.uuid = job.payload['uuid'] if job.payload_class.respond_to?(:uuid=)
14
+ old_after_fork.call(job) if old_after_fork
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
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/resque_extensions'))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), '/resque/plugins/resque_uuid/job_extensions'))
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "resque-uuid"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["davebenvenuti"]
12
+ s.date = "2012-06-17"
13
+ s.email = "davebenvenuti@gmail.com"
14
+ s.extra_rdoc_files = [
15
+ "README.md"
16
+ ]
17
+ s.files = [
18
+ "Gemfile",
19
+ "Gemfile.lock",
20
+ "README.md",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/resque-uuid.rb",
24
+ "lib/resque/plugins/resque_uuid.rb",
25
+ "lib/resque/plugins/resque_uuid/job_extensions.rb",
26
+ "lib/resque/plugins/resque_uuid/resque_extensions.rb",
27
+ "resque-uuid.gemspec",
28
+ "test/redis-test.conf",
29
+ "test/resque/plugins/resque_uuid/job_extensions_test.rb",
30
+ "test/resque/plugins/resque_uuid/resque_extensions_test.rb",
31
+ "test/resque/plugins/resque_uuid_test.rb",
32
+ "test/test_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/davebenvenuti/resque-uuid"
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.15"
37
+ s.summary = "Generates a UUID for Resque jobs as they are enqueued"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
44
+ s.add_runtime_dependency(%q<rake>, [">= 0"])
45
+ s.add_runtime_dependency(%q<resque>, [">= 0"])
46
+ s.add_runtime_dependency(%q<uuidtools>, [">= 0"])
47
+ s.add_runtime_dependency(%q<resque>, ["= 1.13.0"])
48
+ else
49
+ s.add_dependency(%q<jeweler>, [">= 0"])
50
+ s.add_dependency(%q<rake>, [">= 0"])
51
+ s.add_dependency(%q<resque>, [">= 0"])
52
+ s.add_dependency(%q<uuidtools>, [">= 0"])
53
+ s.add_dependency(%q<resque>, ["= 1.13.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<jeweler>, [">= 0"])
57
+ s.add_dependency(%q<rake>, [">= 0"])
58
+ s.add_dependency(%q<resque>, [">= 0"])
59
+ s.add_dependency(%q<uuidtools>, [">= 0"])
60
+ s.add_dependency(%q<resque>, ["= 1.13.0"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,115 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./test/redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir ./test/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
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
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../../../test_helper"))
2
+
3
+ class JobExtensionsTest < Test::Unit::TestCase
4
+
5
+ should "define uuid method for Resque::Job" do
6
+ job = Resque::Job.new :my_queue, 'some' => 'payload', 'uuid' => 'some_unique_id'
7
+
8
+ assert_equal 'some_unique_id', job.uuid
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../../../test_helper"))
2
+
3
+ class ResqueExtensionsTest < Test::Unit::TestCase
4
+ class << self
5
+ # we need to startup and shutdown a redis server for this test case so we can enqueue resque push/pop still work properly
6
+ def startup
7
+ startup_test_redis
8
+ end
9
+
10
+ def shutdown
11
+ shutdown_test_redis
12
+ end
13
+ end
14
+
15
+ should "add a uuid to job payload when pushed" do
16
+ test_uuid = UUIDTools::UUID.random_create
17
+ UUIDTools::UUID.expects(:random_create).returns(test_uuid)
18
+
19
+ fake_payload = { 'payload' => 'blah' }
20
+
21
+ Resque.push :my_queue, fake_payload
22
+
23
+ popped = Resque.pop :my_queue
24
+
25
+ assert_equal fake_payload.merge('uuid' => test_uuid.to_s), popped
26
+ end
27
+
28
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "/../../test_helper"))
2
+
3
+ class FakeJobClass
4
+ def self.old_after_fork_ran!
5
+ @old_after_fork_ran = true
6
+ end
7
+
8
+ def self.old_after_fork_ran?
9
+ !!@old_after_fork_ran
10
+ end
11
+
12
+ def self.uuid=(val)
13
+ @uuid = val
14
+ end
15
+
16
+ def self.uuid
17
+ @uuid
18
+ end
19
+ end
20
+
21
+ class FakeJobClassNoUUID
22
+ # no self.uuid= method defined
23
+ end
24
+
25
+ class ResqueUUIDTest < Test::Unit::TestCase
26
+
27
+ should "extend Resque" do
28
+ assert Resque.is_a?(Resque::Plugins::ResqueUUID::ResqueExtensions)
29
+ end
30
+
31
+ should "extend Resque::Job" do
32
+ assert Resque::Job.include?(Resque::Plugins::ResqueUUID::JobExtensions)
33
+ end
34
+
35
+ should "define Resque.after_fork" do
36
+ fake_job = Resque::Job.new :my_queue, 'some' => 'payload', 'uuid' => 'my_uuid', 'class' => 'FakeJobClass'
37
+
38
+ Resque.after_fork.call(fake_job)
39
+
40
+ # the old after_fork call is defined in test_helper
41
+ assert fake_job.payload['old_after_fork'], "previously defined after_fork did not execute"
42
+ assert_equal 'my_uuid', fake_job.uuid, "job uuid should be set from payload, but isn't"
43
+ assert_equal 'my_uuid', FakeJobClass.uuid, "payload class uuid should be set from payload, but isn't"
44
+ end
45
+
46
+ should "gracefully handle payload classes with now uuid= method" do
47
+ fake_job = Resque::Job.new :my_queue, 'some' => 'payload', 'uuid' => 'my_uuid', 'class' => 'FakeJobClassNoUUID'
48
+
49
+ assert_nothing_raised do
50
+ Resque.after_fork.call(fake_job)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require 'mocha'
4
+ require 'resque'
5
+
6
+ TEST_DIR_BASE = File.dirname(File.expand_path(__FILE__))
7
+
8
+ require File.expand_path(File.join(TEST_DIR_BASE, '/../lib/resque-uuid'))
9
+
10
+ #
11
+ # make sure we can run redis. we run an actual redis-server to enqure resque push/pop still works properly
12
+ #
13
+ if !system("which redis-server")
14
+ puts '', "** can't find `redis-server` in your path"
15
+ puts "** try running `sudo rake install`"
16
+ abort ''
17
+ end
18
+
19
+ # startup and shutdown of test redis server more or less stolen directly from resque test_helper
20
+ def startup_test_redis
21
+ `redis-server #{TEST_DIR_BASE}/redis-test.conf`
22
+ Resque.redis = 'localhost:9736'
23
+ end
24
+
25
+ def shutdown_test_redis
26
+ processes = `ps -A -o pid,command | grep [r]edis-test`.split("\n")
27
+ pids = processes.map { |process| process.split(" ")[0] }
28
+ `rm -f #{TEST_DIR_BASE}/dump.rdb`
29
+ pids.each { |pid| Process.kill("KILL", pid.to_i) }
30
+ end
31
+
32
+ # define another after_fork to ensure we don't mess up other after_forks that might get created
33
+ Resque.after_fork do |job|
34
+ job.payload['old_after_fork'] = true
35
+ end
36
+
37
+ Resque::Plugins::ResqueUUID.enable!
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-uuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - davebenvenuti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jeweler
16
+ requirement: &25018580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *25018580
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &25017980 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *25017980
36
+ - !ruby/object:Gem::Dependency
37
+ name: resque
38
+ requirement: &25017360 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *25017360
47
+ - !ruby/object:Gem::Dependency
48
+ name: uuidtools
49
+ requirement: &25016680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *25016680
58
+ - !ruby/object:Gem::Dependency
59
+ name: resque
60
+ requirement: &25015720 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - =
64
+ - !ruby/object:Gem::Version
65
+ version: 1.13.0
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *25015720
69
+ description:
70
+ email: davebenvenuti@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - README.md
75
+ files:
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - README.md
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/resque-uuid.rb
82
+ - lib/resque/plugins/resque_uuid.rb
83
+ - lib/resque/plugins/resque_uuid/job_extensions.rb
84
+ - lib/resque/plugins/resque_uuid/resque_extensions.rb
85
+ - resque-uuid.gemspec
86
+ - test/redis-test.conf
87
+ - test/resque/plugins/resque_uuid/job_extensions_test.rb
88
+ - test/resque/plugins/resque_uuid/resque_extensions_test.rb
89
+ - test/resque/plugins/resque_uuid_test.rb
90
+ - test/test_helper.rb
91
+ homepage: http://github.com/davebenvenuti/resque-uuid
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.15
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Generates a UUID for Resque jobs as they are enqueued
115
+ test_files: []