rack-server_status 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 209c13022051ab963d2d09b2f8d4a3ab0fed32cc
4
+ data.tar.gz: f4a25b58e4fbc5ca1ac07cef73db09a1c8eebf8d
5
+ SHA512:
6
+ metadata.gz: fd93772d4765b43c4a8e8ca023e24655e82d020b62044301dd4ff73779585a3ae212aa56388515eae03b8571b42f9159cb28b92ad29ef5eb4563dd51f52930ad
7
+ data.tar.gz: 68230a7f8a284f521f0e7a1abf0bbc65328b3ad818e58d02bd6faea5209b350966db6f03a580248fe0efc211495c8949da2cf7f0042a5d19671fa08eb1691d4c
data/.consolerc ADDED
@@ -0,0 +1,3 @@
1
+ # This file is automatically loaded when `bundle console` is run. You can add
2
+ # fixtures and/or initialization code here to make experimenting with your gem
3
+ # easier.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-server_status.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry'
8
+ end
9
+ group :test do
10
+ gem 'rack'
11
+ gem 'timecop'
12
+ end
13
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright 2015 SpringMT
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Rack::ServerStatus [![Build Status](https://travis-ci.org/SpringMT/rack-server_status.svg?branch=master)](https://travis-ci.org/SpringMT/rack-server_status)
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-server_status'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rack-server_status
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/rack-server_status/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class ServerStatus
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,143 @@
1
+ require 'rack/server_status/version'
2
+ require 'json'
3
+ require 'worker_scoreboard'
4
+
5
+ module Rack
6
+ class ServerStatus
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @uptime = Time.now.to_i
10
+ @skip_ps_command = options[:skip_ps_command] || false
11
+ @path = options[:path] || '/server-status'
12
+ @allow = options[:allow] || []
13
+ scoreboard_path = options[:scoreboard_path]
14
+ unless scoreboard_path.nil?
15
+ @scoreboard = WorkerScoreboard.new(scoreboard_path)
16
+ end
17
+ end
18
+
19
+ def call(env)
20
+ set_state!('A', env)
21
+
22
+ @clean_proc = Remover.new
23
+ ObjectSpace.define_finalizer(self, @clean_proc)
24
+
25
+ if @path && env['PATH_INFO'] == @path
26
+ res = handle_server_status(env)
27
+ return res
28
+ end
29
+
30
+ @app.call(env)
31
+ end
32
+
33
+ private
34
+
35
+ def set_state!(status = '_', env)
36
+ return if @scoreboard.nil?
37
+ prev = {}
38
+ unless env.nil?
39
+ prev = {
40
+ remote_addr: env['REMOTE_ADDR'],
41
+ host: env['HTTP_HOST'] || '-',
42
+ method: env['REQUEST_METHOD'],
43
+ uri: env['REQUEST_URI'],
44
+ protocol: env['SERVER_PROTOCOL'],
45
+ time: Time.now.to_i
46
+ }
47
+ end
48
+ prev[:pid] = Process.pid
49
+ prev[:ppid] = Process.ppid
50
+ prev[:uptime] = @uptime
51
+ prev[:status] = status
52
+
53
+ @scoreboard.update(prev.to_json)
54
+ end
55
+
56
+ def allowed?(address)
57
+ return true if @allow.empty?
58
+ @allow.include?(address)
59
+ end
60
+
61
+ def handle_server_status(env)
62
+ unless allowed?(env['REMOTE_ADDR'])
63
+ return [403, {'Content-Type' => 'text/plain'}, [ 'Forbidden' ]]
64
+ end
65
+
66
+ upsince = Time.now.to_i - @uptime
67
+ duration = "#{upsince} seconds"
68
+ body = "Uptime: #{@uptime} (#{duration})\n"
69
+ status = {Uptime: @uptime}
70
+
71
+ unless @scoreboard.nil?
72
+ stats = @scoreboard.read_all
73
+ parent_pid = Process.ppid
74
+ all_workers = []
75
+ idle = 0
76
+ busy = 0
77
+ if @skip_ps_command
78
+ all_workers = stats.keys
79
+ elsif RUBY_PLATFORM =~/mswin(?!ce)|mingw|cygwin|bccwin/
80
+ ps = `LC_ALL=C command ps -e -o ppid,pid`
81
+ ps.each_line do |line|
82
+ line.lstrip!
83
+ next if line =~ /^\D/
84
+ ppid, pid = line.chmop.split(/\s+/, 2)
85
+ all_workers << pid.to_i if ppid.to_i == parent_pid
86
+ end
87
+ else
88
+ all_workers = stats.keys
89
+ end
90
+ process_status_str = ''
91
+ process_status_list = []
92
+
93
+ all_workers.each do |pid|
94
+ json =stats[pid] || '{}'
95
+ pstatus = begin; JSON.parse(json, symbolize_names: true); rescue; end
96
+ pstatus ||= {}
97
+ if !pstatus[:status].nil? && pstatus[:status] == 'A'
98
+ busy += 1
99
+ else
100
+ idle += 1
101
+ end
102
+ unless pstatus[:time].nil?
103
+ pstatus[:ss] = Time.now.to_i - pstatus[:time].to_i
104
+ end
105
+ pstatus[:pid] ||= pid
106
+ pstatus.delete :time
107
+ pstatus.delete :ppid
108
+ pstatus.delete :uptime
109
+ process_status_str << sprintf("%s\n", [:pid, :status, :remote_addr, :host, :method, :uri, :protocol, :ss].map {|item| pstatus[item] || '' }.join(' '))
110
+ process_status_list << pstatus
111
+ end
112
+ body << <<"EOF"
113
+ BusyWorkers: #{busy}
114
+ IdleWorkers: #{idle}
115
+ --
116
+ pid status remote_addr host method uri protocol ss
117
+ #{process_status_str}
118
+ EOF
119
+ body.chomp!
120
+ status[:BusyWorkers] = busy
121
+ status[:IdleWorkers] = idle
122
+ status[:stats] = process_status_list
123
+ else
124
+ body << "WARN: Scoreboard has been disabled\n"
125
+ status[:WARN] = 'Scoreboard has been disabled'
126
+ end
127
+ if (env['QUERY_STRING'] || '') =~ /\bjson\b/
128
+ return [200, {'Content-Type' => 'application/json; charset=utf-8'}, [status.to_json]]
129
+ end
130
+ return [200, {'Content-Type' => 'text/plain'}, [body]]
131
+ end
132
+
133
+ class Remover
134
+ def initialize
135
+ end
136
+ def call(*args)
137
+ set_state!('_')
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/server_status/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-server_status"
8
+ spec.version = Rack::ServerStatus::VERSION
9
+ spec.authors = ["SpringMT"]
10
+ spec.email = ["today.is.sky.blue.sky@gmail.com"]
11
+
12
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
13
+ spec.required_rubygems_version = ">= 2.0"
14
+
15
+ spec.summary = %q{Show server status}
16
+ spec.description = %q{Show server status}
17
+ spec.homepage = "https://github.com/SpringMT/rack-server_status"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "worker_scoreboard"
26
+ spec.add_development_dependency "bundler", "~> 1.7"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+
30
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ describe Rack::ServerStatus do
6
+ app = lambda { |env|
7
+ [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]]
8
+ }
9
+
10
+ context 'confirm to Rack::Lint' do
11
+ context 'Not affected WorkerScoreboard' do
12
+ subject do
13
+ Rack::Lint.new(Rack::ServerStatus.new(app))
14
+ end
15
+ it do
16
+ response = Rack::MockRequest.new(subject).get('/')
17
+ expect(response.body).to eq 'Hello, World!'
18
+ end
19
+ end
20
+ context 'Affected WorkerScoreboard' do
21
+ subject do
22
+ Rack::Lint.new(Rack::ServerStatus.new(app, scoreboard_path: Dir.tmpdir))
23
+ end
24
+ it do
25
+ response = Rack::MockRequest.new(subject).get('/')
26
+ expect(response.body).to eq 'Hello, World!'
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'return valid server-status' do
32
+ subject do
33
+ Rack::Lint.new(Rack::ServerStatus.new(app, scoreboard_path: Dir.tmpdir))
34
+ end
35
+ it do
36
+ response = Rack::MockRequest.new(subject).get('/server-status')
37
+ expect(response.successful?).to be_truthy
38
+ expect(response.headers['Content-Type']).to eq 'text/plain'
39
+ end
40
+ end
41
+
42
+ context 'return json valid server-status' do
43
+ subject do
44
+ Rack::Lint.new(Rack::ServerStatus.new(app, scoreboard_path: Dir.tmpdir))
45
+ end
46
+ it do
47
+ response = Rack::MockRequest.new(subject).get('/server-status?json')
48
+ expect(response.successful?).to be_truthy
49
+ expect(response.headers['Content-Type']).to eq 'application/json; charset=utf-8'
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler.setup(:default, :test)
3
+ Bundler.require(:default, :test)
4
+
5
+ $TESTING=true
6
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib/rack/')
7
+ require 'rack/server_status'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-server_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - SpringMT
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: worker_scoreboard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Show server status
70
+ email:
71
+ - today.is.sky.blue.sky@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".consolerc"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/rack/server_status.rb
85
+ - lib/rack/server_status/version.rb
86
+ - rack-server_status.gemspec
87
+ - spec/server_status_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: https://github.com/SpringMT/rack-server_status
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '2.0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Show server status
113
+ test_files:
114
+ - spec/server_status_spec.rb
115
+ - spec/spec_helper.rb
116
+ has_rdoc: