From ea41bef94244f5e7e97720fb39aefc3167fe0aaa Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 27 Aug 2024 14:21:33 +0200 Subject: [PATCH] fix: move before new context gets created --- pkg/plug/grpc.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/plug/grpc.go b/pkg/plug/grpc.go index 4de7628..6ba590b 100644 --- a/pkg/plug/grpc.go +++ b/pkg/plug/grpc.go @@ -58,18 +58,10 @@ func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation) } span.AddEvent("Retrieved user source", trace.WithAttributes(attribute.String("user_source_id", creation.UserSourceId))) - // gRPC closes the context after the call ended. So the whole scrapping stopped without waiting - // by using this method we assign a new context to each new request we get. - // This can be used for example to close the context with the given id - ctx = trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{TraceID: span.SpanContext().TraceID()})) - taskCtx, cancel := context.WithCancel(ctx) - s.ctx[id] = cancel - span.AddEvent("Created new context for task", trace.WithAttributes(attribute.String("task_id", id))) - if !userSource.AccountValidate { err = errors.New("user is not validated") - log.WithContext(taskCtx).WithError(err).WithField("task_id", id).Error("Task execution failed") + log.WithContext(ctx).WithError(err).WithField("task_id", id).Error("Task execution failed") span.RecordError(err) span.SetStatus(codes.Error, err.Error()) @@ -78,6 +70,14 @@ func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation) return &plugTaskState, err } + // gRPC closes the context after the call ended. So the whole scrapping stopped without waiting + // by using this method we assign a new context to each new request we get. + // This can be used for example to close the context with the given id + ctx = trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{TraceID: span.SpanContext().TraceID()})) + taskCtx, cancel := context.WithCancel(ctx) + s.ctx[id] = cancel + span.AddEvent("Created new context for task", trace.WithAttributes(attribute.String("task_id", id))) + log.WithContext(taskCtx).WithFields(log.Fields{ "task_id": id, "user_source_id": creation.UserSourceId,