logentries/le_ruby — pull request #23 by tardate (2014-05-28 14:11:33+00:00)
Title: Clear the log queue and terminate async worker cleanly on process shutdown

This is a suggested fix for issue #18 - seems to work well in both Rails and non-Rails environments. Sorry, no test for this yet (it's complicated!). Here's the simple manual test:

*$ ruby shutdown_test.rb*

require 'le'
logger = Le.new('my logentries key', debug: true, local: true)
20.times do |n|
logger.info "logging #{n+1}/20 @ #{Time.now}"
end

Without this fix, most if not all of the 20 log messages will never get to logentries because the process dies first. But with this fix, we wait for them to get posted. As seen here:

*$ tail logentriesGem.log*

D, [2014-05-28T21:47:43.240303 #77353] DEBUG -- : LE: Asynchronous socket writer started
D, [2014-05-28T21:47:43.240349 #77353] DEBUG -- : LE: Reopening connection to Logentries API server
D, [2014-05-28T21:47:43.241263 #77353] DEBUG -- : LE: commencing shutdown, queue has 21 entries to clear
D, [2014-05-28T21:47:43.956915 #77353] DEBUG -- : LE: Connection established
D, [2014-05-28T21:47:43.957133 #77353] DEBUG -- : LE: Closing Asyncrhonous socket writer
D, [2014-05-28T21:47:44.044244 #77353] DEBUG -- : LE: shutdown complete, queue is empty with 0 entries

Note that I've pushed this on top of PR #22 although there's no real dependency