Provide task queue registration and service container injection.

This commit is contained in:
mikestefanello 2024-06-19 13:53:44 -04:00
parent 0d2ad6e936
commit 912ae2ca6b
5 changed files with 96 additions and 78 deletions

View file

@ -1,9 +1,36 @@
package tasks
import (
"context"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/services"
)
// ExampleTask is an example implementation of services.Task
// This represents the task that can be queued for execution via the task client and should contain everything
// that your queue subscriber needs to process the task.
type ExampleTask struct {
Message string
}
// Name satisfies the services.Task interface by proviing a unique name for this Task type
func (t ExampleTask) Name() string {
return "example_task"
}
// 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) services.Queue {
return services.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
})
}

10
pkg/tasks/register.go Normal file
View file

@ -0,0 +1,10 @@
package tasks
import (
"github.com/mikestefanello/pagoda/pkg/services"
)
// Register registers all task queues with the task client
func Register(c *services.Container) {
c.Tasks.Register(NewExampleTaskQueue(c))
}