mirror of
https://github.com/hibiken/asynq.git
synced 2026-04-26 07:25:56 +03:00
[GH-ISSUE #285] [FEATURE REQUEST] Returning result from handler along with error #2138
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#2138
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 @sujit-baniya on GitHub (Jun 28, 2021).
Original GitHub issue: https://github.com/hibiken/asynq/issues/285
Originally assigned to: @hibiken on GitHub.
Is your feature request related to a problem? Please describe.
No
Describe the solution you'd like
Currently the signature of a handler is
func(ctx context.Context, t *asynq.Task) error. When using middleware, I would like to get result of the function and handle the code further.I suppose following signature would work
@crossworth commented on GitHub (Jun 28, 2021):
Hi, do you want to handle the code further only on error or even on success?
If it's only on error, you could probably create a custom error with the data you need, return it and type assert on your middleware.
If you want even with success, I think a better approach would be using
context.WithValue, maybe changingTaskto have the context on it.With this approach we could easily do:
and retrieve the value in the middleware.
I think this way is more idiomatic and similar of how the http requests works.
@sujit-baniya commented on GitHub (Jun 28, 2021):
@crossworth I would want to handle in both cases and apply condition on middleware accordingly
@hibiken commented on GitHub (Jun 29, 2021):
@sujit-baniya Thank you for opening a feature request!
And @crossworth thank you so much for helping answering questions for various feature requests/bugs 🙏
As @crossworth mentioned, you should use a custom error type if you want to distinguish different error scenarios.
For successful case, both of the suggestion are valid but requires API changes.
I'm going to be working on adding a support for workflow and I think we'll implement some mechanism to store results for that feature.
In the meantime, I think you can use task ID and store the result in DB or Redis.
Example:
and in the middleware, you can read the persisted result.
@hibiken commented on GitHub (Nov 7, 2021):
This can now be done through
ResultWritersince version 0.19!