hupper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *~
2
+ /.rspec
3
+ /Gemfile.lock
4
+ *.gem
5
+ .bundle
6
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in Hupper.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hupper"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hupper"
7
+ s.version = Hupper::VERSION
8
+ s.authors = ["Alexander Staubo"]
9
+ s.email = ["alex@origo.no"]
10
+ s.homepage = ""
11
+ s.summary = s.description = %q{Hupper is a tiny library that provides a generic mechanism to execute snippets of code before and after forking processes.}
12
+
13
+ s.rubyforge_project = "hupper"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rspec"
21
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright © 2011 Alexander Staubo
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
@@ -0,0 +1,60 @@
1
+ Hupper
2
+ ======
3
+
4
+ Hupper is a tiny library that provides a generic mechanism to execute snippets of code before and after forking processes.
5
+
6
+ According to Unix semantics, files and sockets must be closed and reopened when passing the boundary between the parent and the child process.
7
+
8
+ Hupper lets an application or library to fork its process without having to know about all such dependencies.
9
+
10
+ Hupper is ideally combined with [Unicorn](http://unicorn.bogomips.org/). See below for an example.
11
+
12
+ Typical usage
13
+ -------------
14
+
15
+ Here's an example that controls ActiveRecord's connection:
16
+
17
+ Hupper.on_initialize do
18
+ ActiveRecord::Base.establish_connection
19
+ end
20
+
21
+ Hupper.on_release do
22
+ ActiveRecord::Base.connection.disconnect!
23
+ end
24
+
25
+ Now you can do:
26
+
27
+ Hupper.release!
28
+ Process.fork do
29
+ Hupper.initialize!
30
+ #
31
+ # ... implement child process
32
+ #
33
+ end
34
+
35
+ Using with Unicorn
36
+ ------------------
37
+
38
+ In your `unicorn.rb` configuration file, put:
39
+
40
+ before_fork do |server, worker|
41
+ Hupper.release!
42
+ end
43
+
44
+ after_fork do |server, worker|
45
+ Hupper.initialize!
46
+ end
47
+
48
+ Using SIGHUP
49
+ ------------
50
+
51
+ In order to respond to the standard Unix signal `SIGHUP`:
52
+
53
+ Hupper.hook_signal!
54
+
55
+ When the application receives `SIGHUP`, the release handles will be called first, followed by the initialize handlers.
56
+
57
+ License
58
+ -------
59
+
60
+ Licensed under the MIT license. See `LICENSE` file.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,62 @@
1
+ require 'thread'
2
+
3
+ # Hupper is a simple mechanism
4
+ module Hupper
5
+
6
+ VERSION = '0.0.1'
7
+
8
+ class << self
9
+
10
+ # Register an initialization block and call it.
11
+ def on_initialize(&block)
12
+ MUTEX.synchronize do
13
+ @@on_initialize_blocks.push(block)
14
+ end
15
+ return yield block
16
+ end
17
+
18
+ # Register a shutdown block and call it.
19
+ def on_release(&block)
20
+ MUTEX.synchronize do
21
+ @@on_release_blocks.push(block)
22
+ end
23
+ nil
24
+ end
25
+
26
+ def initialize!
27
+ MUTEX.synchronize { @@on_initialize_blocks.dup }.each do |block|
28
+ block.call
29
+ end
30
+ end
31
+
32
+ def release!
33
+ MUTEX.synchronize { @@on_release_blocks.dup }.each do |block|
34
+ block.call
35
+ end
36
+ end
37
+
38
+ # Hooks +SIGHUP+ signal.
39
+ def hook_signal!(signal_name = 'HUP')
40
+ unless @@previous_hup_handler
41
+ @@current_hup_handler = proc {
42
+ release!
43
+ initialize!
44
+ if @@previous_hup_handler.is_a?(Proc) and @@previous_hup_handler != @@current_hup_handler
45
+ @@previous_hup_handler.call
46
+ end
47
+ }
48
+ @@previous_hup_handler = Signal.trap(signal_name, &@@current_hup_handler)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ @@on_initialize_blocks ||= []
55
+ @@on_release_blocks ||= []
56
+ @@previous_hup_handler ||= nil
57
+
58
+ MUTEX = Mutex.new
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hupper do
4
+
5
+ describe "#on_initialize" do
6
+ it 'returns block result' do
7
+ Hupper.on_initialize { 42 }.should == 42
8
+ end
9
+
10
+ it 'runs block' do
11
+ list = []
12
+ Hupper.on_initialize { list << 42 }
13
+ list.should == [42]
14
+ end
15
+ end
16
+
17
+ describe "#on_release" do
18
+ it 'returns nil' do
19
+ Hupper.on_release { 42 }.should == nil
20
+ end
21
+
22
+ it 'does not run block' do
23
+ list = []
24
+ Hupper.on_release { list << 42 }
25
+ list.should == []
26
+ end
27
+ end
28
+
29
+ describe "#initialize!" do
30
+ it 'works with no blocks' do
31
+ lambda { Hupper.initialize! }.should_not raise_error
32
+ end
33
+
34
+ it 'runs registered blocks' do
35
+ list = []
36
+ Hupper.on_initialize { list << 42 }
37
+ Hupper.initialize!
38
+ list.should == [42, 42]
39
+ end
40
+ end
41
+
42
+ describe "#release!" do
43
+ it 'works with no blocks' do
44
+ lambda { Hupper.release! }.should_not raise_error
45
+ end
46
+
47
+ it 'runs registered blocks' do
48
+ block = proc { }
49
+ block.should_receive(:call).exactly(1).times
50
+ Hupper.on_release(&block)
51
+ Hupper.release!
52
+ end
53
+ end
54
+
55
+ describe "#hook_signal!" do
56
+ it 'hooks signal' do
57
+ Signal.should_receive(:trap).with('HUP')
58
+ Hupper.hook_signal!
59
+ end
60
+
61
+ it 'calls #initialize! and #release! on signal' do
62
+ Hupper.class_variable_set('@@previous_hup_handler', nil)
63
+ Hupper.class_variable_set('@@current_hup_handler', nil)
64
+
65
+ registered_block = nil
66
+ Signal.should_receive(:trap) { |*args, &block|
67
+ registered_block = block
68
+ }
69
+
70
+ Hupper.hook_signal!
71
+
72
+ Hupper.should_receive(:initialize!).once
73
+ Hupper.should_receive(:release!).once
74
+ registered_block.call
75
+ end
76
+
77
+ it 'chains previous signal handler' do
78
+ Hupper.class_variable_set('@@previous_hup_handler', nil)
79
+ Hupper.class_variable_set('@@current_hup_handler', nil)
80
+
81
+ previous_block = proc { }
82
+ Signal.trap('HUP', &previous_block)
83
+
84
+ registered_block = nil
85
+ Signal.should_receive(:trap) { |*args, &block|
86
+ registered_block = block
87
+ previous_block
88
+ }
89
+ Hupper.hook_signal!
90
+
91
+ previous_block.should_receive(:call).once
92
+ registered_block.call
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'hupper'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hupper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Alexander Staubo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-02 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Hupper is a tiny library that provides a generic mechanism to execute snippets of code before and after forking processes.
35
+ email:
36
+ - alex@origo.no
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Hupper.gemspec
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - lib/hupper.rb
51
+ - spec/hupper_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: ""
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: hupper
82
+ rubygems_version: 1.8.6
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Hupper is a tiny library that provides a generic mechanism to execute snippets of code before and after forking processes.
86
+ test_files:
87
+ - spec/hupper_spec.rb
88
+ - spec/spec_helper.rb