cepa-health 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cepa-health.rb CHANGED
@@ -8,10 +8,9 @@ module CepaHealth
8
8
  class Result
9
9
  attr_reader :records
10
10
 
11
- def execute(name, block)
12
- v = !!instance_exec(&block)
13
- @success = @success && v
14
- record(name, v, v ? "Success" : "Failed")
11
+ def execute(block)
12
+ v = instance_exec(&block)
13
+ Array === v ? record(*v) : record("Probe", v, v ? "Success" : "Failed")
15
14
  end
16
15
 
17
16
  def initialize
@@ -21,7 +20,7 @@ module CepaHealth
21
20
 
22
21
  def record(name, status, comment)
23
22
  @success = @success && !!status
24
- @records << [name, status, comment]
23
+ @records << [name.to_s, !!status, comment.to_s]
25
24
  end
26
25
 
27
26
  def success?
@@ -45,7 +44,7 @@ module CepaHealth
45
44
  result = CepaHealth::Result.new
46
45
  filters = filters.map { |v| v.to_s }
47
46
  selected = filters.empty? ? probes : probes.select { |k,v| filters.include?(k) }
48
- selected.values.flatten(1).each { |v| result.execute(*v) }
47
+ selected.values.flatten(1).each { |v| result.execute(v) }
49
48
  result
50
49
  end
51
50
 
@@ -63,9 +62,9 @@ module CepaHealth
63
62
 
64
63
  # Registers the given block as a probe. An optional level can be supplied,
65
64
  # which can be used as a filter.
66
- def register(name, level = :error, &block)
65
+ def register(level = :error, &block)
67
66
  list = probes[level.to_s] ||= []
68
- list << [name, block]
67
+ list << block
69
68
  self
70
69
  end
71
70
 
@@ -1,4 +1,4 @@
1
1
  module CepaHealth
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
4
4
 
@@ -3,21 +3,20 @@
3
3
 
4
4
  if defined?(Delayed)
5
5
 
6
- CepaHealth.register "Delayed Job", "warn" do
6
+ CepaHealth.register :warn do
7
7
  now = Time.now.utc
8
8
  record "Delayed Job Backlog", true, Delayed::Job.count
9
9
 
10
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")
11
+ type = case
12
+ when Delayed::Job.respond_to?(:order_by) then :mongoid
13
+ when Delayed::Job.respond_to?(:order) then :active_record
14
14
  else nil
15
15
  end
16
16
 
17
- if query.nil?
18
- record "Unknown Delayed Job Backend", false, "#{Delayed::Job}"
19
- else
20
- # Maximum Delayed Job age is 10 minutes
17
+ # Maximum Delayed Job age is 10 minutes
18
+ unless type.nil?
19
+ query = type == :active_record ? Delayed::Job.order("run_at DESC") : Delayed::Job.order_by(:run_at.desc)
21
20
  value = query.last
22
21
  if value.nil? || value.run_at > now
23
22
  record 'Delayed Job Backlog Age', true, 'No expired jobs'
@@ -27,7 +26,13 @@ if defined?(Delayed)
27
26
  end
28
27
  end
29
28
 
30
- true
31
- end
29
+ # Check for failed jobs
30
+ if type.nil?
31
+ [ "Unknown Delayed Job Backend", false, "#{Delayed::Job}" ]
32
+ else
33
+ failures = type == :active_record ? Delayed::Job.where('attempts > 0').count : Delayed::Job.where(:attempts.gt => 0).count
34
+ ["Delayed Job Failures", failures == 0, "#{failures} failed job#{failures == 1 ? '' : 's'}"]
35
+ end
36
+ end
32
37
 
33
38
  end
data/probes/mongoid.rb CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  if defined?(Mongoid)
5
5
 
6
- CepaHealth.register "Mongoid" do
6
+ CepaHealth.register do
7
7
  value = { 'ok' => nil }
8
8
  tries = 3
9
9
  begin
@@ -13,7 +13,8 @@ if defined?(Mongoid)
13
13
  tries -= 1
14
14
  retry unless tries <= 0
15
15
  end
16
- value['ok'] == 1.0
16
+
17
+ [ "Mongoid", value['ok'] == 1.0, "Ping Database"]
17
18
  end
18
19
 
19
20
  end
data/probes/rails.rb CHANGED
@@ -4,9 +4,8 @@
4
4
  if defined?(Rails)
5
5
 
6
6
  # A Trivial Rails probe.
7
- CepaHealth.register "Rails" do
8
- record "Rails Major Version", true, Rails.version.split('.').first
9
- true
7
+ CepaHealth.register do
8
+ ["Rails Major Version", true, Rails.version.split('.').first]
10
9
  end
11
10
 
12
11
  end
data/probes/sqlite.rb CHANGED
@@ -6,10 +6,9 @@ if defined?(ActiveRecord) && defined?(SQLite3)
6
6
  CepaHealth.register "SQLite" do
7
7
  begin
8
8
  ActiveRecord::Base.connection.exec_query("PRAGMA quick_check")
9
- true
9
+ [ "SQLite", true, "Quick Check" ]
10
10
  rescue Exception => e
11
- record("SQLite Failure", false, e.inspect)
12
- false
11
+ [ "SQLite", false, e.inspect ]
13
12
  end
14
13
  end
15
14
 
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.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: