pingable 0.0.1
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.
- data/.gitignore +6 -0
- data/Gemfile +2 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/example/config.ru +14 -0
- data/lib/pingable/common_checks.rb +51 -0
- data/lib/pingable/core.rb +34 -0
- data/lib/pingable/handler.rb +26 -0
- data/lib/pingable/version.rb +3 -0
- data/lib/pingable.rb +4 -0
- data/pingable.gemspec +24 -0
- metadata +105 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
Pinger
|
2
|
+
======
|
3
|
+
|
4
|
+
Pinger is a simple framework to implement a 'ping' URL in Rack-based web applications.
|
5
|
+
|
6
|
+
For example, a Rails or Sinatra app's `config.ru` may look like this:
|
7
|
+
|
8
|
+
map '/ping' do
|
9
|
+
use Pingable::Handler
|
10
|
+
end
|
11
|
+
run MyApp
|
12
|
+
|
13
|
+
Now you can add checks in a modular fashion:
|
14
|
+
|
15
|
+
Pingable.add_check lambda {
|
16
|
+
unless check_something
|
17
|
+
"Oh noes, something failed"
|
18
|
+
end
|
19
|
+
}
|
20
|
+
|
21
|
+
Something a bit more complex:
|
22
|
+
|
23
|
+
class TwitterCheck
|
24
|
+
def initialize(url)
|
25
|
+
@url = url
|
26
|
+
end
|
27
|
+
|
28
|
+
def call
|
29
|
+
# ... check the URL ...
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Pingable.add_check TwitterCheck.new(:url => "http://twitter.com/")
|
34
|
+
|
35
|
+
A check is simply an object which:
|
36
|
+
|
37
|
+
* implements `call` with no arguments.
|
38
|
+
* returns one of the following on error:
|
39
|
+
* a string
|
40
|
+
* a hash of {:message => message}
|
41
|
+
* an array of the above
|
42
|
+
* returns nil on success.
|
43
|
+
|
44
|
+
Common checks
|
45
|
+
-------------
|
46
|
+
|
47
|
+
Pingable comes with a set of checks that are commonly needed for web applications. To install these, add the following:
|
48
|
+
|
49
|
+
Pingable.common_checks!
|
50
|
+
|
51
|
+
These include:
|
52
|
+
|
53
|
+
* A check to verify ActiveRecord's current connection.
|
54
|
+
* A check to check the ability for Rails' cache to read and write values.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/example/config.ru
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'pingable'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
Pingable.common_checks!
|
5
|
+
Pingable.add_check lambda {
|
6
|
+
{:message => "oh noes"}
|
7
|
+
}
|
8
|
+
|
9
|
+
map '/ping' do
|
10
|
+
use Pingable::Handler
|
11
|
+
end
|
12
|
+
run lambda { |env|
|
13
|
+
[200, {'Content-Type' => 'text/plain'}, ['OK']]
|
14
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Pingable
|
2
|
+
|
3
|
+
class << self
|
4
|
+
@@common_checks_added ||= false
|
5
|
+
|
6
|
+
# Add checks for standard gems such as Active Record.
|
7
|
+
def common_checks!
|
8
|
+
unless @@common_checks_added
|
9
|
+
add_active_record!
|
10
|
+
add_rails_cache!
|
11
|
+
@@common_checks_added = true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def add_active_record!
|
18
|
+
if defined?(ActiveRecord)
|
19
|
+
add_check lambda {
|
20
|
+
begin
|
21
|
+
ActiveRecord::Base.verify_active_connections!
|
22
|
+
ActiveRecord::Base.connection.execute('select 1')
|
23
|
+
nil
|
24
|
+
rescue Exception => e
|
25
|
+
if e.class.name == e.message
|
26
|
+
"ActiveRecord: #{e}"
|
27
|
+
else
|
28
|
+
"ActiveRecord: #{e.class}: #{e.message}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_rails_cache!
|
36
|
+
if defined?(Rails) and Rails.respond_to?(:cache)
|
37
|
+
add_check lambda {
|
38
|
+
begin
|
39
|
+
Rails.cache.read('ping_check')
|
40
|
+
Rails.cache.write('ping_check', 'ok')
|
41
|
+
nil
|
42
|
+
rescue Exception => e
|
43
|
+
"Rails cache: #{e.class}: #{e.message}"
|
44
|
+
end
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Pingable
|
2
|
+
class << self
|
3
|
+
|
4
|
+
@@checks ||= []
|
5
|
+
|
6
|
+
def add_check(check)
|
7
|
+
@@checks << check
|
8
|
+
end
|
9
|
+
|
10
|
+
def run_checks!
|
11
|
+
failures = []
|
12
|
+
@@checks.each do |check|
|
13
|
+
begin
|
14
|
+
result = check.call
|
15
|
+
rescue Exception => e
|
16
|
+
failures.push(:message => "Pinger check failed: #{e}")
|
17
|
+
else
|
18
|
+
if result
|
19
|
+
case result
|
20
|
+
when Array
|
21
|
+
failures.concat result
|
22
|
+
when String
|
23
|
+
failures.push(:message => result)
|
24
|
+
else
|
25
|
+
failures.push(result)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
failures
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Pingable
|
2
|
+
|
3
|
+
class Handler
|
4
|
+
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
failures = Pingable.run_checks!
|
11
|
+
if failures.any?
|
12
|
+
[500, HEADERS, failures.map { |f| f[:message] }.join("\n")]
|
13
|
+
else
|
14
|
+
app_name = env['pingable.name']
|
15
|
+
app_name ||= ''
|
16
|
+
[200, HEADERS, app_name]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
HEADERS = {'Cache-Control' => 'private'}
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/pingable.rb
ADDED
data/pingable.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "pingable/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "pingable"
|
8
|
+
s.version = Pingable::VERSION
|
9
|
+
s.authors = ["Alexander Staubo"]
|
10
|
+
s.email = ["alex@origo.no"]
|
11
|
+
s.homepage = "http://github.com/origo/pingable"
|
12
|
+
s.summary = s.description = %q{Pingable is a Rack handler that let an app respond to a path /ping, with pluggable checks}
|
13
|
+
|
14
|
+
s.rubyforge_project = "pingable"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
|
23
|
+
s.add_runtime_dependency "rack", '>= 1.0.0'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pingable
|
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: 2012-01-27 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
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rack
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 23
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 1.0.0
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
description: Pingable is a Rack handler that let an app respond to a path /ping, with pluggable checks
|
51
|
+
email:
|
52
|
+
- alex@origo.no
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- example/config.ru
|
65
|
+
- lib/pingable.rb
|
66
|
+
- lib/pingable/common_checks.rb
|
67
|
+
- lib/pingable/core.rb
|
68
|
+
- lib/pingable/handler.rb
|
69
|
+
- lib/pingable/version.rb
|
70
|
+
- pingable.gemspec
|
71
|
+
homepage: http://github.com/origo/pingable
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project: pingable
|
100
|
+
rubygems_version: 1.8.6
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Pingable is a Rack handler that let an app respond to a path /ping, with pluggable checks
|
104
|
+
test_files: []
|
105
|
+
|