diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 210ca627c75049e05500c7d7ea75f925567c504e..921b053b4932a0571824563885ad7e9aae4acb70 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -179,16 +179,27 @@ def catchup_notify_possible_renewal(db) end end -def poll_for_notify(db) +def setup_sentry_scope(name) + Sentry.clone_hub_to_current_thread + Sentry.with_scope do |scope| + scope.clear_breadcrumbs + scope.set_transaction_name(name) + Thread.current[:log] = ::LOG.child(transaction: scope.transaction_name) + yield scope + end +end + +def poll_for_notify(db, repo) db.wait_for_notify_defer.then { |notify| - repo = CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new) - repo.find(notify[:extra]).then do |customer| - DbNotification.for(notify, customer, repo) + setup_sentry_scope("DB NOTIFY") do + repo.find(notify[:extra]).then { |customer| + DbNotification.for(notify, customer, repo) + }.then(&:call).catch { |e| + log.error("Error during poll_for_notify", e) + Sentry.capture_exception(e) + }.sync end - }.then(&:call).catch { |e| - log.error("Error during poll_for_notify", e) - Sentry.capture_exception(e) - }.then { EM.add_timer(0.5) { poll_for_notify(db) } } + }.then { EM.add_timer(0.5) { poll_for_notify(db, repo) } } end def load_plans_to_db! @@ -200,19 +211,6 @@ def load_plans_to_db! end end -def db_notify_listen(conn) - Sentry.with_scope do |scope| - scope.clear_breadcrumbs - scope.set_transaction_name("DB NOTIFY") - Thread.current[:log] = ::LOG.child(transaction: scope.transaction_name) - conn.query("LISTEN low_balance") - conn.query("LISTEN possible_renewal") - catchup_notify_low_balance(conn) - catchup_notify_possible_renewal(conn) - poll_for_notify(conn).sync - end -end - when_ready do log.info "Ready" BLATHER = self @@ -223,10 +221,13 @@ when_ready do TEL_SELECTIONS = TelSelections.new DB.hold do |conn| - EMPromise.resolve(nil).then do - Sentry.clone_hub_to_current_thread - db_notify_listen(conn) - end + conn.query("LISTEN low_balance") + conn.query("LISTEN possible_renewal") + catchup_notify_low_balance(conn) + catchup_notify_possible_renewal(conn) + + repo = CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new, db: conn) + poll_for_notify(conn, repo) end load_plans_to_db!