-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from Miksus/docs/fix
DOCS: Update documentation
- Loading branch information
Showing
16 changed files
with
531 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
Bigger Applications | ||
=================== | ||
|
||
When your project grows in size you might want to | ||
put the application to multiple files. | ||
|
||
.. code-block:: | ||
. | ||
├── app | ||
│ ├── __init__.py | ||
│ ├── main.py | ||
│ ├── conditions.py | ||
│ ├── arguments.py | ||
│ └── tasks | ||
│ ├── __init__.py | ||
│ ├── morning.py | ||
│ └── evening.py | ||
.. note:: | ||
|
||
We use ``__init__.py`` files to make | ||
importing easier from the tasks. This | ||
structure enables us importing the conditions | ||
by ``from app.conditions import my_cond``. | ||
|
||
Files | ||
----- | ||
|
||
Here is a quick explanation of the files and what | ||
you could put in each: | ||
|
||
.. glossary:: | ||
|
||
``__init__.py`` | ||
|
||
Marks the directory as a package. You can leave | ||
these empty. | ||
|
||
``app/main.py`` | ||
|
||
Entry point to the application. This | ||
|
||
It could look like: | ||
|
||
.. code-block:: | ||
from rocketry import Rocketry | ||
from app.tasks import morning, evening | ||
app = Rocketry() | ||
# Set the task groups | ||
app.include_group(morning.group) | ||
app.include_group(evening.group) | ||
if __name__ == "__main__": | ||
app.run() | ||
``app/tasks/...`` | ||
|
||
Put your tasks here. Use also groups and | ||
put the groups in the app in ``app/main.py`` | ||
to avoid problems in importing. | ||
|
||
For example, ``app/tasks/evening.py`` could look like this: | ||
|
||
.. code-block:: | ||
from rocketry import Grouper | ||
from rocketry.args import FuncArg | ||
from app.conditions import my_cond | ||
from app.parameters import get_value | ||
group = Grouper() | ||
@group.task(my_cond) | ||
def do_things(arg=FuncArg(get_value)): | ||
... | ||
``app/conditions.py`` | ||
|
||
Put custom conditions here. | ||
|
||
For example: | ||
|
||
.. code-block:: | ||
from rocketry import Grouper | ||
group = Grouper() | ||
@group.cond() | ||
def my_cond(): | ||
return True or False | ||
We created a group to conveniently declare | ||
the function to be a condition. We don't | ||
need to include this group to the app. | ||
|
||
``app/arguments.py`` | ||
|
||
Put custom parameters here. For example: | ||
|
||
.. code-block:: | ||
def get_value(): | ||
return "Hello" | ||
You can also nest these and pass an argument as | ||
to another argument with ``FuncArg`` similarly | ||
we set in the task. | ||
|
||
Running | ||
------- | ||
|
||
Then you can run this as a Python module: | ||
|
||
.. code-block:: | ||
python -m app.main | ||
.. note:: | ||
|
||
You can also turn this to a package using ``setup.py`` | ||
or add CLI by creating ``__main__.py`` file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
Integrate FastAPI | ||
================= | ||
|
||
The simplest way to combine FastAPI app with Rocketry app | ||
is to start both as async tasks. You can modify the Rocketry's | ||
runtime session in FastAPI. There is an existing example project: | ||
`Rocketry with FastAPI (and React) <https://github.com/Miksus/rocketry-with-fastapi>`_ | ||
|
||
First, we create a simple Rocketry app | ||
(let's call this ``scheduler.py``): | ||
|
||
.. code-block:: python | ||
# Create Rocketry app | ||
from rocketry import Rocketry | ||
app = Rocketry(config={"task_execution": "async"}) | ||
# Create some tasks | ||
@app.task('every 5 seconds') | ||
async def do_things(): | ||
... | ||
if __name__ == "__main__": | ||
app.run() | ||
Then we create a FastAPI app and manipulate the Rocketry | ||
app in it (let's call this ``api.py``): | ||
|
||
.. code-block:: python | ||
# Create FastAPI app | ||
from fastapi import FastAPI | ||
app = FastAPI() | ||
# Import the Rocketry app | ||
from scheduler import app as app_rocketry | ||
session = app_rocketry.session | ||
@app.get("/my-route") | ||
async def get_tasks(): | ||
return session.tasks | ||
@app.post("/my-route") | ||
async def manipulate_session(): | ||
for task in session.tasks: | ||
... | ||
if __name__ == "__main__": | ||
app.run() | ||
Then we combine these in one module | ||
(let's call this ``main.py``): | ||
|
||
.. code-block:: python | ||
import asyncio | ||
import uvicorn | ||
from api import app as app_fastapi | ||
from scheduler import app as app_rocketry | ||
class Server(uvicorn.Server): | ||
"""Customized uvicorn.Server | ||
Uvicorn server overrides signals and we need to include | ||
Rocketry to the signals.""" | ||
def handle_exit(self, sig: int, frame) -> None: | ||
app_rocketry.session.shut_down() | ||
return super().handle_exit(sig, frame) | ||
async def main(): | ||
"Run scheduler and the API" | ||
server = Server(config=uvicorn.Config(app_fastapi, workers=1, loop="asyncio")) | ||
api = asyncio.create_task(server.serve()) | ||
sched = asyncio.create_task(app_rocketry.serve()) | ||
await asyncio.wait([sched, api]) | ||
if __name__ == "__main__": | ||
asyncio.run(main()) | ||
Note that we need to subclass the ``uvicorn.Server`` in order | ||
to make sure the scheduler is also closed when the FastAPI app | ||
closes. Otherwise the system might not respond on keyboard interrupt. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Cookbook | ||
======== | ||
|
||
.. toctree:: | ||
:maxdepth: 3 | ||
:caption: Contents: | ||
|
||
bigger_applications | ||
fastapi |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
Condition Classes | ||
================= | ||
|
||
Condition classes are the low level mechanics of the condition | ||
system. All conditions are instances of the condition classes. | ||
|
||
There are also time period utilities that are used in the | ||
time related conditions. They determine whether a given time | ||
is inside a given time period, such as a given day of month, | ||
day of week, between |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ operates. | |
:maxdepth: 3 | ||
:caption: Contents: | ||
|
||
structure | ||
task/index | ||
conditions/index | ||
config | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.