From 38fa2a69ac918fed905bb09bff97519f38eef498 Mon Sep 17 00:00:00 2001 From: Ali Farhadnia Date: Wed, 17 Jul 2024 10:04:45 +0330 Subject: [PATCH] fix: moving task lock befor status check (#107) --- destination/taskservice/handle_task.go | 31 +++++++++++++------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/destination/taskservice/handle_task.go b/destination/taskservice/handle_task.go index 67ff47e..eca3f7c 100644 --- a/destination/taskservice/handle_task.go +++ b/destination/taskservice/handle_task.go @@ -24,9 +24,24 @@ func (s Service) HandleTask(ctx context.Context, newEvent event.ProcessedEvent) newEvent.TracerCarrier = otela.GetCarrierFromContext(ctx) span.AddEvent("start-handle-task") - var taskStatus taskentity.IntegrationDeliveryStatus taskID := newEvent.ID() + unlock, err := s.LockTaskByID(ctx, taskID) + if err != nil { + span.AddEvent("error-on-lock-task", trace.WithAttributes( + attribute.String("error", err.Error()))) + + return err + } + defer func() { + unlockErr := unlock() + if unlockErr != nil { + logger.L().Error(fmt.Sprintf("unlock task failed %s", unlockErr)) + } + }() + + var taskStatus taskentity.IntegrationDeliveryStatus + // Get task status using idempotency in the task service. if taskStatus, err = s.GetTaskStatusByID(ctx, taskID); err != nil { span.AddEvent("error-on-get-task-status", trace.WithAttributes( @@ -44,20 +59,6 @@ func (s Service) HandleTask(ctx context.Context, newEvent event.ProcessedEvent) return nil } - unlock, err := s.LockTaskByID(ctx, taskID) - if err != nil { - span.AddEvent("error-on-lock-task", trace.WithAttributes( - attribute.String("error", err.Error()))) - - return err - } - defer func() { - err = unlock() - if err != nil { - logger.L().Error(fmt.Sprintf("unlock task failed %s", err)) - } - }() - span.AddEvent("task-status-retrieved", trace.WithAttributes( attribute.String("status", taskStatus.String()), ))