]> CyberLeo.Net >> Repos - CDN/status.git/blob - lib/checks.rb
Avoid trapping Exception; use StandardError instead
[CDN/status.git] / lib / checks.rb
1 require 'configuration'
2 require 'check'
3 require 'database_check'
4 require 'website_check'
5
6
7 class Checks
8   def self.store_path
9     @store_path ||= File.expand_path(File.join(File.dirname(__FILE__), "..", "run", "check.dat"))
10   end
11
12   class Worker
13     @@config = "checks.yml"
14
15     def self.config
16       Configuration.load(@@config)
17     end
18
19     def initialize
20       @checks = []
21     end
22
23     def reload_config
24       return unless !@config || @config.stale?
25       @config ||= self.class.config
26       @checks = @config.map do |check|
27         Kernel.const_get(check[:class]).new(check)
28       end
29     end
30
31     def check
32       reload_config
33       threads = @checks.map {|check| Thread.new { check.do_it } }
34       threads.each(&:join)
35       store
36     end
37
38     def passed?
39       @checks.map(&:passed?).uniq == [ true ]
40     end
41
42     def results
43       @checks
44     end
45
46     def store
47       File.open(Checks.store_path, File::RDWR|File::CREAT, 0644) do |fp|
48         fp.flock(File::LOCK_EX)
49         fp.seek(0)
50         fp.truncate(0)
51         fp.write(Marshal.dump(@checks))
52         fp.flush
53         fp.flock(File::LOCK_UN)
54       end
55     end
56   end
57
58   class Web
59     def load
60       File.open(Checks.store_path, File::RDONLY|File::CREAT, 0644) do |fp|
61         fp.flock(File::LOCK_SH)
62         fp.seek(0)
63         @checks = Marshal.load(fp.read)
64         fp.flock(File::LOCK_UN)
65       end
66     rescue StandardError => err
67       @checks = []
68     end
69
70     def check
71       load
72     end
73
74     def passed?
75       @checks.count > 0 && @checks.map(&:passed?).uniq == [ true ] && @checks.map(&:stale?).uniq == [ false ]
76     end
77
78     def results
79       @checks
80     end
81   end
82 end