From 69a77da4a57f37aab65f312c6f78f841a25db519 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Mon, 17 Oct 2022 18:20:37 -0600 Subject: [PATCH] Adding task ID helper --- email/email.go | 15 ++++++++++----- tasks.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tasks.go diff --git a/email/email.go b/email/email.go index 96e929f..a4be771 100644 --- a/email/email.go +++ b/email/email.go @@ -7,6 +7,7 @@ import ( work "git.sr.ht/~sircmpwn/dowork" "github.com/labstack/echo/v4" + "hg.code.netlandish.com/~netlandish/gobwebs" "petersanchez.com/carrier" ) @@ -49,13 +50,17 @@ func (s *ServiceQueue) mailTask(msg *carrier.Message) *work.Task { s.logger.Printf("Error sending mail: %v", err) } return err - }).Retries(5).After(func(ctx context.Context, task *work.Task) { + }).Retries(5).Before(func(ctx context.Context, task *work.Task) { + gobwebs.TaskIDWork(task) + s.logger.Printf( + "Running email task %s for the %d attempt.", task.Metadata["id"], task.Attempts()) + }).After(func(ctx context.Context, task *work.Task) { if task.Result() == nil { - s.logger.Printf("Mail to %s sent after %d attempts", - strings.Join(msg.GetAllRcpts(), ", "), task.Attempts()) + s.logger.Printf("Mail %s to %s sent after %d attempts", + task.Metadata["id"], strings.Join(msg.GetAllRcpts(), ", "), task.Attempts()) } else { - s.logger.Printf("Mail to %s failed after %d attempts: %v", - strings.Join(msg.GetAllRcpts(), ", "), task.Attempts(), task.Result()) + s.logger.Printf("Mail %s to %s failed after %d attempts: %v", + task.Metadata["id"], strings.Join(msg.GetAllRcpts(), ", "), task.Attempts(), task.Result()) } }) } diff --git a/tasks.go b/tasks.go new file mode 100644 index 0000000..46b2da4 --- /dev/null +++ b/tasks.go @@ -0,0 +1,14 @@ +package gobwebs + +import ( + work "git.sr.ht/~sircmpwn/dowork" + "github.com/segmentio/ksuid" +) + +// TaskIDWork sets a task ID via the Before option for a dowork +// task +func TaskIDWork(task *work.Task) { + if _, ok := task.Metadata["id"]; !ok { + task.Metadata["id"] = ksuid.New().String() + } +} -- 2.45.2