From 2aa10309d08ff92efd4cc8bd5cb4c4915965e8da Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 19 Jul 2022 10:46:55 -0500 Subject: [PATCH 1/2] Don't run our whole app in an at_exit We don't use much of it, and this makes using other at_exit hooks more realistic. --- sgx_jmp.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sgx_jmp.rb b/sgx_jmp.rb index a402a2fcfdaef09d5d711e6376e5b1edcae5dc5a..ecd50ec2b486a945a8cb90d6d5c67157f48fd264 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -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" @@ -897,3 +896,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 } From 21848ff91b29df14b8e78f1b1994ad2d2a671d2a Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 19 Jul 2022 09:38:31 -0500 Subject: [PATCH 2/2] Log in background thread File IO is fast, but can still block reactor. --- .rubocop.yml | 1 + lib/background_log.rb | 29 +++++++++++++++++++++++++++++ sgx_jmp.rb | 4 +++- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/background_log.rb diff --git a/.rubocop.yml b/.rubocop.yml index 325e60072c58e5aa59e626e8c6fab731fb62d190..f46799f2d0b408ff96eef0863d5f947f496eae2a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -29,6 +29,7 @@ Metrics/ParameterLists: Naming/MethodParameterName: AllowNamesEndingInNumbers: false AllowedNames: + - io - m - e - q diff --git a/lib/background_log.rb b/lib/background_log.rb new file mode 100644 index 0000000000000000000000000000000000000000..617a4f1d5b6feb3149a55ea2d7c386b352e43cf1 --- /dev/null +++ b/lib/background_log.rb @@ -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 diff --git a/sgx_jmp.rb b/sgx_jmp.rb index ecd50ec2b486a945a8cb90d6d5c67157f48fd264..50c176476e1f7128b4427bc08ace39c5dae68ffc 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -14,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,