resque-retry-on-lock 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jonathon Storer, Mark Kassal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ Software), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ Resque Lock
2
+ ===========
3
+
4
+ A [Resque][rq] plugin. Requires Resque 1.7.0.
5
+
6
+ This plugin is a modification of defunkt's resque-lock plugin
7
+ located at https://github.com/defunkt/resque-lock
8
+
9
+ All credit goes to defunkt.
10
+
11
+ If you want only one instance of your job running at a time, but
12
+ to also re-enqueue rejected jobs, extend it with this module.
13
+
14
+
15
+ For example:
16
+
17
+ require 'resque/plugins/retry_on_lock'
18
+
19
+ class UpdateNetworkGraph
20
+ extend Resque::Plugins::RetryOnLock
21
+
22
+ def self.perform(repo_id)
23
+ heavy_lifting
24
+ end
25
+ end
26
+
27
+ While other UpdateNetworkGraph jobs will be placed on the queue,
28
+ the Locked class will check Redis to see if any others are
29
+ executing with the same arguments before beginning. If another
30
+ is executing the job will be re-enqueued.
31
+
32
+ If you want to define the key yourself you can override the
33
+ `lock` class method in your subclass, e.g.
34
+
35
+ class UpdateNetworkGraph
36
+ extend Resque::Plugins::RetryOnLock
37
+
38
+ Run only one at a time, regardless of repo_id.
39
+ def self.lock(repo_id)
40
+ "network-graph"
41
+ end
42
+
43
+ def self.perform(repo_id)
44
+ heavy_lifting
45
+ end
46
+ end
47
+
48
+ The above modification will ensure only one job of class
49
+ UpdateNetworkGraph is running at a time, regardless of the
50
+ repo_id. Normally a job is locked using a combination of its
51
+ class name and arguments.
52
+
53
+ [rq]: http://github.com/jonstorer/resque-retry-on-lock
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rake/testtask'
2
+ require 'rake/rdoctask'
3
+
4
+ def command?(command)
5
+ system("type #{command} > /dev/null")
6
+ end
7
+
8
+ #
9
+ # Tests
10
+ #
11
+
12
+ task :default => :test
13
+
14
+ if command? :turn
15
+ desc "Run tests"
16
+ task :test do
17
+ suffix = "-n #{ENV['TEST']}" if ENV['TEST']
18
+ sh "turn test/*.rb #{suffix}"
19
+ end
20
+ else
21
+ Rake::TestTask.new do |t|
22
+ t.libs << 'lib'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+ end
27
+
28
+ #
29
+ # Gems
30
+ #
31
+
32
+ begin
33
+ require 'mg'
34
+ MG.new("resque-retry-on-lock.gemspec")
35
+
36
+ desc "Build a gem."
37
+ task :gem => :package
38
+
39
+ # Ensure tests pass before pushing a gem.
40
+ task :gemcutter => :test
41
+
42
+ desc "Push a new version to Gemcutter and publish docs."
43
+ task :publish => :gemcutter do
44
+ sh "git push origin master --tags"
45
+ end
46
+ rescue LoadError
47
+ warn "mg not available."
48
+ warn "Install it with: gem i mg"
49
+ end
@@ -0,0 +1,70 @@
1
+ module Resque
2
+ module Plugins
3
+ # If you want only one instance of your job running at a time,
4
+ # but you want [term] jobs to run later, extend it with
5
+ # this module.
6
+ #
7
+ # For example:
8
+ #
9
+ # require 'resque/plugins/retry_on_lock'
10
+ #
11
+ # class UpdateNetworkGraph
12
+ # extend Resque::Plugins::RetryOnLock
13
+ #
14
+ # def self.perform(repo_id)
15
+ # heavy_lifting
16
+ # end
17
+ # end
18
+ #
19
+ # While other UpdateNetworkGraph jobs will be placed on the queue,
20
+ # the Lock class will check Redis to see if any others are
21
+ # executing with the same arguments before beginning. If another
22
+ # worker isexecuting the job will be re-enqueued.
23
+ #
24
+ # If you want to define the key yourself you can override the
25
+ # `lock` class method in your subclass, e.g.
26
+ #
27
+ # class UpdateNetworkGraph
28
+ # extend Resque::Plugins::RetryOnLock
29
+ #
30
+ # # Run only one at a time, regardless of repo_id.
31
+ # def self.lock(repo_id)
32
+ # "network-graph"
33
+ # end
34
+ #
35
+ # def self.perform(repo_id)
36
+ # heavy_lifting
37
+ # end
38
+ # end
39
+ #
40
+ # The above modification will ensure only one job of class
41
+ # UpdateNetworkGraph is running at a time, regardless of the
42
+ # repo_id. Normally a job is locked using a combination of its
43
+ # class name and arguments.
44
+ module RetryOnLock
45
+ # Override in your job to control the lock key. It is
46
+ # passed the same arguments as `perform`, that is, your job's
47
+ # payload.
48
+ def lock(*args)
49
+ "lock:#{name}-#{args.to_s}"
50
+ end
51
+
52
+ # Where the magic happens.
53
+ def around_perform_lock(*args)
54
+ # Re-Enqueue if another job has created a lock.
55
+ if Resque.redis.setnx(lock(*args), true)
56
+ begin
57
+ yield
58
+ ensure
59
+ # Always clear the lock when we're done, even if there is an
60
+ # error.
61
+ Resque.redis.del(lock(*args))
62
+ end
63
+ else
64
+ Resque.enqueue(self, *args)
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'resque'
4
+ require 'resque/plugins/retry_on_lock'
5
+
6
+ $counter = 0
7
+
8
+ class Job
9
+ extend Resque::Plugins::RetryOnLock
10
+ @queue = :test
11
+
12
+ def self.perform
13
+ $counter += 1
14
+ sleep 1
15
+ end
16
+ end
17
+
18
+ class LockTest < Test::Unit::TestCase
19
+ def test_lint
20
+ assert_nothing_raised do
21
+ Resque::Plugin.lint(Resque::Plugins::RetryOnLock)
22
+ end
23
+ end
24
+
25
+ def test_version
26
+ major, minor, patch = Resque::Version.split('.')
27
+ assert_equal 1, major.to_i
28
+ assert minor.to_i >= 7
29
+ end
30
+
31
+ def test_lock
32
+ (count = 3).times { Resque.enqueue(Job) }
33
+ worker = Resque::Worker.new(Job.instance_eval{@queue})
34
+
35
+ until (size = Resque.size(Job.instance_eval{@queue}.to_s)).zero? do
36
+ workers = []
37
+ size.times { workers << Thread.new { worker.process } }
38
+ workers.each { |t| t.join }
39
+ assert_equal (count - size + 1), $counter
40
+ assert_equal (size - 1), Resque.size(Job.instance_eval{@queue}.to_s)
41
+ end
42
+
43
+ assert_equal count,$counter
44
+
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-retry-on-lock
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Jonathon Storer
14
+ - Mark Kassal
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-03-03 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: |
24
+ A Resque plugin. If you want only one instance of your job
25
+ running at a time, but want to re-enqueue rejected jobs,
26
+ extend it with this module.
27
+
28
+ For example:
29
+
30
+ class UpdateNetworkGraph
31
+ extend Resque::Jobs::RetryOnLocked
32
+
33
+ def self.perform(repo_id)
34
+ heavy_lifting
35
+ end
36
+ end
37
+
38
+ email: me@jonathonstorer.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - LICENSE
49
+ - lib/resque/plugins/retry_on_lock.rb
50
+ - test/retry_on_lock_test.rb
51
+ has_rdoc: true
52
+ homepage: https://github.com/jonstorer/resque-retry-on-lock
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A Resque plugin for ensuring only one instance of your job is running at a time, re-enqueuing duplicates.
85
+ test_files: []
86
+