Class: Kaya::Support::Notification

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

Instance Method Summary collapse

Constructor Details

#initialize(project_name, base_url = nil) ⇒ Notification

Returns a new instance of Notification.



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

def initialize project_name, base_url=nil
  @project_name = project_name
  @base_url = base_url
  @subject_prefix = "[Kaya] [#{@project_name}] "
  if Kaya::Support::Configuration.notification?
    begin
      @email = Gmail.connect!(Kaya::Support::Configuration.notification_username,Kaya::Support::Configuration.notification_password)
      $K_LOG.debug "Notification: Login to Gmail Succesfully"
      $K_LOG.debug "Notification: USING NOTIFICATION TO #{Kaya::Support::Configuration.recipients}"
    rescue => e
      $K_LOG.error "Notification: ERROR TO CONNECT TO GMAIL #{e}".red
      $K_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



67
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
# File 'lib/kaya/support/notification.rb', line 67

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}/kaya/results/#{result.id}/log"

  if Kaya::Support::Configuration.attach_report?
    path_to_file = "#{Dir.pwd}/kaya/temp/#{result.id}.html"
    $K_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}
    $K_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



95
96
97
98
99
# File 'lib/kaya/support/notification.rb', line 95

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



29
30
31
# File 'lib/kaya/support/notification.rb', line 29

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

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



33
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
# File 'lib/kaya/support/notification.rb', line 33

def send_email message_subject, message_body, path_to_file=nil
  message_subject = "#{@subject_prefix} #{message_subject}"
  begin
    email = @email.compose do
      to Kaya::Support::Configuration.recipients
      subject message_subject
      text_part do
        body message_body
      end
      html_part do
        $K_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!
    $K_LOG.debug "Notification: Email sent to (#{Kaya::Support::Configuration.recipients}) | Subject: '#{message_subject}' | Message: #{message_body}" if $K_LOG
  rescue => e
    $K_LOG.error "Notification: Could not sent email to (#{Kaya::Support::Configuration.recipients}) | Subject: '#{message_subject}' | Message: #{message_body} | #{e}\n #{e.backtrace}" if $K_LOG
  end

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