Skip to content

Commit

Permalink
Exception mails now handled by TMail
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethkalmer committed Jun 1, 2009
1 parent ddd516f commit 260a800
Show file tree
Hide file tree
Showing 32 changed files with 7,937 additions and 34 deletions.
22 changes: 22 additions & 0 deletions lib/daemon_kit.rb
Expand Up @@ -20,4 +20,26 @@ module DaemonKit
autoload :Jabber, 'daemon_kit/jabber'
autoload :AMQP, 'daemon_kit/amqp'
autoload :Nanite, 'daemon_kit/nanite'

class << self
def logger
@logger
end

def logger=( logger )
@logger = logger
end

def root
DAEMON_ROOT
end

def env
DAEMON_ENV
end

def framework_root
@framework_root ||= File.join( File.dirname(__FILE__), '..' ).to_absolute_path
end
end
end
58 changes: 43 additions & 15 deletions lib/daemon_kit/error_handlers/mail.rb
@@ -1,33 +1,57 @@
require DaemonKit.framework_root + '/vendor/tmail'
require 'net/smtp'

module DaemonKit
module ErrorHandlers
# Send an email notification of the exception via SMTP
# Send an email notification of the exception via SMTP.
class Mail < Base

# SMTP hostname
@host = 'localhost'
attr_accessor :host


# SMTP port
@port = 25

# Recipients of the notification
@recipients = []
attr_accessor :recipients

# Subject prefix
@prefix = '[DAEMON-KIT]'
attr_accessor :prefix

# Sender address
@sender = 'daemon-kit'
attr_accessor :sender

# SMTP username
@username = nil

# SMTP password
@password = nil

# Authentication mechanism (:plain, :login, or :cram_md5)
@authentication = nil

# Use TLS?
@tls = false

# Domain used when talking to SMTP server
@domain = 'localhost.localdomain'

class << self
attr_accessor :host, :port, :recipients, :prefix, :sender, :username,
:password, :authentication, :tls, :domain
end

def handle_exception( exception )
email = <<EOF
To: #{self.recipients.map { |r| '<' + r + '>' }.join(', ')}
From: <#{self.sender}>
Subject: #{self.prefix} #{exception.message}
Date: #{Time.now}

mail = TMail::Mail.new
mail.to = self.class.recipients
mail.from = self.class.sender
mail.subject = "#{self.class.prefix} #{exception.message}"
mail.set_content_type 'text', 'plain'
mail.mime_version = '1.0'
mail.date = Time.now

mail.body = <<EOF
DaemonKit caught an exception inside #{DaemonKit.configuration.daemon_name}.
Message: #{exception.message}
Expand All @@ -37,10 +61,14 @@ def handle_exception( exception )
Environment: #{ENV.inspect}
EOF
begin
Net::SMTP.start( self.host ) do |smtp|
smtp.send_message( email, self.sender, self.recipients )
smtp = Net::SMTP.new( self.class.host, self.class.port )
smtp.enable_starttls_auto if self.class.tls && smtp.respond_to?(:enable_starttls_auto)
smtp.start( self.class.domain, self.class.username, self.class.password,
self.class.authentication ) do |smtp|
smtp.sendmail( mail.to_s, mail.from, mail.to )
end
rescue
rescue => e
DaemonKit.logger.error "Failed to send exception mail: #{e.message}" if DaemonKit.logger
end
end
end
Expand Down
19 changes: 0 additions & 19 deletions lib/daemon_kit/initializer.rb
Expand Up @@ -18,14 +18,6 @@ module DaemonKit

class << self

def logger
@logger
end

def logger=( logger )
@logger = logger
end

def configuration
@configuration
end
Expand All @@ -46,17 +38,6 @@ def trap( *args, &block )
self.configuration.trap( *args, &block )
end

def framework_root
@framework_root ||= File.join( File.dirname(__FILE__), '..', '..' ).to_absolute_path
end

def root
DAEMON_ROOT
end

def env
DAEMON_ENV
end
end


Expand Down
23 changes: 23 additions & 0 deletions spec/error_handlers_spec.rb
@@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/spec_helper'

describe DaemonKit::Safety do
end

describe DaemonKit::ErrorHandlers::Mail do
it "should send an email report" do
conf = Object.new
conf.stubs(:daemon_name).returns('test')
DaemonKit.stubs(:configuration).returns(conf)

fake_smtp = Object.new
fake_smtp.expects(:start).with('localhost.localdomain', nil, nil, nil)
Net::SMTP.expects(:new).with('localhost', 25).returns(fake_smtp)

begin
raise RuntimeError, "specs don't fail :)"
rescue => e
handler = DaemonKit::ErrorHandlers::Mail.instance
handler.handle_exception( e )
end
end
end
5 changes: 5 additions & 0 deletions vendor/tmail-1.2.3/tmail.rb
@@ -0,0 +1,5 @@
require 'tmail/version'
require 'tmail/mail'
require 'tmail/mailbox'
require 'tmail/core_extensions'
require 'tmail/net'

0 comments on commit 260a800

Please sign in to comment.