Miksus / rocketry

Modern scheduling library for Python
https://rocketry.readthedocs.io
MIT License
3.26k stars 105 forks source link

It has an app.run() but no app.stop() #54

Closed emantos closed 2 years ago

emantos commented 2 years ago

Is your feature request related to a problem? Please describe.

Sometimes, you would want the scheduler to stop running once a certain condition occurs. For instance, I want to check periodically a certain status returned by some REST API. If that status has met my condition, I would want to stop the scheduler.

Describe the solution you'd like

An app.stop() which is the logical opposite of app.run()

Describe alternatives you've considered

I could just exit() the code once the condition is met but that won't work for all cases.

Miksus commented 2 years ago

This is actually already supported via two ways:

  1. The shutdown method in session:
    
    from rocktetry.args import Session

@app.task(execution="thread") def stop_scheduler(session=Session()): session.shutdown()

The session is also found from the app instance:
```python
@app.task(execution="thread")
def stop_scheduler():
    app.session.shutdown()
  1. Using the shut_cond:
from rocketry.conds import after_finish
app = Rocketry(config={
    "shut_cond": after_finish("a_task")
})

The scheduler is shut down when this condition is true. This is probably not that handy to use yet as the scheduler related conditions are not imported to the more convenient condition API but it's fully functioning. The unit tests rely on this mechanism.

It's also advised to use any of these methods instead of using exit as there are some handling on the processes and tasks if the scheduler is correctly shut down. By default the tasks are waited (and terminated if termination conditions are met) but can be tuned to try aggressively shut down all tasks.

Does this solve your issue or do you see it this should be supported directly on the app level?

Miksus commented 2 years ago

These should be documented at least (not yet have had time).

emantos commented 2 years ago

@Miksus It should be enough. Sorry, I didn't see that shutdown method.