.rubocop.yml 🔗
@@ -29,6 +29,7 @@ Metrics/ParameterLists:
Naming/MethodParameterName:
AllowNamesEndingInNumbers: false
AllowedNames:
+ - io
- m
- e
- q
Stephen Paul Weber created
* background-log:
Log in background thread
Don't run our whole app in an at_exit
.rubocop.yml | 1 +
lib/background_log.rb | 29 +++++++++++++++++++++++++++++
sgx_jmp.rb | 11 ++++++++---
3 files changed, 38 insertions(+), 3 deletions(-)
@@ -29,6 +29,7 @@ Metrics/ParameterLists:
Naming/MethodParameterName:
AllowNamesEndingInNumbers: false
AllowedNames:
+ - io
- m
- e
- q
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+# Subclass IO because Logger does an is_a? check
+class BackgroundLog < IO
+ def initialize(io)
+ @io = io
+ @q = Queue.new
+ thread
+ at_exit do
+ @q << :done
+ thread.join
+ end
+ end
+
+ def thread
+ @thread ||= Thread.new do
+ loop do
+ m = @q.pop
+ break if m == :done
+
+ @io.write m
+ end
+ end
+ end
+
+ def write(s)
+ @q << s
+ end
+end
@@ -3,8 +3,7 @@
require "pg/em/connection_pool"
require "bandwidth"
require "bigdecimal"
-require "blather/client/dsl" # Require this first to not auto-include
-require "blather/client"
+require "blather/client/dsl"
require "braintree"
require "date"
require "dhall"
@@ -15,8 +14,10 @@ require "ruby-bandwidth-iris"
require "sentry-ruby"
require "statsd-instrument"
+require_relative "lib/background_log"
+
$stdout.sync = true
-LOG = Ougai::Logger.new($stdout)
+LOG = Ougai::Logger.new(BackgroundLog.new($stdout))
LOG.level = ENV.fetch("LOG_LEVEL", "info")
LOG.formatter = Ougai::Formatters::Readable.new(
nil,
@@ -897,3 +898,7 @@ iq type: [:get, :set] do |iq|
self << Blather::StanzaError.new(iq, "feature-not-implemented", :cancel)
end
+
+trap(:INT) { EM.stop }
+trap(:TERM) { EM.stop }
+EM.run { client.run }