unicorn-timeout 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unicorn-timeout.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Anders Carling
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # Unicorn::Timeout
2
+
3
+ Middleware for timing out current unicorn worker if request takes to long time.
4
+
5
+ If a request times out, a handler block will be called and the current worker
6
+ process will be killed (using SIGTERM).
7
+
8
+ ## Compatibility
9
+
10
+ The gem is tested using Ruby 1.9.3p194.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'unicorn-timeout'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install unicorn-timeout
25
+
26
+ ## Setup/Usage (Rails 3)
27
+
28
+ In `config/application.rb`:
29
+
30
+ config.middleware.use Unicorn::Timeout
31
+
32
+ In `config/initializer/unicorn-timeout.rb` (optional):
33
+
34
+ # Timeout in seconds
35
+ Unicorn::Timeout.timeout = 15 # default
36
+
37
+ # Block that will run (within Thread.exclusive, on monitor thread) just before sending signal to process
38
+ Unicorn::Timeout.handler = lambda { |backtrace| STDERR.puts("Unicorn::Timeout is killing worker ##{Process.pid} with backtrace:\n#{backtrace.inspect}") } # default
39
+
40
+ # Signal that will be sent to current process if timeout is reached
41
+ Unicorn::Timeout.signal = "TERM" # default
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
50
+
51
+ ## Credit
52
+
53
+ This work was inspired and influenced by [rack-timeout](https://github.com/kch/rack-timeout).
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "unicorn/timeout/version"
2
+ require "unicorn/timeout"
@@ -0,0 +1,54 @@
1
+ module Unicorn
2
+ class Timeout
3
+ @timeout = 15
4
+ @handler = lambda { |backtrace| STDERR.puts("Unicorn::Timeout is killing worker ##{Process.pid} with backtrace:\n#{backtrace.inspect}") }
5
+ @signal = "TERM"
6
+ class << self
7
+ attr_accessor :timeout
8
+ attr_accessor :handler
9
+ attr_accessor :signal
10
+ end
11
+
12
+ def initialize(app)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ t = setup_mon_thread
18
+
19
+ begin
20
+ @app.call(env)
21
+ ensure
22
+ kill_mon_thread(t)
23
+ end
24
+ end
25
+
26
+ private
27
+ def setup_mon_thread
28
+ main_thread = Thread.current
29
+
30
+ Thread.new do
31
+ sleep(self.class.timeout)
32
+ kill_main_thread(main_thread)
33
+ end
34
+ end
35
+
36
+ def kill_main_thread(t)
37
+ Thread.exclusive do
38
+ begin
39
+ self.class.handler.call(t.backtrace)
40
+ ensure
41
+ Process.kill(self.class.signal, Process.pid)
42
+ end
43
+ end
44
+ end
45
+
46
+ def kill_mon_thread(t)
47
+ Thread.exclusive do
48
+ t.kill
49
+ t.join
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Unicorn
2
+ class Timeout
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/unicorn/timeout/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Anders Carling"]
6
+ gem.email = ["anders.carling@d05.se"]
7
+ gem.description = %q{Middleware for timing out current unicorn worker if request takes to long time.}
8
+ gem.summary = %q{Middleware for timing out current unicorn worker if request takes to long time.}
9
+ gem.homepage = "http://www.github.com/FootballAddicts/unicorn-timeout"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "unicorn-timeout"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Unicorn::Timeout::VERSION
17
+
18
+ gem.add_development_dependency "bundler", "~> 1.3"
19
+ gem.add_development_dependency "rake"
20
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicorn-timeout
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Anders Carling
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ prerelease: false
16
+ name: bundler
17
+ type: :development
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ none: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: '1.3'
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ prerelease: false
32
+ name: rake
33
+ type: :development
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ none: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ description: Middleware for timing out current unicorn worker if request takes to
47
+ long time.
48
+ email:
49
+ - anders.carling@d05.se
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/unicorn-timeout.rb
60
+ - lib/unicorn/timeout.rb
61
+ - lib/unicorn/timeout/version.rb
62
+ - unicorn-timeout.gemspec
63
+ homepage: http://www.github.com/FootballAddicts/unicorn-timeout
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ hash: -2098244330666962203
76
+ version: '0'
77
+ none: false
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ hash: -2098244330666962203
85
+ version: '0'
86
+ none: false
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.23
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Middleware for timing out current unicorn worker if request takes to long
93
+ time.
94
+ test_files: []