Default to SQLite rather than Postgres & Redis (#72)

* Initial rough draft switch to sqlite.

* Rewrote cache implemenation.

* Provide typed tasks.

* Task cleanup.

* Use same db for tasks.

* Provide task queue registration and service container injection.

* Added optional delay to tasks. Pool buffers when encoding.

* Added tests for the task client and runner.

* Added handler examples for caching and tasks.

* Cleanup and documentation.

* Use make in workflow.

* Updated documentation.

* Updated documentation.
This commit is contained in:
Mike Stefanello 2024-06-22 10:34:26 -04:00 committed by GitHub
parent 5e9e502b42
commit a096abd195
29 changed files with 956 additions and 910 deletions

View file

@ -2,21 +2,35 @@ package tasks
import (
"context"
"log"
"github.com/hibiken/asynq"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/services"
)
// TypeExample is the type for the example task.
// This is what is passed in to TaskClient.New() when creating a new task
const TypeExample = "example_task"
// ExampleProcessor processes example tasks
type ExampleProcessor struct {
// 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
}
// ProcessTask handles the processing of the task
func (p *ExampleProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error {
log.Printf("executing task: %s", t.Type())
return nil
// 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))
}