cic-custodial/internal/tasker/client.go
Mohammed Sohail 341a760f02
feat: (wip) add account activation and gas quota lock
* This is a crude lock that restricts each account to the set gas quota.
2023-03-08 06:49:09 +00:00

57 lines
1.1 KiB
Go

package tasker
import (
"context"
"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
)
type TaskerClientOpts struct {
RedisPool *redis.RedisPool
}
type TaskerClient struct {
Client *asynq.Client
}
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, extraOpts ...asynq.Option) (*asynq.TaskInfo, error) {
if task.Id == "" {
task.Id = uuid.NewString()
}
defaultOpts := []asynq.Option{
asynq.Queue(string(queueName)),
asynq.TaskID(task.Id),
asynq.Retention(taskRetention),
asynq.Timeout(taskTimeout),
}
taskOpts := append(defaultOpts, extraOpts...)
qTask := asynq.NewTask(
string(taskName),
task.Payload,
taskOpts...
)
taskInfo, err := c.Client.EnqueueContext(ctx, qTask)
if err != nil {
return nil, err
}
return taskInfo, nil
}