From 5666bd3b645393686392bd46b7ff6b0c608d0959 Mon Sep 17 00:00:00 2001 From: lalopsb Date: Wed, 10 Mar 2021 16:28:39 -0300 Subject: [PATCH] Enforce immediate join of thread and fix linting errors --- lib/exception_hunter/tracking.rb | 10 +++++++--- spec/exception_hunter_spec.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/exception_hunter/tracking.rb b/lib/exception_hunter/tracking.rb index fdde75a..14ae607 100644 --- a/lib/exception_hunter/tracking.rb +++ b/lib/exception_hunter/tracking.rb @@ -19,7 +19,11 @@ module Tracking # @param [User] user in the current session. (optional) # @return [void] def track(exception, custom_data: {}, user: nil) - open_transactions? ? create_error_within_new_thread(exception, custom_data, user) : create_error(exception, custom_data, user) + if open_transactions + create_error_within_new_thread(exception, custom_data, user) + else + create_error(exception, custom_data, user) + end nil end @@ -27,11 +31,11 @@ def track(exception, custom_data: {}, user: nil) private def create_error_within_new_thread(exception, custom_data, user) - Thread.new do + Thread.new { ActiveRecord::Base.connection_pool.with_connection do create_error(exception, custom_data, user) end - end + }.join end def create_error(exception, custom_data, user) diff --git a/spec/exception_hunter_spec.rb b/spec/exception_hunter_spec.rb index 26da776..692387d 100644 --- a/spec/exception_hunter_spec.rb +++ b/spec/exception_hunter_spec.rb @@ -67,7 +67,7 @@ module ExceptionHunter context 'when the error is tracked within a transaction' do before do allow(ActiveRecord::Base.connection).to receive(:open_transactions).and_return(1) - allow(Thread).to receive(:new).and_yield + allow(Thread).to receive(:new).and_call_original end it 'creates a new error within a new thread' do