Class: MailRoom::MailboxWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_room/mailbox_watcher.rb

Overview

Watch a Mailbox

Author:

  • Tony Pitale

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mailbox) ⇒ MailboxWatcher

Watch a new mailbox

Parameters:



13
14
15
16
17
18
# File 'lib/mail_room/mailbox_watcher.rb', line 13

def initialize(mailbox)
  @mailbox = mailbox

  @running = false
  @connection = nil
end

Instance Attribute Details

#watching_threadObject

Returns the value of attribute watching_thread.



9
10
11
# File 'lib/mail_room/mailbox_watcher.rb', line 9

def watching_thread
  @watching_thread
end

Instance Method Details

#quitObject

stop running, cleanup connection



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mail_room/mailbox_watcher.rb', line 45

def quit
  @mailbox.logger.info({ context: @mailbox.context, action: "Quitting connection..." })
  @running = false

  if @connection
    @connection.quit
    @connection = nil
  end

  @mailbox.logger.info({ context: @mailbox.context, action: "Terminating watching thread..." })

  if self.watching_thread
    thr = self.watching_thread.join(60)
    @mailbox.logger.info({ context: @mailbox.context, action: "Timeout waiting for watching thread" }) unless thr
  end

  @mailbox.logger.info({ context: @mailbox.context, action: "Done with thread cleanup" })
end

#runObject

run the mailbox watcher



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mail_room/mailbox_watcher.rb', line 27

def run
  @mailbox.logger.info({ context: @mailbox.context, action: "Setting up watcher" })
  @running = true

  connection.on_new_message do |message|
    @mailbox.deliver(message)
  end

  self.watching_thread = Thread.start do
    while(running?) do
      connection.wait
    end
  end

  watching_thread.abort_on_exception = true
end

#running?Boolean

are we running?

Returns:

  • (Boolean)


22
23
24
# File 'lib/mail_room/mailbox_watcher.rb', line 22

def running?
  @running
end