Module: BatchKit::Helpers::Email

Includes:
Html
Defined in:
lib/batch-kit/helpers/email.rb

Overview

Defines a number of methods to help with generating email messages in both plain-text and HTML formats.

Instance Method Summary collapse

Methods included from Html

#add_table_cells, #create_head_tag, #create_html_document, #create_html_table, #create_style_tag

Instance Method Details

#add_job_details_to_email(body) ⇒ Object

Adds details of tasks run and their duration to an email.

Parameters:

  • body (Array<String>)

    An array containing the lines of the message body. Job execution details will be added as an HTML table to this.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/batch-kit/helpers/email.rb', line 73

def add_job_details_to_email(body)
    has_instances = self.job_run.task_runs.find{ |tr| tr.instance }
    body << "<br>"
    body << "<div class='separator'></div>"
    body << "<p>"
    body << "<p>Job execution details:</p>"
    create_html_table(body, self.job_run.task_runs,
                      {name: :name, label: 'Task'},
                      {name: :instance, show: has_instances},
                      {name: :start_time, label: 'Start Time'},
                      {name: :end_time, label: 'End Time'},
                      {label: 'Duration', class: 'right',
                       value: lambda{ |tr| DateTime.display_duration(tr.elapsed) }})
    body.slice!(-2..-1)
    body << "<tr><th>#{self.job.name}</th>"
    body << "<th>#{self.job_run.instance}</th>" if has_instances
    body << "<th class='right'>#{self.job_run.start_time.strftime("%H:%M:%S")}</th>"
    body << "<th class='right'></th>"
    body << "<th class='right'>#{DateTime.display_duration(self.job_run.elapsed)}</th></tr>"
    body << "</tbody>"
    body << "</table>"
    body << "<br>"
end

#create_email(cfg = nil) ⇒ Object

Creates a new Mail message object that can be used to create and send an email.

Parameters:

  • cfg (BatchKit::Config) (defaults to: nil)

    A config object containing details of an SMTP gateway for delivering the message. Defaults to the config object defined by including BatchKit#Configurable.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/batch-kit/helpers/email.rb', line 23

def create_email(cfg = nil)
    cfg = config if cfg.nil? && self.respond_to?(:config)
    Mail.defaults do
        delivery_method :smtp, cfg.smtp
    end

    Mail.new(to: mail_list(cfg[:to]),
             cc: mail_list(cfg[:cc]),
             from: cfg[:email_from] || "#{self.job.job_class.name}@#{self.job.computer}",
             reply_to: mail_list(cfg[:reply_to]))
end

#create_failure_email(cfg = config) ⇒ Object

Creates an email message containing details of the exception that caused this job to fail.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/batch-kit/helpers/email.rb', line 100

def create_failure_email(cfg = config)
    msg = create_email(cfg)
    to = cfg[:failure_email_to]
    to = to.join(', ') if to.is_a?(Array)
    cc = cfg[:failure_email_cc]
    cc = cc.join(', ') if cc.is_a?(Array)
    msg.to = to
    msg.cc = cc
    msg.subject = "#{self.job.name} job on #{self.job.computer} Failed!"

    # Add details of the failed job and task runs
    body = []
    self.job.runs.each do |jr|
        ex = nil
        jr.task_runs.select{ |tr| tr.exception != nil }.each do |tr|
            ex = tr.exception
            body << "Job '#{jr.label}' has failed in task '#{tr.label}'."
            body << "\n#{ex.class.name}: #{ex.message}"
            body << "\nBacktrace:"
            body += ex.backtrace
            body << "\n"
        end
        if ex != jr.exception
            body << "Job '#{jr.label}' has failed."
            body << "\n#{jr.exception.class.name}: #{jr.exception.message}"
            body << "\nBacktrace:"
            body += jr.exception.backtrace
            body << "\n"
        end
    end

    # Add job log file as attachment (if it exists)
    if self.respond_to?(:log) && self.log.log_file
        body << "See the attached log for details.\n"
        msg.add_file(self.log.log_file)
    end
    msg.body = body.join("\n")
    msg
end

#create_html_email(cfg = config, body_text = nil) {|Array<String>| ... } ⇒ Object

Creates an HTML formatted email, with a default set of styles.

Parameters:

  • cfg (BatchKit::Config) (defaults to: config)

    A config object containing details of an SMTP gateway for delivering the message. Defaults to the config object defined by including BatchKit#Configurable.

  • body_text (String) (defaults to: nil)

    An optional string containing text to add to the email body.

Yields:

  • (Array<String>)

    an Array of strings to which body content can be added.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/batch-kit/helpers/email.rb', line 53

def create_html_email(cfg = config, body_text = nil, &blk)
    if cfg.is_a?(String) || cfg.is_a?(Array)
        body_text = cfg
        cfg = nil
    end
    msg = create_email(cfg)
    body = create_html_document(body_text, &blk)
    msg.html_part = Mail::Part.new do |part|
      part.content_type('text/html; charset=UTF-8')
      part.body(body.join("\n"))
    end
    msg
end

#mail_list(recips) ⇒ Object

Mail likes its recipient lists as a comma-separated list in a String. To make this easier to use, this helper method converts an array of values into sucn a string.



39
40
41
# File 'lib/batch-kit/helpers/email.rb', line 39

def mail_list(recips)
    recips.is_a?(Array) ? recips.join(', ') : recips
end

#recipient_count(msg) ⇒ Object

Given a message, returns the number of recipients currently set.

Parameters:

  • msg (Mail)

    A Mail message object.



164
165
166
167
168
169
# File 'lib/batch-kit/helpers/email.rb', line 164

def recipient_count(msg)
    count = 0
    count += msg.to.size if msg.to
    count += msg.cc.size if msg.cc
    count
end

#save_msg_to_file(msg, path, options = {}) ⇒ Object

Saves the content of a message to a file.

Parameters:

  • msg (Mail)

    The message whose content is to be saved.

  • path (String)

    The path to the file to be created.

  • options (Hash) (defaults to: {})

    An options hash; see FileUtils.archive for details of supported option settings.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/batch-kit/helpers/email.rb', line 178

def save_msg_to_file(msg, path, options = {})
    FileUtils.archive(path, options)
    file = File.open(path, "w")
    in_html = false
    msg.html_part.to_s.each_line do |line|
        line.chomp!
        in_html ||= (line =~ /^<html/i)
        if in_html
            file.puts line
            file.puts "<title>#{msg.subject}</title>" if line =~ /<head>/
        end
    end
    file.close
    log.detail "Saved email body to #{path}"
end

#send_failure_email(cfg = config, recipients = nil) ⇒ Object

Sends a failure email message in response to a job failure.

Parameters:

  • recipients (String|Array) (defaults to: nil)

    The recipient(s) to receive the email. If no recipients are specified, the con



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/batch-kit/helpers/email.rb', line 145

def send_failure_email(cfg = config, recipients = nil)
    unless cfg.is_a?(Hash)
        recipients = cfg
        cfg = config
    end
    msg = create_failure_email(cfg)
    if recipients
        # Override default recipients
        msg.to = recipients
        msg.cc = nil
    end
    msg.deliver!
    log.detail "Failure email sent to #{recipient_count(msg)} recipients"
end