cic-custodial/internal/tasker/client.go

52 lines
969 B
Go
Raw Permalink Normal View History

2022-11-30 10:51:24 +01:00
package tasker
import (
"context"
2022-11-30 10:51:24 +01:00
"time"
"github.com/google/uuid"
"github.com/grassrootseconomics/cic-custodial/pkg/redis"
"github.com/hibiken/asynq"
)
const (
taskTimeout = 60 * time.Second
taskRetention = 48 * time.Hour
)
2022-11-30 10:51:24 +01:00
type TaskerClientOpts struct {
RedisPool *redis.RedisPool
2022-11-30 10:51:24 +01:00
}
type TaskerClient struct {
Client *asynq.Client
2022-11-30 10:51:24 +01:00
}
func NewTaskerClient(o TaskerClientOpts) *TaskerClient {
return &TaskerClient{
Client: asynq.NewClient(o.RedisPool),
}
}
func (c *TaskerClient) CreateTask(ctx context.Context, taskName TaskName, queueName QueueName, task *Task) (*asynq.TaskInfo, error) {
2022-11-30 10:51:24 +01:00
if task.Id == "" {
task.Id = uuid.NewString()
}
qTask := asynq.NewTask(
string(taskName),
task.Payload,
asynq.Queue(string(queueName)),
asynq.TaskID(task.Id),
asynq.Retention(taskRetention),
asynq.Timeout(taskTimeout),
2022-11-30 10:51:24 +01:00
)
taskInfo, err := c.Client.EnqueueContext(ctx, qTask)
2022-11-30 10:51:24 +01:00
if err != nil {
return nil, err
}
return taskInfo, nil
}