[GH-ISSUE #139] [FEATURE REQUEST] Set default options for each task type in Client #40

Closed
opened 2026-03-02 05:18:09 +03:00 by kerem · 0 comments
Owner

Originally created by @hibiken on GitHub (Apr 24, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/139

Originally assigned to: @hibiken on GitHub.

**Is your feature request related to a problem?
It's tedious and error-prone to specify Queue name as an Option when enqueuing a task.
If a certain type of task needs to go into a specific queue, it's better to specify that once and not every time enqueueing that type of task.

This also applies to other options as well. It's more convenient and common to say
for this type of task, set timeout to be this duration, or
for this type of task, set unique TTL to be this duration.

Describe the solution you'd like
Add a method to Client to set default options for a type.

func (c *Client) SetDefaultOptions(pattern string, opts ...Option)

Example:

c := asynq.NewClient(r)
// Routes maps task types to queue names.
c.SetDefaultOptions("web:email", asynq.Queues("web"))
c.SetDefaultOptions("feed:import", asynq.Queues("feed"), asynq.Timeout(time.Hour))

feedTask := asynq.NewTask("feed:import", payload)
c.Enqueue(feedTask) // Should be routed to "feed" queue.

webTask := asynq.NewTask("web:email", payload)
c.Enqueue(webTask) // Should be routed to "web" queue.

otherTask := asynq.NewTask("other:task", payload)
c.Enqueue(otherTask) // Should be routed to "default" queue. No matching routing so use "default" queue.

// You should be able to override routing at enqueue time.
feedTask := asynq.NewTask("feed:tasks", payload)
c.Enqueue(feedTask, asynq.Queue("other_queue"))

Describe alternatives you've considered
None for now.

Additional context
Inspiration: Celery has a task routing configuration option.

Originally created by @hibiken on GitHub (Apr 24, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/139 Originally assigned to: @hibiken on GitHub. **Is your feature request related to a problem? It's tedious and error-prone to specify Queue name as an `Option` when enqueuing a task. If a certain type of task needs to go into a specific queue, it's better to specify that once and not every time enqueueing that type of task. This also applies to other options as well. It's more convenient and common to say for this type of task, set timeout to be this duration, or for this type of task, set unique TTL to be this duration. **Describe the solution you'd like** Add a method to `Client` to set default options for a type. ```go func (c *Client) SetDefaultOptions(pattern string, opts ...Option) ``` Example: ```go c := asynq.NewClient(r) // Routes maps task types to queue names. c.SetDefaultOptions("web:email", asynq.Queues("web")) c.SetDefaultOptions("feed:import", asynq.Queues("feed"), asynq.Timeout(time.Hour)) feedTask := asynq.NewTask("feed:import", payload) c.Enqueue(feedTask) // Should be routed to "feed" queue. webTask := asynq.NewTask("web:email", payload) c.Enqueue(webTask) // Should be routed to "web" queue. otherTask := asynq.NewTask("other:task", payload) c.Enqueue(otherTask) // Should be routed to "default" queue. No matching routing so use "default" queue. // You should be able to override routing at enqueue time. feedTask := asynq.NewTask("feed:tasks", payload) c.Enqueue(feedTask, asynq.Queue("other_queue")) ``` **Describe alternatives you've considered** None for now. **Additional context** Inspiration: [Celery](http://docs.celeryproject.org/en/latest/userguide/routing.html#guide-routing) has a task routing configuration option.
kerem 2026-03-02 05:18:09 +03:00
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#40
No description provided.