bprinty / Flask-Execute

A Simpler Pattern for Using Celery with Flask
https://flask-execute.readthedocs.io/en/latest/
MIT License
13 stars 1 forks source link

Dynamically creating a scheduled task #24

Open zafaralam opened 3 years ago

zafaralam commented 3 years ago

Hi! This is a great plugin and I have been using it in my dev environment and it has been pretty easy to use.

I'm trying to create scheduled tasks based on user request, Is it possible to create a scheduled task dynamically? Below is an example of what I would like to do.

@app.route("/setup-task", methods=["POST"]
def setup_task():
    req_json = request.get_json()
    celery.schedule(
        hour=req_json['hour'],
        minute=req_json['minute'],
        method_name='myTask',
        name='scheduled-task-to-run-at-midnight'
    )
    return 'A Task has been scheduled'

Please let me know if you need me to provide further information. Appreciate your work and time.

Thanks

bprinty commented 3 years ago

Thanks! I'm glad to hear the plugin has been helpful.

Unfortunately, dynamic scheduling with celery isn't currently available. In the meantime, a potential workaround could be:

  1. Create "placeholder" tasks that run periodically at intervals useful to your application.
  2. Whenever a new task needs to be scheduled, save it to a database table (assuming your application uses a database).
  3. Whenever those "placeholder" tasks execute, they query your database for scheduled tasks to execute and run them via celery. If there aren't any scheduled tasks saved in the database, the "placeholder" task would just NOOP.

Either way, I think dynamic scheduling would be an awesome addition to the project. I'll add it to the queue of future work.

zafaralam commented 3 years ago

Thanks! I'm glad to hear the plugin has been helpful.

Unfortunately, dynamic scheduling with celery isn't currently available. In the meantime, a potential workaround could be:

  1. Create "placeholder" tasks that run periodically at intervals useful to your application.
  2. Whenever a new task needs to be scheduled, save it to a database table (assuming your application uses a database).
  3. Whenever those "placeholder" tasks execute, they query your database for scheduled tasks to execute and run them via celery. If there aren't any scheduled tasks saved in the database, the "placeholder" task would just NOOP.

Either way, I think dynamic scheduling would be an awesome addition to the project. I'll add it to the queue of future work.

Thanks for getting back and also adding it as a feature. I also thought of the workaround and implemented it for now in my app!

Looking forward to it. Would love to help out in anyway I can 😄