From 53b7e99882ff3cabc097a2e78e0151e14f7ef267 Mon Sep 17 00:00:00 2001 From: Sinh Date: Mon, 8 Jul 2024 16:17:11 +0700 Subject: [PATCH] feat: refactor task queue system for improved efficiency - Add a new method `Enqueue` to enqueue tasks with specific options Signed-off-by: Sinh --- task.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/task.go b/task.go index 968207d..ffbfdef 100644 --- a/task.go +++ b/task.go @@ -38,6 +38,36 @@ func (i Instance) RunTask(typename string, payload []byte, priority string, retr return i.Client.Enqueue(task, options...) } +func (i Instance) Enqueue(typename string, payload []byte, priority string, retryTimes int, processIn time.Duration) (*asynq.TaskInfo, error) { + // Create task and options + task := asynq.NewTask(typename, payload) + options := make([]asynq.Option, 0) + + // Priority + if priority != PriorityCritical && priority != PriorityDefault && priority != PriorityLow { + priority = PriorityDefault + } + priority = i.Config.QueuePrefix + priority + options = append(options, asynq.Queue(priority)) + + // Retry times + if retryTimes < 0 { + retryTimes = 0 + } + options = append(options, asynq.MaxRetry(retryTimes)) + + // Task timeout + if i.Config.TaskTimeout > 0 { + options = append(options, asynq.Timeout(i.Config.TaskTimeout)) + } + if processIn > 0 { + options = append(options, asynq.ProcessIn(processIn)) + } + + // Enqueue task + return i.Client.Enqueue(task, options...) +} + // ScheduledTask create new task and run at specific time // cronSpec follow cron expression // https://www.freeformatter.com/cron-expression-generator-quartz.html