First of all, questions about flask_context including context for RQ jobs seems to be common issue but I searched a lot and still couldn't solve my problem.
My decorator functions (tried both of them in different variations):
def redis_decorator1(f):
def inner_wrapper(*args, **kwargs):
# db interactions
return res
return inner_wrapper
def redis_decorator2(app):
# app.app_context().push()
def wrapper(f):
def inner_wrapper(*args, **kwargs):
# db interactions
return res
return inner_wrapper
return wrapper
...
@redis_decorator
def under_decorator_func(*some_args)
In function under_decorator_func
is used flask.current_app
.
Problems:
First decorator - RuntimeError: "Working outside of application
context" when redis task starts.
Second decorator - Same error on app start
I also tried app.app_context().push()
right after app is created so I have no idea what is going on here.
Turns out solution was quite obvious and required a bit more knowledge about python contexts:
app = create_app() # or anything that creates your Flask application
...
def redis_decorator2(app):
def wrapper(f):
def inner_wrapper(*args, **kwargs):
with app.app_context():
# db interactions
return res
return inner_wrapper
return wrapper
Important thing here to use flask context in inner_wrapper
function and not any layer above, otherwise you will get same error as before.
Hope it will help somebody.