Class: SimpleWS::Jobs::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/simplews/notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, host, ws, options = {}) ⇒ Notifier


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simplews/notifier.rb', line 23

def initialize(name, host, ws, options = {})
  @host = host
  @name   = name
  @ws     = ws
  @smtp_host  = options[:smtp_host] || 'localhost'
  @smtp_port  = options[:smtp_port] || 25
  @sleep_time = options[:sleep_time] || 2
  @filename   = options[:filename]
  
  if @filename && File.exists?(@filename)
    @jobs = Marshal.load(File.open(@filename))
  else
    @jobs = {}
  end
end

Instance Method Details

#add_job(job_id, email) ⇒ Object


39
40
41
42
# File 'lib/simplews/notifier.rb', line 39

def add_job(job_id, email)
  @jobs[job_id] = email
  File.open(@filename, 'w') do |f| f.write Marshal.dump(@jobs) end if @filename
end

#delete_job(job_id) ⇒ Object


44
45
46
47
# File 'lib/simplews/notifier.rb', line 44

def delete_job(job_id)
  @jobs.delete(job_id)
  File.open(@filename, 'w') do |f| f.write Marshal.dump(@jobs) end if @filename
end

#driverObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/simplews/notifier.rb', line 8

def driver
  case
  when String === @ws
    if File.exists? @ws
      return SOAP::WSDLDriverFactory.new(@ws).create_rpc_driver
    end
  when Array === @ws
    return SimpleWS.get_driver(@ws[0], @ws[1])
  when SOAP::WSDLDriverFactory === @ws
    return @ws
  end

  raise "Do not know how to connect to driver"
end

#error(job_id, email, msg) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simplews/notifier.rb', line 86

def error(job_id, email, msg)
  body ="Dear \#{ @name } user:\n\nYou job with id '\#{ job_id }' has finished with error message:\n\n\#{ msg }\n\nURL: http://\#{@host.chomp}/\#{ job_id }\n\nNote: Do not reply to this message, it is automatically generated.\n"
  send_mail(email, "#{@name} [ERROR]: #{ job_id }", body)
end

#pending?Boolean


114
115
116
# File 'lib/simplews/notifier.rb', line 114

def pending?
  ! @jobs.empty?
end

#processObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simplews/notifier.rb', line 49

def process
  @jobs.each do |job_id, email|
    if driver.done job_id
      begin
        if driver.error job_id
          error(job_id, email, driver.messages(job_id).last)
        else
          success(job_id, email)
        end
      ensure
        delete_job(job_id)
      end
    end
  end
end

#send_mail(to, subject, body) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/simplews/notifier.rb', line 65

def send_mail(to, subject, body)
  puts "Sending mail to #{ to }: #{ subject }"
  message = RMail::Message.new

  from = "[email protected]" + @host.sub(/:.*/,'')

  message.header['To'] = to
  message.header['From'] = from
  message.header['Subject'] = subject
  
  main = RMail::Message.new    
  main.body = body

  message.add_part(main)

  Net::SMTP.start(@smtp_host.chomp, @smtp_port.to_i) do |smtp|
    smtp.send_message message.to_s, from, to
  end
end

#startObject


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/simplews/notifier.rb', line 118

def start
  puts "Starting Email notifier."
  puts "Name: #{ @name }"
  puts "Host: #{ @host }"
  puts "SMTP: #{ @smtp_host }"
  @thread = Thread.new do
    while true
      begin
        process
        sleep @sleep_time
      rescue
        puts $!.message
      end
    end
  end
end

#stopObject


135
136
137
# File 'lib/simplews/notifier.rb', line 135

def stop
  @thread.kill
end

#success(job_id, email) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/simplews/notifier.rb', line 101

def success(job_id, email)
  body ="Dear \#{ @name } user:\n\nYou job with id '\#{ job_id }' has finished successfully:\n\nURL: http://\#{@host.chomp}/\#{ job_id }\n\nNote: Do not reply to this message, it is automatically generated.\n"
  send_mail(email, "#{@name} [SUCCESS]: #{ job_id }", body)
end