ealdent-resque-lock 0.1.2

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) 2010 Chris Wanstrath
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.
@@ -0,0 +1,48 @@
1
+ Resque Lock
2
+ ===========
3
+
4
+ A [Resque][rq] plugin. Requires Resque 1.7.0.
5
+
6
+ If you want only one instance of your job running at a time, extend it
7
+ with this module.
8
+
9
+
10
+ For example:
11
+
12
+ require 'resque/plugins/lock'
13
+
14
+ class UpdateNetworkGraph
15
+ extend Resque::Plugins::Lock
16
+
17
+ def self.perform(repo_id)
18
+ heavy_lifting
19
+ end
20
+ end
21
+
22
+ While other UpdateNetworkGraph jobs will be placed on the queue,
23
+ the Locked class will check Redis to see if any others are
24
+ executing with the same arguments before beginning. If another
25
+ is executing the job will be aborted.
26
+
27
+ If you want to define the key yourself you can override the
28
+ `lock` class method in your subclass, e.g.
29
+
30
+ class UpdateNetworkGraph
31
+ extend Resque::Plugins::Lock
32
+
33
+ Run only one at a time, regardless of repo_id.
34
+ def self.lock(repo_id)
35
+ "network-graph"
36
+ end
37
+
38
+ def self.perform(repo_id)
39
+ heavy_lifting
40
+ end
41
+ end
42
+
43
+ The above modification will ensure only one job of class
44
+ UpdateNetworkGraph is running at a time, regardless of the
45
+ repo_id. Normally a job is locked using a combination of its
46
+ class name and arguments.
47
+
48
+ [rq]: http://github.com/defunkt/resque
@@ -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-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,71 @@
1
+ module Resque
2
+ module Plugins
3
+ # If you want only one instance of your job running at a time,
4
+ # extend it with this module.
5
+ #
6
+ # For example:
7
+ #
8
+ # require 'resque/plugins/lock'
9
+ #
10
+ # class UpdateNetworkGraph
11
+ # extend Resque::Plugins::Lock
12
+ #
13
+ # def self.perform(repo_id)
14
+ # heavy_lifting
15
+ # end
16
+ # end
17
+ #
18
+ # While other UpdateNetworkGraph jobs will be placed on the queue,
19
+ # the Lock class will check Redis to see if any others are
20
+ # executing with the same arguments before beginning. If another
21
+ # is executing the job will be aborted.
22
+ #
23
+ # If you want to define the key yourself you can override the
24
+ # `lock` class method in your subclass, e.g.
25
+ #
26
+ # class UpdateNetworkGraph
27
+ # extend Resque::Plugins::Lock
28
+ #
29
+ # # Run only one at a time, regardless of repo_id.
30
+ # def self.lock(repo_id)
31
+ # "network-graph"
32
+ # end
33
+ #
34
+ # def self.perform(repo_id)
35
+ # heavy_lifting
36
+ # end
37
+ # end
38
+ #
39
+ # The above modification will ensure only one job of class
40
+ # UpdateNetworkGraph is running at a time, regardless of the
41
+ # repo_id. Normally a job is locked using a combination of its
42
+ # class name and arguments.
43
+ module Lock
44
+ # Override in your job to control the lock key. It is
45
+ # passed the same arguments as `perform`, that is, your job's
46
+ # payload.
47
+ def lock(*args)
48
+ "lock:#{name}-#{args.to_s}"
49
+ end
50
+
51
+ # Convenience method, not used internally.
52
+ def locked?(*args)
53
+ Resque.redis.exists(lock(*args))
54
+ end
55
+
56
+ # Where the magic happens.
57
+ def around_perform_lock(*args)
58
+ # Abort if another job has created a lock.
59
+ return unless Resque.redis.setnx(lock(*args), Time.now.utc)
60
+
61
+ begin
62
+ yield
63
+ ensure
64
+ # Always clear the lock when we're done, even if there is an
65
+ # error.
66
+ Resque.redis.del(lock(*args))
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,42 @@
1
+ require 'test/unit'
2
+ require 'resque'
3
+ require 'resque/plugins/lock'
4
+
5
+ $counter = 0
6
+
7
+ class Job
8
+ extend Resque::Plugins::Lock
9
+ @queue = :test
10
+
11
+ def self.perform
12
+ $counter += 1
13
+ sleep 1
14
+ end
15
+ end
16
+
17
+ class LockTest < Test::Unit::TestCase
18
+ def test_lint
19
+ assert_nothing_raised do
20
+ Resque::Plugin.lint(Resque::Plugins::Lock)
21
+ end
22
+ end
23
+
24
+ def test_version
25
+ major, minor, patch = Resque::Version.split('.')
26
+ assert_equal 1, major.to_i
27
+ assert minor.to_i >= 7
28
+ end
29
+
30
+ def test_lock
31
+ 3.times { Resque.enqueue(Job) }
32
+ worker = Resque::Worker.new(:test)
33
+
34
+ workers = []
35
+ 3.times do
36
+ workers << Thread.new { worker.process }
37
+ end
38
+ workers.each { |t| t.join }
39
+
40
+ assert_equal 1, $counter
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ealdent-resque-lock
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Chris Wanstrath
14
+ - Jason Adams
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-14 00:00:00 -04: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, extend it with this module. This version
26
+ stores the timestamp in the lock.
27
+
28
+ For example:
29
+
30
+ class UpdateNetworkGraph
31
+ extend Resque::Jobs::Locked
32
+
33
+ def self.perform(repo_id)
34
+ heavy_lifting
35
+ end
36
+ end
37
+
38
+ email: chris@ozmm.org
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - LICENSE
49
+ - lib/resque/plugins/lock.rb
50
+ - test/lock_test.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/ealdent/resque-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.
85
+ test_files: []
86
+