Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Logger #66

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/mail_room.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require 'net/imap'
require 'optparse'
require 'yaml'
require 'logger'

module MailRoom
def self.logger
@logger ||= Logger.new(STDOUT)
end

def self.logger=(logger)
@logger = (logger ? logger : Logger.new("/dev/null"))
end
end

require "mail_room/version"
Expand Down
7 changes: 5 additions & 2 deletions lib/mail_room/arbitration/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def deliver?(uid)

incr = nil
redis.multi do |client|
# At this point, `incr` is a future, which will get its value after
# At this point, `incr` is a future, which will get its value after
# the MULTI command returns.
incr = client.incr(key)

Expand All @@ -37,7 +37,10 @@ def deliver?(uid)
# we are the first mail_room to try to deliver this message, so we get to.
# If we get any other value, another mail_room already (tried to) deliver
# the message, so we don't have to anymore.
incr.value == 1
result = incr.value == 1
MailRoom.logger.info("Arbitration result for #{uid} = #{result}")

result
end

private
Expand Down
2 changes: 2 additions & 0 deletions lib/mail_room/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ def initialize(args)
# Start the coordinator running, sets up signal traps
def start
Signal.trap(:INT) do
logger.info("Interrupt signal received")
coordinator.running = false
end

Signal.trap(:TERM) do
logger.info("Terminate signal received")
exit
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mail_room/coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def initialize(mailboxes)
# start each of the watchers to running
def run
watchers.each(&:run)

self.running = true

sleep_while_running
ensure
quit
Expand Down
3 changes: 1 addition & 2 deletions lib/mail_room/delivery/postback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ def deliver(message)
connection.post do |request|
request.url @delivery_options.delivery_url
request.body = message
# request.options[:timeout] = 3
# request.headers['Content-Type'] = 'text/plain'
end

MailRoom.logger.info("Message delivered to #{@delivery_options.delivery_url}")
true
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/mail_room/delivery/que.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(options)
# @param message [String] the email message as a string, RFC822 format
def deliver(message)
queue_job(message)
MailRoom.logger.info("Message pushed onto Que queue")
end

private
Expand Down
1 change: 1 addition & 0 deletions lib/mail_room/delivery/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def deliver(message)

client.lpush("queue:#{options.queue}", JSON.generate(item))

MailRoom.logger.info("Message pushed onto Sidekiq queue")
true
end

Expand Down
7 changes: 7 additions & 0 deletions lib/mail_room/mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def arbitrator
end

def deliver?(uid)
MailRoom.logger.info("#{context} Asking #{arbitrator.class.name} if we should deliver #{uid}")

arbitrator.deliver?(uid)
end

Expand All @@ -72,6 +74,7 @@ def deliver(message)
body = message.attr['RFC822']
return true unless body

MailRoom.logger.info("#{context} Delivering #{message.attr['UID']} through #{delivery.class.name}")
delivery.deliver(body)
end

Expand All @@ -80,6 +83,10 @@ def ssl_options
replace_verify_mode(ssl)
end

def context
"[#{self.email}-#{self.name}]:"
end

private

def parsed_arbitration_options
Expand Down
8 changes: 7 additions & 1 deletion lib/mail_room/mailbox_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def process
delivered = @mailbox.deliver(message)

if delivered && @mailbox.delete_after_delivery
MailRoom.logger.info("#{@mailbox.context} Add DELETED flag to #{message.attr['UID']}")
@imap.store(message.seqno, "+FLAGS", [Net::IMAP::DELETED])
end
end
Expand All @@ -43,7 +44,12 @@ def new_messages
# search for all new (unseen) message ids
# @return [Array<Integer>] message ids
def new_message_ids
@imap.uid_search(@mailbox.search_command).select { |uid| @mailbox.deliver?(uid) }
all_unread = @imap.uid_search(@mailbox.search_command)
MailRoom.logger.info("#{@mailbox.context} #{all_unread.count} new messages with ids: #{all_unread.join(', ')}")

to_deliver = all_unread.select { |uid| @mailbox.deliver?(uid) }
MailRoom.logger.info("#{@mailbox.context} #{to_deliver.count} messages to be delivered by this mailroom instance.\n Their ids: #{all_unread.join(', ')}")
to_deliver
end

# @private
Expand Down
12 changes: 11 additions & 1 deletion lib/mail_room/mailbox_watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ def message_exists?(response)

# log in and set the mailbox
def setup
MailRoom.logger.info("#{@mailbox.context} Setting up watcher")
reset

MailRoom.logger.info("#{@mailbox.context} Start TLS session")
start_tls

MailRoom.logger.info("#{@mailbox.context} Logging in to the mailbox")
log_in

MailRoom.logger.info("#{@mailbox.context} Set the mailbox to #{@mailbox.name}")
set_mailbox
end

Expand Down Expand Up @@ -98,6 +105,7 @@ def set_mailbox
def idle
return unless ready_to_idle?

MailRoom.logger.info("#{@mailbox.context} Idling...")
@idling = true

self.timeout_thread = Thread.start do
Expand All @@ -122,7 +130,7 @@ def stop_idling
return unless idling?

imap.idle_done

idling_thread.join
self.idling_thread = nil
end
Expand All @@ -146,6 +154,7 @@ def run
process_mailbox
rescue Net::IMAP::Error, IOError => e
# we've been disconnected, so re-setup
MailRoom.logger.warn("#{@mailbox.context} Disconnected")
setup
end
end
Expand All @@ -163,6 +172,7 @@ def quit

# trigger the handler to process this mailbox for new messages
def process_mailbox
MailRoom.logger.info("#{@mailbox.context} Processing started")
handler.process
end

Expand Down
7 changes: 4 additions & 3 deletions spec/lib/mailbox_watcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@
end

describe '#idle' do
let(:imap) {stub}
let(:watcher) {MailRoom::MailboxWatcher.new(nil)}
let(:mailbox) { MailRoom::Mailbox.new(email: "mep@mep.mep", name: "inbox") }
let(:imap) { stub }
let(:watcher) {MailRoom::MailboxWatcher.new(mailbox) }

before :each do
watcher.stubs(:imap).returns(imap)
Expand All @@ -89,7 +90,7 @@
end

context "when logged in" do
before :each do
before do
imap.stubs(:idle_done)

watcher.stubs(:logged_in?).returns(true)
Expand Down