resque-multi-job-forks 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -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) 2009 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.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = resque-multi-job-forks
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 simple specify the number of jobs to perform using the JOBS_PER_FORK environment variable:
6
+
7
+ QUEUE=* JOBS_PER_FORK=100 rake resque:work
8
+
9
+ This will have each fork process 100 jobs, before terminating. If less than 100 jobs are enqueued, the fork will terminate when the queue is empty.
10
+
11
+ This plugin also defines a new hook, that gets called right before the fork terminates:
12
+
13
+ Resque.before_child_exit do |worker|
14
+ worker.log("#{worker.jobs_processed} were processed in this fork")
15
+ end
16
+
17
+
18
+ == Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (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)
26
+ * Send me a pull request. Bonus points for topic branches.
27
+
28
+ == Copyright
29
+
30
+ Copyright (c) 2010 Mick Staugaard. See LICENSE for details.
data/Rakefile ADDED
@@ -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-multi-job-forks"
8
+ gem.version = "0.1.2"
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 = "mick@staugaard.com"
13
+ gem.homepage = "http://github.com/staugaard/resque-multi-job-forks"
14
+ gem.authors = ["Mick Staugaard"]
15
+ gem.add_dependency "resque", ">= 1.6.0", "< 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-multi-job-forks #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
@@ -0,0 +1,50 @@
1
+ require 'resque'
2
+ require 'resque/worker'
3
+
4
+ module Resque
5
+ # the `before_child_exit` hook will run in the child process
6
+ # right before the child process terminates
7
+ #
8
+ # Call with a block to set the hook.
9
+ # Call with no arguments to return the hook.
10
+ def self.before_child_exit(&block)
11
+ block ? (@before_child_exit = block) : @before_child_exit
12
+ end
13
+
14
+ # Set the before_child_exit proc.
15
+ def self.before_child_exit=(before_child_exit)
16
+ @before_child_exit = before_child_exit
17
+ end
18
+
19
+ class Worker
20
+ attr_accessor :jobs_per_fork
21
+ attr_reader :jobs_processed
22
+
23
+ unless method_defined?(:done_working_without_multi_job_forks)
24
+ def done_working_with_multi_job_forks
25
+ done_working_without_multi_job_forks
26
+
27
+ self.jobs_per_fork ||= [ ENV['JOBS_PER_FORK'].to_i, 1 ].max
28
+ @jobs_processed ||= 0
29
+
30
+ @jobs_processed += 1
31
+
32
+ if @jobs_processed == 1
33
+ old_after_fork = Resque.after_fork
34
+ Resque.after_fork = nil
35
+
36
+ while @jobs_processed < jobs_per_fork && job = reserve
37
+ process(job)
38
+ end
39
+
40
+ Resque.after_fork = old_after_fork
41
+
42
+ run_hook :before_child_exit, self
43
+ @jobs_processed = 0
44
+ end
45
+ end
46
+ alias_method :done_working_without_multi_job_forks, :done_working
47
+ alias_method :done_working, :done_working_with_multi_job_forks
48
+ end
49
+ end
50
+ end
data/test/helper.rb ADDED
@@ -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-multi-job-forks'
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+
3
+ class SomeJob
4
+ def self.perform(i)
5
+ $SEQUENCE << "work_#{i}".to_sym
6
+ end
7
+ end
8
+
9
+ Resque.after_fork do
10
+ $SEQUENCE << :after_fork
11
+ end
12
+
13
+ Resque.before_child_exit do |worker|
14
+ $SEQUENCE << "before_child_exit_#{worker.jobs_processed}".to_sym
15
+ end
16
+
17
+ class TestResqueMultiJobForks < Test::Unit::TestCase
18
+ def setup
19
+ $SEQUENCE = []
20
+ end
21
+
22
+ def test_sequence_of_events
23
+ Resque.redis.flush_all
24
+
25
+ ENV['JOBS_PER_FORK'] = '2'
26
+
27
+ worker = Resque::Worker.new(:jobs)
28
+
29
+ Resque::Job.create(:jobs, SomeJob, 1)
30
+ Resque::Job.create(:jobs, SomeJob, 2)
31
+ Resque::Job.create(:jobs, SomeJob, 3)
32
+
33
+ worker.work(0)
34
+
35
+ assert_equal([:after_fork, :work_1, :work_2, :before_child_exit_2, :after_fork, :work_3, :before_child_exit_1], $SEQUENCE)
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-multi-job-forks
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Mick Staugaard
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-07 00:00:00 -07: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
+ - 6
30
+ - 0
31
+ version: 1.6.0
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ segments:
35
+ - 1
36
+ - 8
37
+ - 0
38
+ version: 1.8.0
39
+ type: :runtime
40
+ version_requirements: *id001
41
+ description: |-
42
+ When your resque jobs are frequent and fast,
43
+ the overhead of forking and running your after_fork might get too big.
44
+ email: mick@staugaard.com
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - LICENSE
51
+ - README.rdoc
52
+ files:
53
+ - .document
54
+ - .gitignore
55
+ - LICENSE
56
+ - README.rdoc
57
+ - Rakefile
58
+ - lib/resque-multi-job-forks.rb
59
+ - test/helper.rb
60
+ - test/test_resque-multi-job-forks.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/staugaard/resque-multi-job-forks
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Have your resque workers process more that one job
91
+ test_files:
92
+ - test/helper.rb
93
+ - test/test_resque-multi-job-forks.rb