resque-lonely_job 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resque-lonelyjob.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jonathan R. Wallace
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Resque::LonelyJob
2
+
3
+ A [Resque](https://github.com/defunkt/resque) plugin. Requires Resque 1.20.0.
4
+
5
+ Ensures that for a given queue, only one worker is working on a job at any given
6
+ time.
7
+
8
+ This differs from [resque-lock](from https://github.com/defunkt/resque-lock) and
9
+ [resque-loner](http://github.com/jayniz/resque-loner) in that the same job may
10
+ be queued multiple times but you're guaranteed that first job queued will run to
11
+ completion before subsequent jobs are run.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'resque-lonely_job'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install resque-lonely_job
26
+
27
+ ## Usage
28
+
29
+ Example #1:
30
+
31
+ require 'resque/plugins/lonely_job'
32
+
33
+ class StrictlySerialJob
34
+ extend Resque::Plugins::LonelyJob
35
+
36
+ @queue = :serial_work
37
+
38
+ def self.perform
39
+ # only one at a time in this block, no parallelism allowed for this
40
+ # particular queue
41
+ end
42
+ end
43
+
44
+ Example #2: Let's say you want the serial constraint to apply at a more granular
45
+ level. Instead of applying at the queue level, you can overwrite the .redis\_key
46
+ method.
47
+
48
+ require 'resque/plugins/lonely_job'
49
+
50
+ class StrictlySerialJob
51
+ extend Resque::Plugins::LonelyJob
52
+
53
+ @queue = :serial_work
54
+
55
+ # Returns a string that will be used as the redis key
56
+ # NOTE: it is recommended to prefix your string with the 'lonely_job:' to
57
+ # namespace your key!
58
+ def self.redis_key(account_id, *args)
59
+ "lonely_job:strictly_serial_job:#{account_id}"
60
+ end
61
+
62
+ def self.perform(account_id, *args)
63
+ # only one at a time in this block, no parallelism allowed for this
64
+ # particular queue
65
+ end
66
+ end
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
@@ -0,0 +1,7 @@
1
+ module Resque
2
+ module Plugins
3
+ module LonelyJob
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,45 @@
1
+ require 'resque-lonely_job/version'
2
+
3
+ module Resque
4
+ module Plugins
5
+ module LonelyJob
6
+ LOCK_TIMEOUT = 60 * 60 * 24 * 5 # 5 days
7
+
8
+ def lock_timeout
9
+ Time.now.utc + LOCK_TIMEOUT + 1
10
+ end
11
+
12
+ # Overwrite this method to uniquely identify which mutex should be used
13
+ # for a resque worker.
14
+ def redis_key(*args)
15
+ "lonely_job:#{@queue}"
16
+ end
17
+
18
+ def can_lock_queue?(*args)
19
+ Resque.redis.setnx(redis_key(*args), lock_timeout)
20
+ end
21
+
22
+ def unlock_queue(*args)
23
+ Resque.redis.del(redis_key(*args))
24
+ end
25
+
26
+ def before_perform(*args)
27
+ unless can_lock_queue?(*args)
28
+ # can't get the lock, so place self at the end of the queue
29
+ Resque.enqueue(self, *args)
30
+
31
+ # and don't perform
32
+ raise Resque::Job::DontPerform
33
+ end
34
+ end
35
+
36
+ def around_perform(*args)
37
+ begin
38
+ yield
39
+ ensure
40
+ unlock_queue(*args)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/resque-lonely_job/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jonathan R. Wallace"]
6
+ gem.email = ["jonathan.wallace@gmail.com"]
7
+ gem.summary = %q{A resque plugin that ensures that only one job for a given queue will be running on any worker at a given time.}
8
+ gem.homepage = "http://github.com/wallace/resque-lonely_job"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "resque-lonely_job"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Resque::Plugins::LonelyJob::VERSION
16
+
17
+ gem.add_dependency 'resque', '~> 1.20.0'
18
+ gem.add_dependency 'mock_redis', '~> 0.4.1'
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'rspec'
21
+
22
+ gem.description = <<desc
23
+ Ensures that for a given queue, only one worker is working on a job at any given time.
24
+
25
+ Example:
26
+
27
+ require 'resque/plugins/lonely_job'
28
+
29
+ class StrictlySerialJob
30
+ extend Resque::Plugins::LonelyJob
31
+
32
+ @queue = :serial_work
33
+
34
+ def self.perform
35
+ # only one at a time in this block, no parallelism allowed for this
36
+ # particular queue
37
+ end
38
+ end
39
+ desc
40
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ class SerialJob
4
+ extend Resque::Plugins::LonelyJob
5
+ @queue = :serial_work
6
+
7
+ def self.perform(*args); end
8
+ end
9
+
10
+ class SerialJobWithCustomRedisKey
11
+ extend Resque::Plugins::LonelyJob
12
+ @queue = :serial_work
13
+
14
+ def redis_key(account_id, *args)
15
+ "lonely_job:#{@queue}:#{account_id}"
16
+ end
17
+
18
+ def self.perform(account_id, *args); end
19
+ end
20
+
21
+ describe Resque::Plugins::LonelyJob do
22
+ before do
23
+ Resque.redis.flushall
24
+ end
25
+
26
+ describe ".can_lock_queue?" do
27
+ it 'can lock a queue' do
28
+ SerialJob.can_lock_queue?(:serial_work).should be_true
29
+ end
30
+
31
+ it 'cannot lock an already locked queue' do
32
+ SerialJob.can_lock_queue?(:serial_work).should be_true
33
+ SerialJob.can_lock_queue?(:serial_work).should be_false
34
+ end
35
+ end
36
+
37
+ describe ".perform" do
38
+ describe "using the default redis key" do
39
+ it 'should lock and unlock the queue' do
40
+ job = Resque::Job.new(:serial_work, { 'class' => 'SerialJob', 'args' => %w[account_one job_one] })
41
+
42
+ # job is the first SerialJob to run so it can lock the queue and perform
43
+ SerialJob.should_receive(:can_lock_queue?).and_return(true)
44
+
45
+ # but it should also clean up after itself
46
+ SerialJob.should_receive(:unlock_queue)
47
+
48
+ job.perform
49
+ end
50
+
51
+ it 'should clean up lock even with catastrophic job failure' do
52
+ job = Resque::Job.new(:serial_work, { 'class' => 'SerialJob', 'args' => %w[account_one job_one] })
53
+
54
+ # job is the first SerialJob to run so it can lock the queue and perform
55
+ SerialJob.should_receive(:can_lock_queue?).and_return(true)
56
+
57
+ # but we have a catastrophic job failure
58
+ SerialJob.should_receive(:perform).and_raise(Exception)
59
+
60
+ # and still it should clean up after itself
61
+ SerialJob.should_receive(:unlock_queue)
62
+
63
+ # unfortunately, the job will be lost but resque doesn't guarantee jobs
64
+ # aren't lost
65
+ -> { job.perform }.should raise_error(Exception)
66
+ end
67
+
68
+ it 'should place self at the end of the queue if unable to acquire the lock' do
69
+ Resque.size(:serial_work).should == 0
70
+
71
+ job = Resque::Job.new(:serial_work, { 'class' => 'SerialJob', 'args' => %w[account_one job_one] })
72
+
73
+ SerialJob.should_receive(:can_lock_queue?).and_return(false)
74
+
75
+ # perform returns false when DontPerform exception is raised in
76
+ # before_perform callback
77
+ job.perform.should be_false
78
+
79
+ Resque.size(:serial_work).should == 1
80
+ end
81
+ end
82
+
83
+ describe "with a custom redis_key" do
84
+ it 'should lock and unlock the queue' do
85
+ job = Resque::Job.new(:serial_work, { 'class' => 'SerialJobWithCustomRedisKey', 'args' => %w[account_one job_one] })
86
+
87
+ # job is the first SerialJobWithCustomRedisKey to run so it can lock the queue and perform
88
+ SerialJobWithCustomRedisKey.should_receive(:can_lock_queue?).and_return(true)
89
+
90
+ # but it should also clean up after itself
91
+ SerialJobWithCustomRedisKey.should_receive(:unlock_queue)
92
+
93
+ job.perform
94
+ end
95
+
96
+ it 'should clean up lock even with catastrophic job failure' do
97
+ job = Resque::Job.new(:serial_work, { 'class' => 'SerialJobWithCustomRedisKey', 'args' => %w[account_one job_one] })
98
+
99
+ # job is the first SerialJobWithCustomRedisKey to run so it can lock the queue and perform
100
+ SerialJobWithCustomRedisKey.should_receive(:can_lock_queue?).and_return(true)
101
+
102
+ # but we have a catastrophic job failure
103
+ SerialJobWithCustomRedisKey.should_receive(:perform).and_raise(Exception)
104
+
105
+ # and still it should clean up after itself
106
+ SerialJobWithCustomRedisKey.should_receive(:unlock_queue)
107
+
108
+ # unfortunately, the job will be lost but resque doesn't guarantee jobs
109
+ # aren't lost
110
+ -> { job.perform }.should raise_error(Exception)
111
+ end
112
+
113
+ it 'should place self at the end of the queue if unable to acquire the lock' do
114
+ Resque.size(:serial_work).should == 0
115
+
116
+ job = Resque::Job.new(:serial_work, { 'class' => 'SerialJobWithCustomRedisKey', 'args' => %w[account_one job_one] })
117
+
118
+ SerialJobWithCustomRedisKey.should_receive(:can_lock_queue?).and_return(false)
119
+
120
+ # perform returns false when DontPerform exception is raised in
121
+ # before_perform callback
122
+ job.perform.should be_false
123
+
124
+ Resque.size(:serial_work).should == 1
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+
5
+ require 'mock_redis'
6
+ require 'resque'
7
+ require 'resque-lonely_job'
8
+
9
+ RSpec.configure do |config|
10
+ config.before(:suite) do
11
+ Resque.redis = MockRedis.new
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-lonely_job
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonathan R. Wallace
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: resque
16
+ requirement: &70323053547940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.20.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70323053547940
25
+ - !ruby/object:Gem::Dependency
26
+ name: mock_redis
27
+ requirement: &70323053547380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.4.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70323053547380
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70323053546980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70323053546980
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70323053546520 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70323053546520
58
+ description: ! "Ensures that for a given queue, only one worker is working on a job
59
+ at any given time.\n\nExample:\n\n require 'resque/plugins/lonely_job'\n\n class
60
+ StrictlySerialJob\n extend Resque::Plugins::LonelyJob\n\n @queue = :serial_work\n\n
61
+ \ def self.perform\n # only one at a time in this block, no parallelism allowed
62
+ for this\n # particular queue\n end\n end\n"
63
+ email:
64
+ - jonathan.wallace@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - lib/resque-lonely_job.rb
75
+ - lib/resque-lonely_job/version.rb
76
+ - resque-lonely_job.gemspec
77
+ - spec/lib/lonely_job_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: http://github.com/wallace/resque-lonely_job
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.17
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A resque plugin that ensures that only one job for a given queue will be
103
+ running on any worker at a given time.
104
+ test_files:
105
+ - spec/lib/lonely_job_spec.rb
106
+ - spec/spec_helper.rb
107
+ has_rdoc: