From 47fd0327581948e6eae35a0e96eff367799907b9 Mon Sep 17 00:00:00 2001 From: Paulo Felipe Silva de Souza Date: Sat, 6 Jan 2024 11:25:02 -0300 Subject: [PATCH 1/4] chore bling_order_item_creator_job_spec.rb, application_job.rb: move rescue from to application job. this is so to catch the exception in all jobs. --- app/jobs/application_job.rb | 7 +++++++ app/jobs/bling_order_item_creator_base_job.rb | 6 ------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index bef39599..136ac512 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -6,4 +6,11 @@ class ApplicationJob < ActiveJob::Base # Most jobs are safe to ignore if the underlying records are no longer available # discard_on ActiveJob::DeserializationError + queue_as :default + + rescue_from(StandardError) do |exception| + Sentry.capture_message(exception) + end + + retry_on StandardError, wait: :exponentially_longer, attempts: 5 end diff --git a/app/jobs/bling_order_item_creator_base_job.rb b/app/jobs/bling_order_item_creator_base_job.rb index 0fbebbbe..5741f2ed 100644 --- a/app/jobs/bling_order_item_creator_base_job.rb +++ b/app/jobs/bling_order_item_creator_base_job.rb @@ -1,12 +1,6 @@ # frozen_string_literal: true class BlingOrderItemCreatorBaseJob < ApplicationJob - queue_as :default - rescue_from(StandardError) do |exception| - Sentry.capture_message(exception) - end - - retry_on StandardError, wait: :exponentially_longer, attempts: 5 attr_accessor :account_id, :alteration_date From b0051a5fabca308a6d7106399b5bf5bd61947755 Mon Sep 17 00:00:00 2001 From: Paulo Felipe Silva de Souza Date: Sat, 6 Jan 2024 11:25:33 -0300 Subject: [PATCH 2/4] chore order_items_job.rb: queue as items --- app/jobs/order_items_job.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/jobs/order_items_job.rb b/app/jobs/order_items_job.rb index 639edc11..a3ee5c79 100644 --- a/app/jobs/order_items_job.rb +++ b/app/jobs/order_items_job.rb @@ -1,6 +1,5 @@ class OrderItemsJob < ApplicationJob - queue_as :default - retry_on StandardError, attempts: 5, wait: :exponentially_longer + queue_as :items def perform(record) return unless record.items.empty? From 3aefee8216ecee69fdf2fe7b53b34109cebeb6a0 Mon Sep 17 00:00:00 2001 From: Paulo Felipe Silva de Souza Date: Sat, 6 Jan 2024 12:07:37 -0300 Subject: [PATCH 3/4] chore order_items_job.rb: show the error message when raising exception. --- app/jobs/order_items_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/jobs/order_items_job.rb b/app/jobs/order_items_job.rb index a3ee5c79..e1c4993e 100644 --- a/app/jobs/order_items_job.rb +++ b/app/jobs/order_items_job.rb @@ -7,7 +7,7 @@ def perform(record) account_id = record.account_id items_attributes = [] order = Services::Bling::FindOrder.call(id: record.bling_order_id, order_command: 'find_order', tenant: account_id) - raise StandardError if order['error'].present? + raise(StandardError, order['error']) if order['error'].present? order['data']['itens'].each do |item| items_attributes << { From be8651a1fef6d3465fd513c1a63542017493fa90 Mon Sep 17 00:00:00 2001 From: Paulo Felipe Silva de Souza Date: Sat, 6 Jan 2024 13:19:41 -0300 Subject: [PATCH 4/4] refactor order_items_job.rb, order_items_job.rb: Do not call the job to create items if items are already created. --- app/jobs/order_items_job.rb | 2 -- app/models/bling_order_item.rb | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/jobs/order_items_job.rb b/app/jobs/order_items_job.rb index e1c4993e..eaceec4a 100644 --- a/app/jobs/order_items_job.rb +++ b/app/jobs/order_items_job.rb @@ -2,8 +2,6 @@ class OrderItemsJob < ApplicationJob queue_as :items def perform(record) - return unless record.items.empty? - account_id = record.account_id items_attributes = [] order = Services::Bling::FindOrder.call(id: record.bling_order_id, order_command: 'find_order', tenant: account_id) diff --git a/app/models/bling_order_item.rb b/app/models/bling_order_item.rb index 53e6db6d..2ae9a1d4 100644 --- a/app/models/bling_order_item.rb +++ b/app/models/bling_order_item.rb @@ -133,6 +133,8 @@ def value end def synchronize_items + return unless items.empty? + OrderItemsJob.perform_later(self) end end