saasitone/pkg/tasks/example.go

57 lines
1.7 KiB
Go
Raw Permalink Normal View History

package tasks
import (
"context"
"time"
"github.com/mikestefanello/backlite"
2024-07-09 17:57:05 -07:00
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
)
// ExampleTask is an example implementation of backlite.Task
// This represents the task that can be queued for execution via the task client and should contain everything
// that your queue processor needs to process the task.
type ExampleTask struct {
Message string
}
// Config satisfies the backlite.Task interface by providing configuration for the queue that these items will be
func (t ExampleTask) Name() string {
// placed into for execution.
return "example_task"
}
func (t ExampleTask) Config() backlite.QueueConfig {
return backlite.QueueConfig{
Name: "ExampleTask",
MaxAttempts: 3,
Timeout: 5 * time.Second,
Backoff: 10 * time.Second,
Retention: &backlite.Retention{
Duration: 24 * time.Hour,
OnlyFailed: false,
Data: &backlite.RetainData{
OnlyFailed: false,
},
},
}
}
// NewExampleTaskQueue provides a Queue that can process ExampleTask tasks
// The service container is provided so the subscriber can have access to the app dependencies.
// All queues must be registered in the Register() function.
// Whenever an ExampleTask is added to the task client, it will be queued and eventually sent here for execution.
func NewExampleTaskQueue(c *services.Container) backlite.Queue {
return backlite.NewQueue[ExampleTask](func(ctx context.Context, task ExampleTask) error {
log.Default().Info("Example task received",
"message", task.Message,
)
log.Default().Info("This can access the container for dependencies",
"echo", c.Web.Reverse("home"),
)
return nil
})
}