~netlandish/gobwebs

69a77da4a57f37aab65f312c6f78f841a25db519 — Peter Sanchez 2 years ago 946744a
Adding task ID helper
2 files changed, 24 insertions(+), 5 deletions(-)

M email/email.go
A tasks.go
M email/email.go => email/email.go +10 -5
@@ 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())
		}
	})
}

A tasks.go => tasks.go +14 -0
@@ 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()
	}
}