mirror of
https://github.com/hibiken/asynq.git
synced 2026-04-25 23:15:51 +03:00
[GH-ISSUE #313] [FEATURE REQUEST] Change process task context behaviour #1147
Labels
No labels
CLI
bug
designing
documentation
duplicate
enhancement
good first issue
good first issue
help wanted
idea
invalid
investigate
needs-more-info
performance
pr-welcome
pull-request
question
wontfix
work in progress
work in progress
work-around-available
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/asynq#1147
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @crossworth on GitHub (Aug 19, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/313
Originally assigned to: @hibiken on GitHub.
Is your feature request related to a problem? Please describe.
Maybe. It could help solve #285 and #265 and make the library more idiomatic.
Describe the solution you'd like
I would like to propose that we change the ProcessTask signature from:
To:
And that we add the
context.Contextto the task struct to make it more open to middleware patterns.Describe alternatives you've considered
None, but as stated in https://github.com/hibiken/asynq/issues/285#issuecomment-870732712 we could use a database to hold state between the middleware and task, this would solve some of the problems that the missing context causes.
Additional context
The current implemenation allows many of the middleware patterns, like allowing a middleware setting a value on context, metrics, tracing, but for more complex middleware scenarios is not perfect. Like in the case where we want to use the context to pass information back to middleware. Placing the context of the task we make more clear that the context is only valid for that task as well, and ofcourse this make the task more like the
http.Requestand all the patterns that are common on http middleware/handlers today.It's important to note that it may cause confusion on task registration, the consumer may think that it can store context value and it will be serialized and stored on the storage engine, for this reason I think we should limit the context API, allowing only the context retrieve and
WithContextthat creates a copy of a task (to be used inside middleware or task handlers).I have implemented this propose on https://github.com/crossworth/asynq/tree/feature/change-process-signature and have create a repository with examples using this propose https://github.com/crossworth/asynq-task-ctx-examples
The examples includes:
What do you think @hibiken? If you think this is a good improvement let me know that I will create the PR and test with asynqmon/cli (currently tests are passing).
@hibiken commented on GitHub (Aug 21, 2021):
@crossworth Thank you for the proposal!
I think there's a huge benefit in making the API similar to
net/httppackage, since there are lot of other packages built around thenet/httpand makes this package integrate with those packages easier.Since this is a breaking change, I'd like to be careful when making the decision. Let's list out pros and cons of the current and suggested API and we can go from there!
@crossworth commented on GitHub (Aug 22, 2021):
I will work on enumerating the pros and cons.
Just a note, we could solve this problem by changing the function signature from
func(ctx context.Context, t *asynq.Task) errortofunc(ctx context.Context, t *asynq.Task) (context.Context, error)as well, but I think we should try mimic thenet/httpway of handling requests and since we are on the v0 its easy to do it now than later.@crossworth commented on GitHub (Aug 28, 2021):
Current API
PROS:
CONS:
Suggested API
PROS:
CONS:
The context may be used on the workflow feature (#244) (like sharing the same context between tasks) as well.
I know that the pros and cons are not that strong, but allowing the task to change the context and the middleware consume it is a big benefit.
@hibiken commented on GitHub (Sep 7, 2021):
@crossworth thank you for putting this together!
I'm currently designing the implementation of workflow and will take this proposal into consideration 👍