resque-jobs-per-fork 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ *.gemspec
21
+
22
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sam Granieri (Inspired by Mick Staugaard)
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,37 @@
1
+ = resque-jobs-per-fork
2
+
3
+ If you have very frequent and fast resque jobs, the overhead of forking and running your after_fork hook, might get too big. Using this resque plugin, you can have your workers perform more than one job, before terminating.
4
+
5
+ You simply specify the number of jobs you want each fork to run using the JOBS_PER_FORK environment variable:
6
+
7
+ QUEUE=* JOBS_PER_FORK=5 rake resque:work
8
+
9
+ You *have* to specify JOBS_PER_FORK environment variable or else your jobs won't run with this gem
10
+
11
+ This will have each fork 5 jobs, before terminating.
12
+
13
+ This plugin also defines 2 new hooks that get called after the fork starts and before the fork terminates
14
+
15
+ Resque.before_perform_jobs_per_fork do |worker|
16
+ worker.log("Your message here")
17
+ end
18
+
19
+ Resque.after_perform_jobs_per_fork do |worker|
20
+ worker.log("Your message here")
21
+ end
22
+
23
+ Version 0.3.0 works on Resque 1.8.x
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so I don't break it in a
30
+ future version unintentionally.
31
+ * Commit, do not mess with rakefile, version, or history.
32
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
33
+ * Send me a pull request. Bonus points for topic branches.
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2010 Sam Granieri (Inspired by Mick Staugaard). See LICENSE for details.
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "resque-jobs-per-fork"
8
+ gem.version = "0.3.0"
9
+ gem.summary = %Q{Have your resque workers process more that one job}
10
+ gem.description = %Q{When your resque jobs are frequent and fast,
11
+ the overhead of forking and running your after_fork might get too big.}
12
+ gem.email = "sam@samgranieri.com"
13
+ gem.homepage = "http://github.com/samgranieri/resque-jobs-per-fork"
14
+ gem.authors = ["Sam Granieri, Mick Staugaard"]
15
+ gem.add_dependency "resque", "~> 1.8.0"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "resque-jobs-per-fork #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
@@ -0,0 +1,55 @@
1
+ require 'resque'
2
+ require 'resque/worker'
3
+
4
+
5
+ module Resque
6
+
7
+ # the `before_perform_jobs_per_fork` hook will run in the child perform
8
+ # right before the child perform starts
9
+ #
10
+ # Call with a block to set the hook.
11
+ # Call with no arguments to return the hook.
12
+ def self.before_perform_jobs_per_fork(&block)
13
+ block ? (@before_perform_jobs_per_fork = block) : @before_perform_jobs_per_fork
14
+ end
15
+
16
+ # Set the before_perform_jobs_per_fork proc.
17
+ def self.before_perform_jobs_per_fork=(before_perform_jobs_per_fork)
18
+ @before_perform_jobs_per_fork = before_perform_jobs_per_fork
19
+ end
20
+
21
+ # the `after_perform_jobs_per_fork` hook will run in the child perform
22
+ # right before the child perform terminates
23
+ #
24
+ # Call with a block to set the hook.
25
+ # Call with no arguments to return the hook.
26
+ def self.after_perform_jobs_per_fork(&block)
27
+ block ? (@after_perform_jobs_per_fork = block) : @after_perform_jobs_per_fork
28
+ end
29
+
30
+ # Set the after_perform_jobs_per_fork proc.
31
+ def self.after_perform_jobs_per_fork=(after_perform_jobs_per_fork)
32
+ @after_perform_jobs_per_fork = after_perform_jobs_per_fork
33
+ end
34
+
35
+ class Worker
36
+
37
+ def perform_with_jobs_per_fork(job)
38
+ raise "You need to set JOBS_PER_FORK on the command line" unless ENV['JOBS_PER_FORK']
39
+ run_hook :before_perform_jobs_per_fork, self
40
+ jobs_performed ||= 0
41
+ while jobs_performed < ENV['JOBS_PER_FORK'].to_i do
42
+ if jobs_performed == 0
43
+ perform_without_jobs_per_fork(job)
44
+ elsif another_job = reserve
45
+ perform_without_jobs_per_fork(another_job)
46
+ end
47
+ jobs_performed += 1
48
+ end
49
+ jobs_performed = nil
50
+ run_hook :after_perform_jobs_per_fork, self
51
+ end
52
+ alias_method :perform_without_jobs_per_fork, :perform
53
+ alias_method :perform, :perform_with_jobs_per_fork
54
+ end
55
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $TESTING = true
7
+
8
+ require 'resque-jobs-per-fork'
@@ -0,0 +1,63 @@
1
+ require 'helper'
2
+
3
+ class SomeJob
4
+ def self.perform(i)
5
+ $SEQUENCE << "work_#{i}".to_sym
6
+ puts 'working...'
7
+ sleep(0.1)
8
+ end
9
+ end
10
+
11
+ Resque.before_perform_jobs_per_fork do |worker|
12
+ $SEQUENCE << "before_perform_jobs_per_fork".to_sym
13
+ end
14
+
15
+ Resque.after_perform_jobs_per_fork do |worker|
16
+ $SEQUENCE << "after_perform_jobs_per_fork".to_sym
17
+ end
18
+
19
+ class TestResqueMultiJobFork < Test::Unit::TestCase
20
+ def setup
21
+ $SEQUENCE = []
22
+ Resque.redis.flushdb
23
+ ENV['JOBS_PER_FORK'] = '2'
24
+ @worker = Resque::Worker.new(:jobs)
25
+ end
26
+
27
+ def test_one_job
28
+ Resque::Job.create(:jobs, SomeJob, 1)
29
+ @worker.work(0)
30
+ assert_equal([:before_perform_jobs_per_fork, :work_1, :after_perform_jobs_per_fork], $SEQUENCE)
31
+ end
32
+
33
+ def test_two_jobs
34
+ Resque::Job.create(:jobs, SomeJob, 1)
35
+ Resque::Job.create(:jobs, SomeJob, 2)
36
+ @worker.work(0)
37
+ assert_equal([:before_perform_jobs_per_fork, :work_1, :work_2, :after_perform_jobs_per_fork], $SEQUENCE)
38
+ end
39
+
40
+ def test_three_jobs
41
+ Resque::Job.create(:jobs, SomeJob, 1)
42
+ Resque::Job.create(:jobs, SomeJob, 2)
43
+ Resque::Job.create(:jobs, SomeJob, 3)
44
+ @worker.work(0)
45
+ assert_equal([:before_perform_jobs_per_fork, :work_1, :work_2, :after_perform_jobs_per_fork,
46
+ :before_perform_jobs_per_fork, :work_3, :after_perform_jobs_per_fork], $SEQUENCE)
47
+ end
48
+
49
+ def test_work_normally_if_env_var_set
50
+ assert_nothing_raised(RuntimeError) do
51
+ Resque::Job.create(:jobs, SomeJob, 1)
52
+ @worker.work(0)
53
+ end
54
+ end
55
+
56
+ def test_crash_if_no_env_var_set
57
+ ENV.delete('JOBS_PER_FORK')
58
+ assert_raise(RuntimeError) do
59
+ Resque::Job.create(:jobs, SomeJob, 1)
60
+ @worker.work(0)
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-jobs-per-fork
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
+ platform: ruby
11
+ authors:
12
+ - Sam Granieri, Mick Staugaard
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-29 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: resque
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 8
30
+ - 0
31
+ version: 1.8.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: |-
35
+ When your resque jobs are frequent and fast,
36
+ the overhead of forking and running your after_fork might get too big.
37
+ email: sam@samgranieri.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - LICENSE
44
+ - README.rdoc
45
+ files:
46
+ - .document
47
+ - .gitignore
48
+ - LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - lib/resque-jobs-per-fork.rb
52
+ - test/helper.rb
53
+ - test/test_resque-jobs-per-fork.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/samgranieri/resque-jobs-per-fork
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.6
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Have your resque workers process more that one job
84
+ test_files:
85
+ - test/helper.rb
86
+ - test/test_resque-jobs-per-fork.rb