daemon-kit 0.1.8.1 → 0.1.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,84 +0,0 @@
1
- module DaemonKit
2
- # Provides a wrapper for running code inside a 'safety net' Any
3
- # exceptions raised inside a safety net is handled and reported via
4
- # loggers, email or Hoptoad.
5
- #
6
- # The safety net can be configured via DaemonKit.config.safety,
7
- # which holds the only instance of the safety net.
8
- class Safety
9
-
10
- # Who get's notified.
11
- @handler = nil
12
- attr_accessor :handler
13
-
14
- # Registered error handlers
15
- @error_handlers = {}
16
- attr_reader :error_handlers
17
-
18
- class << self
19
-
20
- # Singleton
21
- @instance = nil
22
-
23
- def instance
24
- @instance ||= new
25
- end
26
- private :new
27
-
28
- # Run the provided block inside a safety net.
29
- def run(&block)
30
- self.instance.run(&block)
31
- end
32
-
33
- def register_error_handler( klass )
34
- name = klass.to_s.split('::').last.downcase
35
-
36
- DaemonKit.logger.debug( "Registering error handler '#{name}' (#{klass})" ) if DaemonKit.logger
37
-
38
- instance.instance_eval( <<-EOF, __FILE__, __LINE__ )
39
- def #{name}
40
- @#{name} ||= #{klass}.instance
41
- end
42
- EOF
43
- end
44
- end
45
-
46
- # Run the provided block inside a safety net.
47
- def run(&block)
48
- begin
49
- block.call
50
- rescue => e
51
- # Log
52
- DaemonKit.logger.fatal "Safety net caught exception: #{e.message}"
53
- DaemonKit.logger.fatal "Backtrace: #{e.backtrace.join("\n ")}"
54
-
55
- get_handler.handle_exception( e ) if get_handler
56
- end
57
- end
58
-
59
- def get_handler
60
- if @handler && self.respond_to?( @handler )
61
- h = send( @handler )
62
- return h if h.class.ancestors.include?( DaemonKit::ErrorHandlers::Base )
63
- end
64
-
65
- return nil
66
- end
67
- end
68
- end
69
-
70
- class Object
71
- class << self
72
- def safely(&block)
73
- DaemonKit::Safety.run(&block)
74
- end
75
- end
76
-
77
- def safely(&block)
78
- DaemonKit::Safety.run(&block)
79
- end
80
- end
81
-
82
- # Load our error handlers
83
- require 'daemon_kit/error_handlers/base'
84
- require 'daemon_kit/error_handlers/hoptoad'
@@ -1,23 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe DaemonKit::Safety do
4
- end
5
-
6
- describe DaemonKit::ErrorHandlers::Mail do
7
- it "should send an email report" do
8
- conf = Object.new
9
- conf.stubs(:daemon_name).returns('test')
10
- DaemonKit.stubs(:configuration).returns(conf)
11
-
12
- fake_smtp = Object.new
13
- fake_smtp.expects(:start).with('localhost.localdomain', nil, nil, nil)
14
- Net::SMTP.expects(:new).with('localhost', 25).returns(fake_smtp)
15
-
16
- begin
17
- raise RuntimeError, "specs don't fail :)"
18
- rescue => e
19
- handler = DaemonKit::ErrorHandlers::Mail.instance
20
- handler.handle_exception( e )
21
- end
22
- end
23
- end