Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
refactor(BUX-411): taskq comments & error if runEveryPeriod < 1sec
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Dec 20, 2023
1 parent 13d9ba1 commit e830802
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions taskmanager/taskq.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type TaskRunOptions struct {
TaskName string // Name of the task
}

func (runOptions *TaskRunOptions) runImmediately() bool {
return runOptions.RunEveryPeriod == 0
}

// loadTaskQ will load TaskQ based on the Factory Type and configuration set by the client loading
func (c *TaskManager) loadTaskQ(ctx context.Context) error {
// Check for a valid config (set on client creation)
Expand Down Expand Up @@ -136,14 +140,10 @@ func (c *TaskManager) RunTask(ctx context.Context, options *TaskRunOptions) erro
// Task message will be used to add to the queue
taskMessage := task.WithArgs(ctx, options.Arguments...)

// There are two ways to run a task:
// Option 1: Run the task immediately
if options.RunEveryPeriod == 0 {
if options.runImmediately() {
return c.options.taskq.queue.Add(taskMessage)
}

// Option 2: Run the task periodically using cron
// Note: The first run will be after the period has passed
// Note: The first scheduled run will be after the period has passed
return c.scheduleTaskWithCron(ctx, task, taskMessage, options.RunEveryPeriod)
}

Expand All @@ -156,7 +156,7 @@ func (c *TaskManager) scheduleTaskWithCron(ctx context.Context, task *taskq.Task

// The runEveryPeriod should be greater than 1 second
if runEveryPeriod < 1*time.Second {
runEveryPeriod = 1 * time.Second
return fmt.Errorf("runEveryPeriod should be greater than 1 second")
}

// Lock time is the period minus 500ms to allow for some clock drift
Expand Down

0 comments on commit e830802

Please sign in to comment.