Skip to content

Commit f8d1e0d

Browse files
committed
Add rake exception notification to mailchimp hooks
1 parent a50562a commit f8d1e0d

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

Gemfile.lock

+1-2
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ GEM
126126
rails (>= 3.1.0)
127127
redcarpet (>= 2.0.0)
128128
metaclass (0.0.1)
129-
minitest (2.12.1)
130-
mocha (0.11.4)
131129
mime-types (1.19)
132130
minitest (2.12.1)
131+
mocha (0.11.4)
133132
multi_json (1.3.6)
134133
multi_xml (0.2.2)
135134
multipart-post (1.1.5)

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
require File.expand_path('../config/application', __FILE__)
55
require 'rake'
6+
require File.expand_path('../lib/rake_exception_notification', __FILE__)
67

78
PracticingRubyWeb::Application.load_tasks

lib/rake_exception_notification.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module RakeExceptionNotification
2+
# Exception notification (rails3) only works as a rack middleware,
3+
# but what if you need notifications inside a rake task or a script?
4+
# This is a quick hack around that.
5+
#
6+
# Source: https://gist.github.com/551136
7+
#
8+
# Wrap your code inside an exception_notify block and you will be notified of exceptions
9+
#
10+
# exception_notify { # Dangerous Code Here }
11+
def exception_notify
12+
yield
13+
rescue Exception => exception
14+
if Rails.env.production?
15+
env = {}
16+
env['exception_notifier.options'] = {
17+
# TODO: DRY this configuration up
18+
:email_prefix => '[Practicing Ruby Rake] ',
19+
:exception_recipients => %w{[email protected] [email protected]},
20+
:sections => ['backtrace']
21+
}
22+
ExceptionNotifier::Notifier.exception_notification(env, exception).deliver
23+
end
24+
raise exception
25+
end
26+
end

lib/tasks/mailchimp.rake

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
include ActionView::Helpers::TextHelper
2+
include RakeExceptionNotification
23

34
namespace :mailchimp do
45
desc 'Disable accounts which have been unsubscribed in mailchimp'
56
task :disable_unsubscribed => :environment do
67

78
puts "== Running mailchip:update_subscribers at #{Time.now} =="
89

9-
user_manager = UserManager.new
10+
exception_notify do
11+
user_manager = UserManager.new
12+
user_manager.disable_unsubscribed_users
1013

11-
user_manager.disable_unsubscribed_users
14+
raise "Wow"
15+
end
1216
end
1317
end

0 commit comments

Comments
 (0)