Class: MailDaemon::Handler

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Handler

Returns a new instance of Handler.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mail_daemon.rb', line 18

def initialize(options)
  setup_options(options)
  default_option :connections, []
  default_option :debug, false

  puts "Setting up Signal Traps" if @options[:debug]
  Signal.trap("INT") {
    Thread.new {self.stop}.join
  }
  # Trap `Kill `
  Signal.trap("TERM") {
    Thread.new {self.stop}.join
  }
end

Instance Method Details

#reload(options) ⇒ Object



52
53
54
55
# File 'lib/mail_daemon.rb', line 52

def reload(options)
  reload_options(options)
  self.restart
end

#restartObject



33
34
35
36
37
38
39
40
# File 'lib/mail_daemon.rb', line 33

def restart
  puts "Restarting... " if @options[:debug]
  self.stop if self.running?
  Thread.new do
    puts "Starting in new thread " if @options[:debug]
    self.start(&@client_handler_block)
  end
end

#running?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mail_daemon.rb', line 42

def running?
  @watchers && @watchers.detect{|watcher| watcher.running?}
end

#start(&block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mail_daemon.rb', line 57

def start(&block)
  @watchers = []

  @client_handler_block = block

  @options[:connections].each do |mailbox|
    mailbox[:ssl_options] = {:verify_mode => OpenSSL::SSL::VERIFY_NONE} if mailbox[:ssl]

    puts "Setting up watcher for #{mailbox[:username]}" if @options[:debug]
    @watchers << MailDaemon::Imap::Watcher.new(mailbox.merge(:debug => @options[:debug]))
  end
  @threads = []
  @watchers.each do |watcher|
    @threads << Thread.new do
      watcher.start do |message|
        type = message.delete(:type)
        yield message, type
      end
    end
  end

  @threads.map{|t| t.join}

end

#stopObject

Signal catching



47
48
49
50
# File 'lib/mail_daemon.rb', line 47

def stop
  @watchers.map{|watcher| watcher.stop}
  @threads.map{|t| t.kill }
end