Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] asynq.TaskID does not work for periodic tasks #912

Open
bayoumymac opened this issue Aug 31, 2024 · 0 comments
Open

[BUG] asynq.TaskID does not work for periodic tasks #912

bayoumymac opened this issue Aug 31, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@bayoumymac
Copy link

bayoumymac commented Aug 31, 2024

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)
				}
			},
		})
@bayoumymac bayoumymac added the bug Something isn't working label Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants