2022-02-08 05:58:22 -08:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-27 11:52:05 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mikestefanello/backlite"
|
2022-02-08 05:58:22 -08:00
|
|
|
|
2024-07-09 17:57:05 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
|
2022-02-08 05:58:22 -08:00
|
|
|
)
|
|
|
|
|
2024-07-27 11:52:05 -07:00
|
|
|
// ExampleTask is an example implementation of backlite.Task
|
2024-06-22 07:34:26 -07:00
|
|
|
// This represents the task that can be queued for execution via the task client and should contain everything
|
2024-07-27 11:52:05 -07:00
|
|
|
// that your queue processor needs to process the task.
|
2024-06-22 07:34:26 -07:00
|
|
|
type ExampleTask struct {
|
|
|
|
Message string
|
|
|
|
}
|
2022-02-08 05:58:22 -08:00
|
|
|
|
2024-07-27 11:52:05 -07:00
|
|
|
// Config satisfies the backlite.Task interface by providing configuration for the queue that these items will be
|
2024-06-22 07:34:26 -07:00
|
|
|
func (t ExampleTask) Name() string {
|
2024-07-27 11:52:05 -07:00
|
|
|
// placed into for execution.
|
2024-06-22 07:34:26 -07:00
|
|
|
return "example_task"
|
2022-02-08 05:58:22 -08:00
|
|
|
}
|
|
|
|
|
2024-07-27 11:52:05 -07:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 07:34:26 -07:00
|
|
|
// 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.
|
2024-07-27 11:52:05 -07:00
|
|
|
func NewExampleTaskQueue(c *services.Container) backlite.Queue {
|
|
|
|
return backlite.NewQueue[ExampleTask](func(ctx context.Context, task ExampleTask) error {
|
2024-06-22 07:34:26 -07:00
|
|
|
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
|
|
|
|
})
|
2022-02-08 05:58:22 -08:00
|
|
|
}
|