From a765879a7f56649538a4c7d546361c5cd129e37a Mon Sep 17 00:00:00 2001 From: Jason Hill Date: Thu, 19 Feb 2026 17:00:13 -0500 Subject: [PATCH] WA-NEW-002: guard callback workers + truncate all Mongoid clients in tests --- .../workers/workarea/bust_navigation_cache.rb | 2 ++ .../workers/workarea/generate_promo_codes.rb | 2 ++ .../workers/workarea/index_categorization.rb | 2 ++ .../workers/workarea/index_product_rule.rb | 2 ++ .../workarea/mark_discounts_as_redeemed.rb | 2 ++ .../workers/workarea/save_order_metrics.rb | 2 ++ .../workarea/save_user_order_details.rb | 2 ++ .../app/workers/workarea/send_refund_email.rb | 2 ++ .../workarea/synchronize_user_metrics.rb | 5 ++++ testing/lib/workarea/test_case.rb | 27 ++++++++++++++++++- 10 files changed, 47 insertions(+), 1 deletion(-) diff --git a/core/app/workers/workarea/bust_navigation_cache.rb b/core/app/workers/workarea/bust_navigation_cache.rb index 79eb60d39c..d3ed20fe32 100644 --- a/core/app/workers/workarea/bust_navigation_cache.rb +++ b/core/app/workers/workarea/bust_navigation_cache.rb @@ -13,6 +13,8 @@ def perform(id) taxon = Navigation::Taxon.find(id) taxon.ancestors.each(&:touch) taxon.descendants.each(&:touch) + rescue Mongoid::Errors::DocumentNotFound + nil end end end diff --git a/core/app/workers/workarea/generate_promo_codes.rb b/core/app/workers/workarea/generate_promo_codes.rb index 8095b6738d..dc6765eaed 100644 --- a/core/app/workers/workarea/generate_promo_codes.rb +++ b/core/app/workers/workarea/generate_promo_codes.rb @@ -7,6 +7,8 @@ class GeneratePromoCodes def perform(id) Pricing::Discount::CodeList.find(id).generate_promo_codes! + rescue Mongoid::Errors::DocumentNotFound + nil end end end diff --git a/core/app/workers/workarea/index_categorization.rb b/core/app/workers/workarea/index_categorization.rb index 9aa6aac639..4e960e56d5 100644 --- a/core/app/workers/workarea/index_categorization.rb +++ b/core/app/workers/workarea/index_categorization.rb @@ -15,6 +15,8 @@ def self.perform(category) def perform(id) category = Catalog::Category.find(id) self.class.perform(category) + rescue Mongoid::Errors::DocumentNotFound + nil end end end diff --git a/core/app/workers/workarea/index_product_rule.rb b/core/app/workers/workarea/index_product_rule.rb index c5799197ec..59813db5bc 100644 --- a/core/app/workers/workarea/index_product_rule.rb +++ b/core/app/workers/workarea/index_product_rule.rb @@ -17,6 +17,8 @@ class IndexProductRule def perform(id) product_list = Catalog::Category.find(id) IndexCategorization.perform(product_list) + rescue Mongoid::Errors::DocumentNotFound + nil end end end diff --git a/core/app/workers/workarea/mark_discounts_as_redeemed.rb b/core/app/workers/workarea/mark_discounts_as_redeemed.rb index f6ba9e55a3..b762319375 100644 --- a/core/app/workers/workarea/mark_discounts_as_redeemed.rb +++ b/core/app/workers/workarea/mark_discounts_as_redeemed.rb @@ -9,6 +9,8 @@ def perform(order_id) order = Order.find(order_id) shippings = Shipping.where(order_id: order_id).to_a mark_redeemed(order, shippings) + rescue Mongoid::Errors::DocumentNotFound + nil end def mark_redeemed(order, shippings) diff --git a/core/app/workers/workarea/save_order_metrics.rb b/core/app/workers/workarea/save_order_metrics.rb index ad6f95c5c1..cfb667e536 100644 --- a/core/app/workers/workarea/save_order_metrics.rb +++ b/core/app/workers/workarea/save_order_metrics.rb @@ -36,6 +36,8 @@ def save_user(metrics) def perform(order_id) self.class.perform(Order.find(order_id)) + rescue Mongoid::Errors::DocumentNotFound + nil end end end diff --git a/core/app/workers/workarea/save_user_order_details.rb b/core/app/workers/workarea/save_user_order_details.rb index bc6d779501..f6ab9dfa49 100644 --- a/core/app/workers/workarea/save_user_order_details.rb +++ b/core/app/workers/workarea/save_user_order_details.rb @@ -13,6 +13,8 @@ def perform(order_id) save_payment_details(order, user) save_shipping_details(order, user) + rescue Mongoid::Errors::DocumentNotFound + nil end def save_payment_details(order, user) diff --git a/core/app/workers/workarea/send_refund_email.rb b/core/app/workers/workarea/send_refund_email.rb index 89e4d86d4e..38ea534092 100644 --- a/core/app/workers/workarea/send_refund_email.rb +++ b/core/app/workers/workarea/send_refund_email.rb @@ -12,6 +12,8 @@ def perform(id) refund = Payment::Refund.find(id) return if refund.total.zero? Storefront::PaymentMailer.refunded(id.to_s).deliver_now + rescue Mongoid::Errors::DocumentNotFound + nil end end end diff --git a/core/app/workers/workarea/synchronize_user_metrics.rb b/core/app/workers/workarea/synchronize_user_metrics.rb index 105d82ebb6..e9952c1e85 100644 --- a/core/app/workers/workarea/synchronize_user_metrics.rb +++ b/core/app/workers/workarea/synchronize_user_metrics.rb @@ -31,6 +31,11 @@ def perform(id) }, upsert: true ) + rescue Mongoid::Errors::DocumentNotFound + # Callback workers may execute after the underlying document has been + # removed (e.g. test cleanup / truncation). Missing documents should be + # treated as a no-op. + nil end end end diff --git a/testing/lib/workarea/test_case.rb b/testing/lib/workarea/test_case.rb index 2d073bfd65..8a7e7c93f9 100644 --- a/testing/lib/workarea/test_case.rb +++ b/testing/lib/workarea/test_case.rb @@ -71,10 +71,23 @@ module Workers module SearchIndexing extend ActiveSupport::Concern + def wait_for_elasticsearch!(timeout: 30) + start = Time.now + + loop do + Workarea.elasticsearch.cluster.health(wait_for_status: "yellow") + break + rescue Faraday::ConnectionFailed, ::Elasticsearch::Transport::Transport::Errors::ServiceUnavailable, ::Elasticsearch::Transport::Transport::Errors::BadGateway + raise if (Time.now - start) > timeout + sleep 0.5 + end + end + included do setup do Workarea.config.auto_refresh_search = true WebMock.disable_net_connect!(allow_localhost: true) + wait_for_elasticsearch! Workarea::Elasticsearch::Document.all.each(&:reset_indexes!) Workarea::Search::Storefront.ensure_dynamic_mappings end @@ -241,9 +254,21 @@ module Setup extend ActiveSupport::Concern include ActiveJob::TestHelper + def truncate_all_mongoid_clients! + # Mongoid.truncate! only truncates the global (default) client. + # Workarea uses additional clients (e.g. :metrics), so ensure we clear + # data for all configured clients to avoid cross-test pollution. + Mongoid::Clients.clients.values.each do |client| + client.database.collections.each do |collection| + next if collection.name.start_with?('system.') + collection.find.delete_many + end + end + end + included do setup do - Mongoid.truncate! + truncate_all_mongoid_clients! Workarea.redis.flushdb WebMock.disable_net_connect!(allow_localhost: true) ActionMailer::Base.deliveries.clear