Sha256: 8b4eb19d22d30ce1aa3d7ed144f0b07624ca1b825d74d7e461d13d894b9371a0

Contents?: true

Size: 1.96 KB

Versions: 33

Compression:

Stored size: 1.96 KB

Contents

module DaemonKit
  # Provides a wrapper for running code inside a 'safety net' Any
  # exceptions raised inside a safety net is handled and reported via
  # loggers, email or Hoptoad.
  #
  # The safety net can be configured via DaemonKit.config.safety,
  # which holds the only instance of the safety net.
  class Safety

    # Who get's notified.
    @handler = nil
    attr_accessor :handler

    # Registered error handlers
    @error_handlers = {}
    attr_reader :error_handlers

    class << self

      # Singleton
      @instance = nil

      def instance
        @instance ||= new
      end
      private :new

      # Run the provided block inside a safety net.
      def run(&block)
        self.instance.run(&block)
      end

      def register_error_handler( klass )
        name = klass.to_s.split('::').last.downcase

        DaemonKit.logger.debug( "Registering error handler '#{name}' (#{klass})" ) if DaemonKit.logger

        instance.instance_eval( <<-EOF, __FILE__, __LINE__ )
        def #{name}
          @#{name} ||= #{klass}.instance
        end
        EOF
      end
    end

    # Run the provided block inside a safety net.
    def run(&block)
      begin
        block.call
      rescue => e
        # Log
        DaemonKit.logger.fatal "Safety net caught exception: #{e.message}"
        DaemonKit.logger.fatal "Backtrace: #{e.backtrace.join("\n    ")}"

        get_handler.handle_exception( e ) if get_handler
      end
    end

    def get_handler
      if @handler && self.respond_to?( @handler )
        h = send( @handler )
        return h if h.class.ancestors.include?( DaemonKit::ErrorHandlers::Base )
      end

      return nil
    end
  end
end

class Object
  class << self
    def safely(&block)
      DaemonKit::Safety.run(&block)
    end
  end

  def safely(&block)
    DaemonKit::Safety.run(&block)
  end
end

# Load our error handlers
require 'daemon_kit/error_handlers/base'
require 'daemon_kit/error_handlers/mail'
require 'daemon_kit/error_handlers/hoptoad'

Version data entries

33 entries across 33 versions & 5 rubygems

Version Path
kennethkalmer-daemon-kit-0.1.6 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.10 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.3 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.4 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.5 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.7 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.8 lib/daemon_kit/safety.rb
kennethkalmer-daemon-kit-0.1.7.9 lib/daemon_kit/safety.rb
queso-daemon-kit-0.1.7.10 lib/daemon_kit/safety.rb
skaar-daemon-kit-0.1.7.10 lib/daemon_kit/safety.rb
daemon-kit-0.1.8rc1 lib/daemon_kit/safety.rb
apphunkd-0.13.0 vendor/daemon_kit/lib/daemon_kit/safety.rb
apphunkd-0.12.2 vendor/daemon_kit/lib/daemon_kit/safety.rb
apphunkd-0.12.1 vendor/daemon_kit/lib/daemon_kit/safety.rb
apphunkd-0.12.0 vendor/daemon_kit/lib/daemon_kit/safety.rb
daemon-kit-0.1.8pre lib/daemon_kit/safety.rb
apphunkd-0.11.1 vendor/daemon_kit/lib/daemon_kit/safety.rb
apphunkd-0.11.0 vendor/daemon_kit/lib/daemon_kit/safety.rb
daemon-kit-0.1.7.12 lib/daemon_kit/safety.rb
daemon-kit-0.1.7.11 lib/daemon_kit/safety.rb