[GH-ISSUE #912] [BUG] asynq.TaskID does not work for periodic tasks #2481

Open
opened 2026-03-15 20:37:41 +03:00 by kerem · 0 comments
Owner

Originally created by @bayoumymac on GitHub (Aug 31, 2024).
Original GitHub issue: https://github.com/hibiken/asynq/issues/912

Originally assigned to: @hibiken on GitHub.

Describe the bug
asynq.TaskID doesn't work for periodic tasks as it can only be passed when setting up the scheduler

To Reproduce
setting up a scheduler using the following, does not generate a separate taskId per periodic task enqueued

        scheduler.Register(
		interval,
		asynq.NewTask(task, nil),
		asynq.Queue(queue),
		asynq.TaskID(primitive.NewObjectID().Hex()), // mongodb object id as task id
	)

Expected behavior
ability to set taskId for periodic tasks, there are multiple solutions

  1. change the signature of preEnqueueFunc to return *asynq.Task (most flexible, allows to customize all different attributes)
  2. add a scheduler option such as TaskOpts which can be a func with the following signature func() []asynq.Option {} where it gets executed for every task

bonus: have a validation check on options passed to scheduler.Register and fail if option is not supported

Environment (please complete the following information):

  • OS: N/A
  • Version of asynq package v0.24.0

Additional context
currently this solution works but but definitely is an anti pattern

		scheduler := asynq.NewScheduler(opts, &asynq.SchedulerOpts{
			PreEnqueueFunc: func(task *asynq.Task, opts []asynq.Option) {
				if task != nil {
					*task = *asynq.NewTask(
                                            task.Type(),
                                            task.Payload(),
                                            asynq.TaskID(primitive.NewObjectID().Hex()), // mongodb object id as task id
                                        )
				}
			},
			PostEnqueueFunc: func(task *asynq.TaskInfo, err error) {
				logger.Debugf("enqueued task %s", task.ID) // logs the correct task id
				if err != nil {
					logger.Errorf("error while enqueuing task %s", err)
				}
			},
		})
Originally created by @bayoumymac on GitHub (Aug 31, 2024). Original GitHub issue: https://github.com/hibiken/asynq/issues/912 Originally assigned to: @hibiken on GitHub. **Describe the bug** asynq.TaskID doesn't work for periodic tasks as it can only be passed when setting up the scheduler **To Reproduce** setting up a scheduler using the following, does not generate a separate taskId per periodic task enqueued ```go scheduler.Register( interval, asynq.NewTask(task, nil), asynq.Queue(queue), asynq.TaskID(primitive.NewObjectID().Hex()), // mongodb object id as task id ) ``` **Expected behavior** ability to set taskId for periodic tasks, there are multiple solutions 1. change the signature of preEnqueueFunc to return *asynq.Task (most flexible, allows to customize all different attributes) 2. add a scheduler option such as TaskOpts which can be a func with the following signature func() []asynq.Option {} where it gets executed for every task bonus: have a validation check on options passed to scheduler.Register and fail if option is not supported **Environment (please complete the following information):** - OS: N/A - Version of `asynq` package v0.24.0 **Additional context** currently this solution works but but definitely is an anti pattern ```go scheduler := asynq.NewScheduler(opts, &asynq.SchedulerOpts{ PreEnqueueFunc: func(task *asynq.Task, opts []asynq.Option) { if task != nil { *task = *asynq.NewTask( task.Type(), task.Payload(), asynq.TaskID(primitive.NewObjectID().Hex()), // mongodb object id as task id ) } }, PostEnqueueFunc: func(task *asynq.TaskInfo, err error) { logger.Debugf("enqueued task %s", task.ID) // logs the correct task id if err != nil { logger.Errorf("error while enqueuing task %s", err) } }, }) ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#2481
No description provided.