cepa-health 0.1.0 → 0.2.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.
data/README.md CHANGED
@@ -56,7 +56,7 @@ To define probes, register blocks as:
56
56
  end
57
57
 
58
58
  The result of these Probes is summarized at the `/healthy` path when you run your Rack application. This will render a HTML table, you can similarly use `/healthy.json` or `healthy.txt` for JSON and Text results respectively. Take a look at the
59
- [probes directory](https://github.com/cepaorg/cepa-health/tree/master/probes) for
59
+ [probes directory](https://github.com/jonathannen/cepa-health/tree/master/probes) for
60
60
  some examples of probes.
61
61
 
62
62
  By default, `/healthy` will return all probes. You can cut this back using filters. For example, `healthy.txt?filters=warn` will return a Text summary of just the "warn" level Probes. `healthy.txt?filters=error,warn` resturns both "error" and "warn" probes.
data/cepa-health.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.description = %q{Health Check Middleware for Rails and Rack-based Applications}
12
12
  gem.summary = %q{Provides the facility for probes that are evaluated when a health URL is accessed.}
13
13
  gem.license = "MIT"
14
- gem.homepage = "https://github.com/cepaorg/cepa-health"
14
+ gem.homepage = "https://github.com/jonathannen/cepa-health"
15
15
 
16
16
  gem.files = `git ls-files`.split($/)
17
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -9,7 +9,7 @@ module CepaHealth
9
9
  key = SecureRandom.hex(3)
10
10
  create_file "config/initializers/cepa_health.rb", <<-CONTENT
11
11
  # Configure Cepa Health checks.
12
- # See: https://github.com/cepaorg/cepa-health
12
+ # See: https://github.com/jonathannen/cepa-health
13
13
 
14
14
  # # Comment out the following to remove the standard probes.
15
15
  CepaHealth.load_probes
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
  # Copyright © 2013 Jon Williams. See LICENSE.txt for details.
3
3
  require 'cgi'
4
+ require 'json'
4
5
 
5
6
  module CepaHealth
6
7
 
@@ -42,6 +43,7 @@ module CepaHealth
42
43
  mime = determine_mime_type(path)
43
44
 
44
45
  body = case mime
46
+ when 'application/json' then render_json(result)
45
47
  when 'text/plain' then render_text(result)
46
48
  else render_html(result)
47
49
  end
@@ -60,7 +62,7 @@ module CepaHealth
60
62
  def render_html(result)
61
63
  rows = result.records.map do |name, status, comment|
62
64
  stat = status ? "<td class='status ok'>OK</td>" : "<td class='status fail'>FAIL</td>"
63
- "<tr>#{stat}<td class='name'>#{CGI::escapeHTML(name)}</td><td>#{CGI::escapeHTML(comment)}</td></tr>"
65
+ "<tr>#{stat}<td class='name'>#{CGI::escapeHTML(name.to_s)}</td><td>#{CGI::escapeHTML(comment.to_s)}</td></tr>"
64
66
  end
65
67
  <<-HTML
66
68
  <!DOCTYPE html>
@@ -114,6 +116,12 @@ module CepaHealth
114
116
  HTML
115
117
  end
116
118
 
119
+ def render_json(result)
120
+ result.records.map do |name, status, comment|
121
+ { name: name, status: status, comment: comment }
122
+ end.to_json
123
+ end
124
+
117
125
  def render_text(result)
118
126
  body = "#Entry\t#Status\t#Comment\n"
119
127
  body << (result.success? ? "Overall\tSuccess\n" : "Overall\tFailure\n")
@@ -6,7 +6,14 @@ module CepaHealth
6
6
  class Railtie < Rails::Railtie
7
7
 
8
8
  initializer "cepa_health.configure_rails_initialization" do |app|
9
- app.middleware.use CepaHealth::Middleware
9
+ # Try and insert high up the chain. This means the health check
10
+ # will generally run before sessions are created, etc. This is
11
+ # particularly handy if you have Database-backed sessions.
12
+ begin
13
+ app.middleware.insert_before Rack::Runtime, CepaHealth::Middleware
14
+ rescue
15
+ app.middleware.use CepaHealth::Middleware
16
+ end
10
17
  end
11
18
 
12
19
  end
@@ -1,4 +1,4 @@
1
1
  module CepaHealth
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
4
4
 
data/lib/cepa-health.rb CHANGED
@@ -9,7 +9,8 @@ module CepaHealth
9
9
  attr_reader :records
10
10
 
11
11
  def execute(name, block)
12
- @success &&= !!(v = instance_exec(&block))
12
+ v = !!instance_exec(&block)
13
+ @success = @success && v
13
14
  record(name, v, v ? "Success" : "Failed")
14
15
  end
15
16
 
@@ -19,6 +20,7 @@ module CepaHealth
19
20
  end
20
21
 
21
22
  def record(name, status, comment)
23
+ @success = @success && !!status
22
24
  @records << [name, status, comment]
23
25
  end
24
26
 
@@ -3,9 +3,31 @@
3
3
 
4
4
  if defined?(Delayed)
5
5
 
6
- CepaHealth.register "Delayed Job" do
7
- record "Rails Major Version", true, Rails.version.split('.').first
6
+ CepaHealth.register "Delayed Job", "warn" do
7
+ now = Time.now.utc
8
+ record "Delayed Job Backlog", true, Delayed::Job.count
9
+
10
+ # Detect if the DJ backend is ActiveRecord or Mongoid Based
11
+ query = case
12
+ when Delayed::Job.respond_to?(:order_by) then Delayed::Job.order_by(:run_at.desc)
13
+ when Delayed::Job.respond_to?(:order) then Delayed::Job.order("run_at DESC")
14
+ else nil
15
+ end
16
+
17
+ if query.nil?
18
+ record "Unknown Delayed Job Backend", false, "#{Delayed::Job}"
19
+ else
20
+ # Maximum Delayed Job age is 10 minutes
21
+ value = query.last
22
+ if value.nil? || value.run_at > now
23
+ record 'Delayed Job Backlog Age', true, 'No expired jobs'
24
+ else
25
+ diff = (now - value.run_at)
26
+ record 'Delayed Job Backlog Age', diff < 600, "#{'%.1f' % (diff/60)} mins"
27
+ end
28
+ end
29
+
8
30
  true
9
- end
31
+ end
10
32
 
11
33
  end
data/probes/mongoid.rb CHANGED
@@ -1,3 +1,19 @@
1
1
  # encoding: utf-8
2
2
  # Copyright © 2013 Jon Williams. See LICENSE.txt for details.
3
3
 
4
+ if defined?(Mongoid)
5
+
6
+ CepaHealth.register "Mongoid" do
7
+ value = { 'ok' => nil }
8
+ tries = 3
9
+ begin
10
+ value = Mongoid.default_session.command({ping: 1})
11
+ rescue
12
+ sleep 1
13
+ tries -= 1
14
+ retry unless tries <= 0
15
+ end
16
+ value['ok'] == 1.0
17
+ end
18
+
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cepa-health
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-01 00:00:00.000000000 Z
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -67,7 +67,7 @@ files:
67
67
  - probes/sqlite.rb
68
68
  - spec/cepa_health_middleware_spec.rb
69
69
  - spec/cepa_health_spec.rb
70
- homepage: https://github.com/cepaorg/cepa-health
70
+ homepage: https://github.com/jonathannen/cepa-health
71
71
  licenses:
72
72
  - MIT
73
73
  post_install_message: