Class: Mutx::Support::Notification

Inherits:
Object
  • Object
show all
Includes:
Gmail
Defined in:
lib/mutx/support/notification.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_name, base_url = nil) ⇒ Notification

Returns a new instance of Notification.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mutx/support/notification.rb', line 10

def initialize project_name, base_url=nil
  @project_name = project_name
  @base_url = base_url
  @subject_prefix = "[Mutx] [#{@project_name}] "
  if Mutx::Support::Configuration.notification?
    begin
      @email = Gmail.connect!(Mutx::Support::Configuration.notification_username,Mutx::Support::Configuration.notification_password)
      Mutx::Support::Log.debug "Notification: Login to Gmail Succesfully"
      Mutx::Support::Log.debug "Notification: USING notification TO #{Mutx::Support::Configuration.recipients}"
    rescue => e
      Mutx::Support::Log.error "Notification: ERROR TO CONNECT TO GMAIL #{e}".red
      Mutx::Support::Log.error "Notification: Connecting to GMail error => #{e}"
      @email = NoEmail.new
    end
  else
    @email = NoEmail.new
  end

end

Instance Method Details

#execution_finished(result) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mutx/support/notification.rb', line 68

def execution_finished result
  body = "
    Result Summary: #{result.summary}

    Command: #{result.command}

    Execution name: #{result.execution_name}

    Started at: #{result.started_at_formatted}

    Finished at: #{result.finished_at_formatted}

    Elapsed Time: #{result.elapsed_time} seconds

    Custom Params: #{result.custom_params}

    See log at http://#{@base_url}/mutx/results/#{result.id}/log"

  if Mutx::Support::Configuration.attach_report?
    path_to_file = "#{Dir.pwd}/mutx/temp/#{result.id}.html"
    Mutx::Support::Log.debug "Notification: Creating file report to attach to mail (#{path_to_file})"
    File.open("#{path_to_file}","a+"){|f| f.write result.html_report}
    Mutx::Support::Log.debug "Notification: File created (#{path_to_file})"
  end
  message_subject = "Execution Finished (#{result.id}) "
  send_email message_subject, body, path_to_file
end

#execution_stopped(result, additional_text = nil) ⇒ Object



96
97
98
99
100
# File 'lib/mutx/support/notification.rb', line 96

def execution_stopped result, additional_text=nil
  body = "Execution stopped \n#{additional_text}"
  message_subject = "Execution stopped (#{result.id})"
  send_email message_subject, body
end

#notificate(subject, body) ⇒ Object



30
31
32
# File 'lib/mutx/support/notification.rb', line 30

def notificate subject, body
  send_email subject, body, Mutx::Support::Configuration.notifications_to
end

#send_email(message_subject, message_body, path_to_file = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mutx/support/notification.rb', line 34

def send_email message_subject, message_body, path_to_file=nil
  message_subject = "#{@subject_prefix} #{message_subject}"
  begin
    email = @email.compose do
      to Mutx::Support::Configuration.recipients
      subject message_subject
      text_part do
        body message_body
      end
      html_part do
        Mutx::Support::Log.debug "Notification: Attaching report file (#{path_to_file})"
        content_type 'text/html; charset=UTF-8'
        body "<p>#{message_body}</p>"
      end
      add_file path_to_file
    end
    email.deliver!
    Mutx::Support::Log.debug "Notification: Email sent to (#{Mutx::Support::Configuration.recipients}) | Subject: '#{message_subject}' | Message: #{message_body}" if Mutx::Support::Log
  rescue => e
    Mutx::Support::Log.error "Notification: Could not sent email to (#{Mutx::Support::Configuration.recipients}) | Subject: '#{message_subject}' | Message: #{message_body} | #{e}\n #{e.backtrace}" if Mutx::Support::Log
  end

  if path_to_file
    begin
      File.delete path_to_file
      Mutx::Support::Log.debug "Notification: File #{path_to_file} deleted"
    rescue
      Mutx::Support::Log.error "Notification: Could not delete file #{path_to_file}"
    end
  end
end