Best way to access fn_name from middleware after 6.3.2? #154
Replies: 4 comments 1 reply
-
|
Dependencies should work in middleware, is that not working for you? |
Beta Was this translation helpful? Give feedback.
-
|
Actually good catch, looks like I broke dependency injection in middleware... Oops! You can do something like: @worker.middleware
def print_context(task):
async def wrapper(*args, **kwargs):
if ctx := next(
(k for k in kwargs.values() if isinstance(k, TaskContext)), None
):
print(ctx)
return await task(*args, **kwargs)
return wrapperbut this requires that ctx be in the task's own definition. I may be able to come up with a half-way solution between the new approach and the context vars. |
Beta Was this translation helpful? Give feedback.
-
|
Though it should be noted importing the context var directly was never the supported pattern even before |
Beta Was this translation helpful? Give feedback.
-
|
Makes sense on the unsupported import. What we're really after is the middleware equivalent of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Bumped to 6.3.2 and got
ImportError: cannot import name 'task_context' from 'streaq.types'. I was using the ContextVars from #18 to get the function name inside an observability middleware.TaskDepends/WorkerDependslooks like the new way to do this, but as far as I can tell it only works for task functions —task.__name__in middleware is always"_fn"because of the internal closure. Is there a recommended way to getfn_namein a middleware now?Beta Was this translation helpful? Give feedback.
All reactions