Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2621f66
Showing
21 changed files
with
1,025 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
README.rdoc | ||
lib/**/*.rb | ||
bin/* | ||
features/**/*.feature | ||
LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## MAC OS | ||
.DS_Store | ||
|
||
## TEXTMATE | ||
*.tmproj | ||
tmtags | ||
|
||
## EMACS | ||
*~ | ||
\#* | ||
.\#* | ||
|
||
## VIM | ||
*.swp | ||
|
||
## PROJECT::GENERAL | ||
coverage | ||
rdoc | ||
pkg | ||
|
||
## PROJECT::SPECIFIC | ||
*.log | ||
*.pid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2009 Daniel Cadenas | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
= preforker | ||
|
||
A gem to easily create prefork servers. | ||
|
||
== Example | ||
|
||
Let's see an example using the Mac 'say' command. | ||
|
||
require 'preforker' | ||
|
||
#you can open some socket here or reserve any other resource you want to share with your workers, this is master space | ||
Preforker.new(:timeout => 10) do |master| | ||
#this block is only run from each worker (10 by default) | ||
|
||
#here you should write the code that is needed to be ran each time a fork is created, initializations, etc. | ||
`say hi` | ||
|
||
#here you could IO.select a socket, run an EventMachine service (see example), or just run worker loop | ||
#you need to ask master if it wants you alive periodically or else it will kill you after the timeout elapses. Respect your master! | ||
while master.wants_me_alive? do | ||
sleep 1 | ||
`say ping pong` | ||
end | ||
|
||
#here we can do whatever we want when exiting gracefully | ||
`say bye` | ||
end.start | ||
|
||
To kill the server just run: | ||
|
||
kill -s QUIT `cat preforker.pid` | ||
|
||
See the examples directory and the specs for more examples. | ||
|
||
== Configuration options | ||
|
||
[:timeout] The timeout in seconds, 5 by default. If a worker takes more than this it will be killed and respawned. | ||
[:workers] Number of workers, 10 by default. | ||
[:stdout_path] Path to redirect stdout to. By default it's /dev/null | ||
[:stderr_path] Path to redirect stderr to. By default it's /dev/null | ||
[:app_name] The app name, 'preforker' by default. Used for some ps message, log messages messages and pid file name. | ||
[:pid_path] The path to the pid file for this server. By default it's './preforker.pid'. | ||
[:logger] This is Logger.new('./preforker.log') by default | ||
|
||
== Signals | ||
|
||
You can send some signals to master to control the way it handles the workers lifetime. | ||
|
||
[WINCH] Gracefully kill all workers but keep master alive | ||
[TTIN] Increase number of workers | ||
[TTOU] Decrease number of workers | ||
[QUIT] Kill workers and master in a graceful way | ||
[TERM, INT] Kill workers and master immediately | ||
|
||
== Acknowledgments | ||
|
||
Most of the preforking operating system tricks come from Unicorn. Check out its source code for a hands on tutorial on Unix internals! | ||
|
||
== To do list | ||
|
||
* More tests | ||
* Log rotation through the USR1 signal | ||
* Have something like min_spare_workers, max_workers | ||
|
||
== Note on Patches/Pull Requests | ||
|
||
* Fork the project. | ||
* Make your feature addition or bug fix. | ||
* Add tests for it. This is important so I don't break it in a | ||
future version unintentionally. | ||
* Commit, do not mess with rakefile, version, or history. | ||
(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) | ||
* Send me a pull request. Bonus points for topic branches. | ||
|
||
== Copyright | ||
|
||
Copyright (c) 2010 Daniel Cadenas. See LICENSE for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'rubygems' | ||
require 'rake' | ||
|
||
begin | ||
require 'jeweler' | ||
Jeweler::Tasks.new do |gem| | ||
gem.name = "preforker" | ||
gem.summary = %Q{A gem to easily create prefork servers.} | ||
gem.description = %Q{A gem to easily create prefork servers.} | ||
gem.email = "dcadenas@gmail.com" | ||
gem.homepage = "http://github.com/dcadenas/preforker" | ||
gem.authors = ["Daniel Cadenas"] | ||
gem.add_development_dependency "rspec", ">= 1.3.0" | ||
gem.add_development_dependency "filetesthelper", ">= 0.10.1" | ||
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings | ||
end | ||
Jeweler::GemcutterTasks.new | ||
rescue LoadError | ||
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" | ||
end | ||
|
||
require 'spec/rake/spectask' | ||
Spec::Rake::SpecTask.new(:spec) do |spec| | ||
spec.libs << 'lib' << 'spec' | ||
spec.spec_files = FileList['spec/**/*_spec.rb'] | ||
end | ||
|
||
Spec::Rake::SpecTask.new(:rcov) do |spec| | ||
spec.libs << 'lib' << 'spec' | ||
spec.pattern = 'spec/**/*_spec.rb' | ||
spec.rcov = true | ||
end | ||
|
||
task :spec => :check_dependencies | ||
|
||
task :default => :spec | ||
|
||
require 'rake/rdoctask' | ||
Rake::RDocTask.new do |rdoc| | ||
version = File.exist?('VERSION') ? File.read('VERSION') : "" | ||
|
||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = "preforker #{version}" | ||
rdoc.rdoc_files.include('README*') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'rubygems' | ||
require 'preforker' | ||
require 'mq' | ||
|
||
#be sure to run your AMQP server first | ||
|
||
Preforker.new(:timeout => 5, :workers => 4, :app_name => "Amqp example") do |master| | ||
AMQP.start(:host => 'dcadenas-laptop.local') do | ||
EM.add_periodic_timer(1) do | ||
AMQP.stop{ EM.stop } unless master.wants_me_alive? | ||
end | ||
|
||
MQ.prefetch(1) | ||
channel = MQ.new | ||
test_exchange = channel.fanout('test_exchange') | ||
channel.queue('test_queue').bind(test_exchange).subscribe(:ack => true) do |h, msg| | ||
$stdout.puts "#{$$} received #{msg.inspect}" | ||
h.ack | ||
end | ||
end | ||
end.start | ||
|
||
#run examples/amqp_client.rb to see the output | ||
#kill server with: kill -s TERM `cat 'amqp example.pid'` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'rubygems' | ||
require 'bunny' | ||
|
||
Bunny.run(:host => 'dcadenas-laptop.local', :port => 5672, :user => 'guest', :password => 'guest') do |b| | ||
e = b.exchange('test_exchange', :type => :fanout) | ||
|
||
25.times do |i| | ||
e.publish("number #{i}") | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'preforker' | ||
|
||
#you can open some socket here or reserve any other resource you want to share with your workers, this is master space | ||
Preforker.new(:timeout => 10) do |master| | ||
#this block is only run from each worker (10 by default) | ||
|
||
#here you should write the code that is needed to be ran each time a fork is created, initializations, etc. | ||
`say hi` | ||
|
||
#here you could IO.select a socket, run an EventMachine service (see example), or just run worker loop | ||
#you need to ask master if it wants you alive periodically or else it will kill you after the timeout elapses. Respect your master! | ||
while master.wants_me_alive? do | ||
sleep 1 | ||
`say ping pong` | ||
end | ||
|
||
#here we can do whatever we want when exiting gracefully | ||
`say bye` | ||
end.start | ||
|
||
#to kill the server just run: kill -s QUIT `cat preforker.pid` |
Oops, something went wrong.