resque-queue-lock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) Chris Wanstrath, Ray Krueger
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,39 @@
1
+ Resque Queue Lock
2
+ =================
3
+
4
+ A [Resque][rq] plugin. Requires Resque 1.7.0.
5
+
6
+ If you want only one instance of your job queued at a time, extend it with this module.
7
+
8
+ For example:
9
+
10
+ require 'resque/plugins/queue/lock'
11
+
12
+ class ExampleJob
13
+ extend Resque::Plugins::Queue::Lock
14
+
15
+ def self.perform(*args)
16
+ heavy_lifting
17
+ end
18
+ end
19
+
20
+ While this job is queued, no other ExampleJob jobs with the same arguments will be placed on the queue.
21
+
22
+ If you want to define the lock id yourself you can override the `queue_lock` class method in your job class. It takes the same arguments as `perform`. e.g.
23
+
24
+ class ExampleJob
25
+ extend Resque::Plugins::Queue::Lock
26
+
27
+ def self.queue_lock(*args)
28
+ "network-graph"
29
+ end
30
+
31
+ def self.perform(*args)
32
+ heavy_lifting
33
+ end
34
+ end
35
+
36
+ This differs from the [Resque Lock plugin][resque-lock] in that this plugin will allow jobs with the same lock key to be running and in the queue at the same time. It only prevents multiple jobs with the same lock from being in the queue.
37
+
38
+ [rq]: http://github.com/defunkt/resque
39
+ [resque-lock]: https://github.com/defunkt/resque-lock
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
@@ -0,0 +1,65 @@
1
+ module Resque
2
+ module Plugins
3
+ module Queue
4
+ module Lock
5
+ # Override in your job to control the lock key. It is
6
+ # passed the same arguments as `perform`, that is, your job's
7
+ # payload.
8
+ def queue_lock(*args)
9
+ "#{name}-#{args.to_s}"
10
+ end
11
+
12
+ def before_enqueue__queue_lock(*args)
13
+ _acquire_lock(*args)
14
+ end
15
+
16
+ def before_dequeue__queue_lock(*args)
17
+ _release_lock(*args)
18
+ end
19
+
20
+ def before_perform__queue_lock(*args)
21
+ _release_lock(*args)
22
+ end
23
+
24
+
25
+ def _release_lock(*args)
26
+ _reliably do
27
+ Resque.redis.del( _namespaced_queue_lock(*args) )
28
+ end
29
+ end
30
+
31
+ def _acquire_lock(*args)
32
+ _reliably do
33
+ Resque.redis.setnx( _namespaced_queue_lock(*args), Time.now )
34
+ end
35
+ end
36
+
37
+ def _namespaced_queue_lock(*args)
38
+ lock_name = queue_lock( *Resque::Job.decode(Resque::Job.encode(args)) )
39
+ "queuelock:#{lock_name}"
40
+ end
41
+
42
+ def _reliably
43
+ tries = 0
44
+ begin
45
+ tries += 1
46
+ yield
47
+ rescue Redis::CannotConnectError
48
+ if tries < 30
49
+ sleep tries
50
+ retry
51
+ end
52
+ end
53
+ end
54
+
55
+ def self.all_queue_locks
56
+ Resque.redis.keys('queuelock:*')
57
+ end
58
+
59
+ def self.clear_all_queue_locks
60
+ all_queue_locks.each{ |queue_lock| Resque.redis.del(queue_lock) }
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-queue-lock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Nicholaides
9
+ - Jason Garber
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-03 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: resque
17
+ prerelease: false
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ none: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ none: false
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ prerelease: false
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ none: false
40
+ type: :development
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ none: false
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ prerelease: false
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.12.0
55
+ none: false
56
+ type: :development
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.12.0
62
+ none: false
63
+ - !ruby/object:Gem::Dependency
64
+ name: test-unit
65
+ prerelease: false
66
+ requirement: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ none: false
72
+ type: :development
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ none: false
79
+ description: ! "A Resque plugin. If you want only one instance of your job\nqueued
80
+ at a time, extend it with this module.\n\nFor example:\n\n class ExampleJob\n
81
+ \ extend Resque::Jobs::Queue::Lock\n\n def self.perform(repo_id)\n heavy_lifting\n
82
+ \ end\n end\n"
83
+ email: admin@mashion.net
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - README.md
89
+ - Rakefile
90
+ - LICENSE
91
+ - lib/resque/plugins/queue/lock.rb
92
+ homepage: http://github.com/mashion/resque-queue-lock
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ none: false
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ none: false
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.24
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: A Resque plugin for ensuring only one instance of your job is queued at a
116
+ time.
117
+ test_files: []