2022-11-30 10:51:24 +01:00
|
|
|
package tasker
|
|
|
|
|
|
|
|
import (
|
2023-02-24 17:46:46 +01:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
const (
|
2023-03-06 09:18:41 +01:00
|
|
|
taskTimeout = 60 * time.Second
|
|
|
|
taskRetention = 48 * time.Hour
|
2023-02-20 10:56:30 +01:00
|
|
|
)
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
type TaskerClientOpts struct {
|
2023-03-06 09:18:41 +01:00
|
|
|
RedisPool *redis.RedisPool
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type TaskerClient struct {
|
2023-03-06 09:18:41 +01:00
|
|
|
Client *asynq.Client
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTaskerClient(o TaskerClientOpts) *TaskerClient {
|
|
|
|
return &TaskerClient{
|
|
|
|
Client: asynq.NewClient(o.RedisPool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 07:49:09 +01:00
|
|
|
func (c *TaskerClient) CreateTask(ctx context.Context, taskName TaskName, queueName QueueName, task *Task, extraOpts ...asynq.Option) (*asynq.TaskInfo, error) {
|
2022-11-30 10:51:24 +01:00
|
|
|
if task.Id == "" {
|
|
|
|
task.Id = uuid.NewString()
|
|
|
|
}
|
|
|
|
|
2023-03-08 07:49:09 +01:00
|
|
|
defaultOpts := []asynq.Option{
|
2022-11-30 10:51:24 +01:00
|
|
|
asynq.Queue(string(queueName)),
|
|
|
|
asynq.TaskID(task.Id),
|
2023-03-06 09:18:41 +01:00
|
|
|
asynq.Retention(taskRetention),
|
|
|
|
asynq.Timeout(taskTimeout),
|
2023-03-08 07:49:09 +01:00
|
|
|
}
|
2023-03-08 07:59:10 +01:00
|
|
|
defaultOpts = append(defaultOpts, extraOpts...)
|
2023-03-08 07:49:09 +01:00
|
|
|
|
|
|
|
qTask := asynq.NewTask(
|
|
|
|
string(taskName),
|
|
|
|
task.Payload,
|
2023-03-08 07:59:10 +01:00
|
|
|
defaultOpts...,
|
2022-11-30 10:51:24 +01:00
|
|
|
)
|
|
|
|
|
2023-02-24 17:46:46 +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
|
|
|
|
}
|