rack-idle 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.rdoc +51 -0
  2. data/lib/rack-idle.rb +1 -0
  3. data/lib/rack/idle.rb +72 -0
  4. metadata +52 -0
@@ -0,0 +1,51 @@
1
+ = Rack::Idle
2
+
3
+ Rack::Idle is a Rack middleware that will automatically shutdown idle Rack
4
+ servers.
5
+
6
+ == Installation
7
+
8
+ gem install rack-idle
9
+
10
+ == Usage
11
+
12
+ Add the following to your config.ru:
13
+ require "rack/idle"
14
+
15
+ use Rack::Idle
16
+
17
+ ...
18
+
19
+ and once no requests have been received for 10 minutes +exit+ will be called to
20
+ shutdown the server.
21
+
22
+ The timeout can be customised like so:
23
+ use Rack::Idle, :timeout => 300 # 300 seconds == 5 minutes
24
+
25
+ Additionally a file can be monitored, and the server shutdown if the timestamp
26
+ is updated:
27
+ use Rack::Idle, :watch => "restart.txt" # touch restart.txt to shutdown
28
+
29
+ == Licence
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2012 Matthew Sadler
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining a copy
36
+ of this software and associated documentation files (the "Software"), to deal
37
+ in the Software without restriction, including without limitation the rights
38
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39
+ copies of the Software, and to permit persons to whom the Software is
40
+ furnished to do so, subject to the following conditions:
41
+
42
+ The above copyright notice and this permission notice shall be included in
43
+ all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
51
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require File.expand_path("../rack/idle", __FILE__)
@@ -0,0 +1,72 @@
1
+ module Rack # :nodoc:
2
+ end
3
+
4
+ # Rack::Idle is a Rack middleware that will automatically shutdown idle Rack
5
+ # servers.
6
+ #
7
+ # Just add the following to your config.ru:
8
+ # require "rack/idle"
9
+ #
10
+ # use Rack::Idle
11
+ #
12
+ # ...
13
+ #
14
+ # and once no requests have been received for 10 minutes +exit+ will be called
15
+ # to shutdown the server.
16
+ #
17
+ # The timeout can be customised like so:
18
+ # use Rack::Idle, :timeout => 300 # 300 seconds == 5 minutes
19
+ #
20
+ # Additionally a file can be monitored, and the server shutdown if the timestamp
21
+ # is updated:
22
+ # use Rack::Idle, :watch => "restart.txt" # touch restart.txt to shutdown
23
+ #
24
+ class Rack::Idle
25
+
26
+ # :call-seq: Idle.new(app, [options]) -> idle
27
+ #
28
+ # Create a new idle middleware instance, wrapping +app+.
29
+ #
30
+ # Available options:
31
+ # [timeout] Amount of time (in seconds) to wait after a request before calling
32
+ # +exit+ to shutdown the server
33
+ #
34
+ # [watch] Path to a file (relative to the working directory) to monitor,
35
+ # shutting down the server if the last modified date is updated.
36
+ #
37
+ def initialize(app, opts={})
38
+ @app = app
39
+
40
+ self.class.last_activity = Time.now
41
+ timeout = (opts[:timeout] || 600)
42
+
43
+ watch = opts[:watch]
44
+ if watch && File.exist?(watch)
45
+ watch_mtime = File.mtime(watch)
46
+ elsif watch
47
+ watch_mtime = Time.now
48
+ end
49
+
50
+ Thread.new do
51
+ loop do
52
+ exit if Time.now - self.class.last_activity > timeout
53
+ exit if watch && File.exist?(watch) && File.mtime(watch) > watch_mtime
54
+ sleep 1
55
+ end
56
+ end.abort_on_exception = true
57
+ end
58
+
59
+ class << self
60
+ attr_accessor :last_activity # :nodoc:
61
+ end
62
+
63
+ # :call-seq: idle.call(env) -> status_headers_body
64
+ #
65
+ # Proxied to the +app+ supplied to ::new.
66
+ #
67
+ def call(env)
68
+ self.class.last_activity = Time.now
69
+ @app.call(env)
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-idle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matthew Sadler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-21 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Automatically shutdown idle Rack servers.
15
+ email: mat@sourcetagsandcodes.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ files:
21
+ - lib/rack/idle.rb
22
+ - lib/rack-idle.rb
23
+ - README.rdoc
24
+ homepage: http://github.com/matsadler/rack-idle
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options:
28
+ - --main
29
+ - README.rdoc
30
+ - --charset
31
+ - utf-8
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.10
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Shutdown idle servers
52
+ test_files: []