resque-batchworker 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +35 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/resque-batchworker.rb +3 -0
- data/lib/resque/plugins/batchworker.rb +93 -0
- data/lib/resque/plugins/safe_fork.rb +20 -0
- data/resque-batchworker.gemspec +56 -0
- data/test/helper.rb +22 -0
- data/test/test_resque-batchworker.rb +20 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ian Warshak
|
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,35 @@
|
|
1
|
+
= resque-batchworker
|
2
|
+
|
3
|
+
Works with Resque 1.8.5
|
4
|
+
|
5
|
+
Resque::Plugins::Batchworker is an alternative to the standard Worker class.
|
6
|
+
|
7
|
+
This class works by forking X children, which all work until there are no more jobs
|
8
|
+
to run, and then all of the children quit. Note that the traditional Worker
|
9
|
+
will fork for every single job. This class will not do that. Keep that in mind.
|
10
|
+
|
11
|
+
This was written for a situation where hundreds of thousands of jobs get queued
|
12
|
+
throughout the day but ONLY wanted them to be processed during off-peak hours
|
13
|
+
(i.e. middle of the night)
|
14
|
+
|
15
|
+
To use Batchworker, run this:
|
16
|
+
require 'resque/plugins/batchworker'
|
17
|
+
Resque::Plugins::Batchworker.initiate_work NUMBER_OF_PROCESSES, QUEUE_NAME
|
18
|
+
|
19
|
+
|
20
|
+
Thanks to Facilities Technology Group for allowing this code to be released
|
21
|
+
publicly.
|
22
|
+
|
23
|
+
== Note on Patches/Pull Requests
|
24
|
+
|
25
|
+
* Fork the project.
|
26
|
+
* Make your feature addition or bug fix.
|
27
|
+
* Add tests for it. This is important so I don't break it in a
|
28
|
+
future version unintentionally.
|
29
|
+
* Commit, do not mess with rakefile, version, or history.
|
30
|
+
(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)
|
31
|
+
* Send me a pull request. Bonus points for topic branches.
|
32
|
+
|
33
|
+
== Copyright
|
34
|
+
|
35
|
+
Copyright (c) 2010 Ian Warshak. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "resque-batchworker"
|
8
|
+
gem.summary = %Q{Resque plugin for batch processing of jobs}
|
9
|
+
gem.description = %Q{Forks X children, processes all jobs in the queue and all processes quit when there are no more jobs to complete}
|
10
|
+
gem.email = "iwarshak@stripey.net"
|
11
|
+
gem.homepage = "http://github.com/iwarshak/resque-batchworker"
|
12
|
+
gem.authors = ["Ian Warshak"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "resque-batchworker #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Resque
|
2
|
+
module Plugins
|
3
|
+
class Batchworker < Resque::Worker
|
4
|
+
include Resque::Helpers
|
5
|
+
extend Resque::Helpers
|
6
|
+
|
7
|
+
def work(worker_number, &block)
|
8
|
+
# This runs in a child process
|
9
|
+
$0 = "resque: Starting"
|
10
|
+
run_hook :after_fork
|
11
|
+
prune_dead_workers
|
12
|
+
register_worker
|
13
|
+
|
14
|
+
@logger = Logger.new("batchworker.log")
|
15
|
+
@logger.debug("about to start a forked worker with count #{worker_number}")
|
16
|
+
@logger.debug("inside safe_fork #{Process.pid}")
|
17
|
+
job_counter = 0
|
18
|
+
begin
|
19
|
+
@logger.debug("about to register worker #{Process.pid}")
|
20
|
+
@logger.debug("#{Process.pid } registered worker! about to look for a job")
|
21
|
+
while job = reserve
|
22
|
+
@logger.info("#{Process.pid } found job #{job}")
|
23
|
+
working_on(job)
|
24
|
+
procline "#{self} Processing #{job.queue} since #{Time.now.to_i} number:#{worker_number}"
|
25
|
+
perform(job, &block)
|
26
|
+
job_counter += 1
|
27
|
+
end
|
28
|
+
ensure
|
29
|
+
@logger.info "#{self} processed #{job_counter} jobs"
|
30
|
+
@logger.info "#{Process.pid } unregistering worker #{self}"
|
31
|
+
unregister_worker
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def self.initiate_work(count, queue, &block)
|
37
|
+
count ||= 1
|
38
|
+
self.new(queue.to_sym).startup # Want this to be called before forking
|
39
|
+
|
40
|
+
safe_fork(count.to_i) do |worker_number|
|
41
|
+
worker = self.new(queue.to_sym)
|
42
|
+
worker.work(worker_number)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def perform(job = nil)
|
47
|
+
return unless job ||= reserve
|
48
|
+
|
49
|
+
begin
|
50
|
+
# We don't want this called here because we only want it called once.
|
51
|
+
# run_hook :after_fork, job
|
52
|
+
@logger.error("#{Process.pid} performing #{job}")
|
53
|
+
job.perform
|
54
|
+
rescue Object => e
|
55
|
+
log "#{job.inspect} failed: #{e.inspect}"
|
56
|
+
job.fail(e)
|
57
|
+
failed!
|
58
|
+
else
|
59
|
+
log "done: #{job.inspect}"
|
60
|
+
ensure
|
61
|
+
yield job if block_given?
|
62
|
+
done_working
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
undef fork if method_defined?(:fork)
|
67
|
+
|
68
|
+
def startup
|
69
|
+
enable_gc_optimizations
|
70
|
+
# register_signal_handlers # This won't work, so remove it.
|
71
|
+
run_hook :before_first_fork
|
72
|
+
run_hook :before_fork
|
73
|
+
|
74
|
+
# Fix buffering so we can `rake resque:work > resque.log` and
|
75
|
+
# get output from the child in there.
|
76
|
+
$stdout.sync = true
|
77
|
+
end
|
78
|
+
|
79
|
+
def prune_dead_workers
|
80
|
+
all_workers = self.class.all # the only change here.
|
81
|
+
known_workers = worker_pids unless all_workers.empty?
|
82
|
+
all_workers.each do |worker|
|
83
|
+
host, pid, queues = worker.id.split(':')
|
84
|
+
next unless host == hostname
|
85
|
+
next if known_workers.include?(pid)
|
86
|
+
log! "Pruning dead worker: #{worker}"
|
87
|
+
worker.unregister_worker
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'resque'
|
2
|
+
require 'active_support'
|
3
|
+
|
4
|
+
module Kernel
|
5
|
+
def safe_fork(count = 1)
|
6
|
+
if Object.const_defined?('ActiveRecord') && ActiveRecord.const_defined?('Base')
|
7
|
+
ActiveRecord::Base.lock_optimistically = false
|
8
|
+
ActiveRecord::Base.connection_pool.disconnect!
|
9
|
+
end
|
10
|
+
|
11
|
+
count.times do |i|
|
12
|
+
fork do
|
13
|
+
Resque.redis.connect_to_server if Object.const_defined?('Resque')
|
14
|
+
yield(i) if block_given?
|
15
|
+
exit!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
Process.waitall
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{resque-batchworker}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ian Warshak"]
|
12
|
+
s.date = %q{2010-06-04}
|
13
|
+
s.description = %q{Forks X children, processes all jobs in the queue and all processes quit when there are no more jobs to complete}
|
14
|
+
s.email = %q{iwarshak@stripey.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/resque-batchworker.rb",
|
27
|
+
"lib/resque/plugins/batchworker.rb",
|
28
|
+
"lib/resque/plugins/safe_fork.rb",
|
29
|
+
"resque-batchworker.gemspec",
|
30
|
+
"test/helper.rb",
|
31
|
+
"test/test_resque-batchworker.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/iwarshak/resque-batchworker}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
37
|
+
s.summary = %q{Resque plugin for batch processing of jobs}
|
38
|
+
s.test_files = [
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_resque-batchworker.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'redis'
|
5
|
+
require 'active_support'
|
6
|
+
require 'resque'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
+
require 'resque-batchworker'
|
11
|
+
|
12
|
+
class Test::Unit::TestCase
|
13
|
+
end
|
14
|
+
|
15
|
+
class BatchJob
|
16
|
+
@queue = :test
|
17
|
+
|
18
|
+
def self.perform(args)
|
19
|
+
r = Redis.new
|
20
|
+
r.rpush :batch_job_test, Process.pid
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestResqueBatchworker < Test::Unit::TestCase
|
4
|
+
should "be a valid plugin" do
|
5
|
+
assert_nothing_raised do
|
6
|
+
Resque::Plugin.lint(Resque::Plugins::Batchworker)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
should "process 2 jobs with 2 workers" do
|
10
|
+
4.times {|i| Resque.enqueue(BatchJob, i)}
|
11
|
+
Resque::Plugins::Batchworker.initiate_work 2, :test
|
12
|
+
redis = Redis.new
|
13
|
+
pids = []
|
14
|
+
while pid = redis.rpop(:batch_job_test)
|
15
|
+
pids << pid
|
16
|
+
end
|
17
|
+
assert_equal pids.size, 4
|
18
|
+
assert_equal pids.uniq.size, 2
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-batchworker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ian Warshak
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-04 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: thoughtbot-shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: Forks X children, processes all jobs in the queue and all processes quit when there are no more jobs to complete
|
33
|
+
email: iwarshak@stripey.net
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- LICENSE
|
40
|
+
- README.rdoc
|
41
|
+
files:
|
42
|
+
- .document
|
43
|
+
- .gitignore
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
- Rakefile
|
47
|
+
- VERSION
|
48
|
+
- lib/resque-batchworker.rb
|
49
|
+
- lib/resque/plugins/batchworker.rb
|
50
|
+
- lib/resque/plugins/safe_fork.rb
|
51
|
+
- resque-batchworker.gemspec
|
52
|
+
- test/helper.rb
|
53
|
+
- test/test_resque-batchworker.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/iwarshak/resque-batchworker
|
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: Resque plugin for batch processing of jobs
|
84
|
+
test_files:
|
85
|
+
- test/helper.rb
|
86
|
+
- test/test_resque-batchworker.rb
|