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:



11
12
13
14
15
16
# File 'lib/mail_room/mailbox_watcher.rb', line 11

def initialize(mailbox)
  @mailbox = mailbox

  @running = false
  @connection = nil
end

Instance Attribute Details

#watching_threadObject

Returns the value of attribute watching_thread.



7
8
9
# File 'lib/mail_room/mailbox_watcher.rb', line 7

def watching_thread
  @watching_thread
end

Instance Method Details

#quitObject

stop running, cleanup connection



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mail_room/mailbox_watcher.rb', line 43

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

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

  if self.watching_thread
    self.watching_thread.join
  end
end

#runObject

run the mailbox watcher



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

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)


20
21
22
# File 'lib/mail_room/mailbox_watcher.rb', line 20

def running?
  @running
end