viniciuschiele / flask-apscheduler

Adds APScheduler support to Flask
Apache License 2.0
1.12k stars 184 forks source link

RuntimeError: working outside of application context #16

Closed ddurieux closed 8 years ago

ddurieux commented 8 years ago

With flask, I use mongo database and my scheduler job use this mongo, but i have error

RuntimeError: working outside of application context

When I use call function, I use

with app.test_request_context():
    functionxxx()

But here not possible to works. Do you know how to fix this problem?

viniciuschiele commented 8 years ago

I think you should use with with app.app_context():

Is this code a unit test? If so, why do you need a Flask App?

Could you provide me a full example?

Thanks!

ddurieux commented 8 years ago

I have found a solution, so I think we can close this ;)

jcxmej commented 7 years ago

@ddurieux would have been helpful if you could have shared your solution before closing the issue.. thanks.

vijay-axis commented 7 years ago

@ddurieux please share the solution. Thank you

ddurieux commented 7 years ago

Yep, sorry for the delay,

I add jobs in config and load the config by eve: https://github.com/Alignak-monitoring-contrib/alignak-backend/blob/develop/alignak_backend/app.py#L1829-L1874

I have a file with the scheduler functions: https://github.com/Alignak-monitoring-contrib/alignak-backend/blob/develop/alignak_backend/scheduler.py

vijay-axis commented 7 years ago

@ddurieux Thank you

Guibod commented 7 years ago

This was not really helpful, let me explain it my way:

You should expose your APP as variable in one of your application module. For instance, in your app.py you don't expose a create_app() but a live app instance instead.

Then you do a local import of this application in your scheduled function:

def job1():
    from mylib.app import app
    app.logger.error('FUFU!')

At the moment i try to understand if it is not better to call with app.application_context(): Anyway this solution is simple.

caio-vinicius commented 2 years ago

I was using request.post, request.put instead of requests.post, requests.put.