Giter Club home page Giter Club logo

Comments (17)

ncmonger avatar ncmonger commented on July 19, 2024 13
scheduler = APScheduler()

def your_task():
    app = apscheduler.app
    with app.app_context():
        users = User.query.all()

This only works if the scheduler is defined within the same module as the task. For any sizeable real-life application, the job most likely life in another file, so we have to make a global variable that can be imported from the separate module that contains the job.

Related to that, why isn't the scheduler object available from within the task by default?

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024 7

What do you mean by within a blueprint?
A task is not related to a blueprint, you need to have access to the original Flask app instance to create a new app context.

You can access it using apscheduler.app.

e.g:

scheduler = APScheduler()

def your_task():
    app = apscheduler.app
    with app.app_context():
        users = User.query.all()

from flask-apscheduler.

trinanda avatar trinanda commented on July 19, 2024 2
scheduler = APScheduler()

def your_task():
    app = apscheduler.app
    with app.app_context():
        users = User.query.all()

This only works if the scheduler is defined within the same module as the task. For any sizeable real-life application, the job most likely life in another file, so we have to make a global variable that can be imported from the separate module that contains the job.

Related to that, why isn't the scheduler object available from within the task by default?

This solved my issue, thanks Man :)

from flask-apscheduler.

ncmonger avatar ncmonger commented on July 19, 2024 1

Yes, a task is a normal function, but it is invoked within your library. We could specify that if the programmer want the scheduler object in the task function, just define a named parameter that will be assigned the scheduler object at runtime.

This will allow all task to access application context stuff without having to do a global import. Does this make sense?

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

Hi,

I guess you are using Flask-SQLAlchemy and you want to access your model within a task.
That is a small example how to create a context within a task.

e.g:

def your_task():
    with app.app_context():
        users = User.query.all()
        ...

I will soon add an example in the examples folder.

from flask-apscheduler.

dhhagan avatar dhhagan commented on July 19, 2024

Yea! I am using flask-sqlalchemy. I tried more or less what you posted above, but haven't had any luck. I'm trying to use within a blueprint, which may also be causing issues.

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

I just added a new example:
https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/flask_context.py

Let me know if it doesn't help you.

from flask-apscheduler.

dhhagan avatar dhhagan commented on July 19, 2024

Awesome. I think I finally just got it to work. Thanks for the help.

from flask-apscheduler.

dhhagan avatar dhhagan commented on July 19, 2024

Actually, I've been having one more issue. It seems that although everything runs great in development, in production I get an error from apscheduler that says it can't import the module.

It throws: LookupError: Error resolving reference app.utilities:backup_data_to_s3: could not import module.

Have you seen anything like this? For context, I'm using gunicorn/eventlet behind an NGINX proxy.

screen shot 2017-02-07 at 10 39 31 am

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

Hard to tell, usually it happens when either the task path is incorrect or the module has something wrong.

The folder /var/www/tata/tata has a file init.py? If so, I guess your task path should be tata.app.utilities:daily_backup_data_to_s3

from flask-apscheduler.

dhhagan avatar dhhagan commented on July 19, 2024

@viniciuschiele Nope. No __init__.py file in that directory. There is in the app directory. The file structure is:

\app models.py utilities.py __init__.py \blueprint_1 config.py manage.py

I think that's all of the relevant files...It could be that something in the module is wrong, though it works/executes fine locally..so I'm a bit confused.

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

One thing you could do is to get the full path of the task using an utility from APScheduler.

from apscheduler.util import obj_to_ref
from app.utilities import daily_backup_data_to_s3

full_path = obj_to_ref(daily_backup_data_to_s3)

Run the above code in production, it will return the path of your task function.

Just for knowledge, my library uses the inverse method to get the function based on the path.

from apscheduler.util import ref_to_obj

task_func = ref_to_obj('app.utilities:daily_backup_data_to_s3')

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

Hi @dhhagan

Any luck on it?

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

yeah, that is what I recommend, just define your APScheduler object in a global module like init.py and in the task files you can import this object.

I think I didn't get your question, a task is just a normal function, how would it be able to access the sheduler object "by default" ?
Maybe you meant something like the current_app from Flask?

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 19, 2024

That's not simple to build because it is not my library that calls the tasks, my library just add endpoints to the Flask app to manage the tasks from APScheduler which is another library.

from flask-apscheduler.

ncmonger avatar ncmonger commented on July 19, 2024

I see. I remember looking into the function that invokes the function but now I remember that is jobs.py in the APScheduler not Flask-APScheduler. You are correct, it won't be easy at this level.

from flask-apscheduler.

andrequeiroz2 avatar andrequeiroz2 commented on July 19, 2024
scheduler = APScheduler()

def your_task():
    app = apscheduler.app
    with app.app_context():
        users = User.query.all()

Isso só funciona se o planejador for definido no mesmo módulo da tarefa. Para qualquer aplicativo considerável da vida real, o trabalho provavelmente está em outro arquivo, então temos que fazer uma variável global que pode ser importada do módulo separado que contém o trabalho.

Relacionado a isso, por que o objeto planejador não está disponível na tarefa por padrão?

obrigado apos dias tentando tudo, isso resolveu meu problema.

from flask-apscheduler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.