From 37ce32a1dce0cd8e982f8572b86cd29734273648 Mon Sep 17 00:00:00 2001 From: Eduardo Firvida Date: Fri, 20 Jun 2025 22:19:51 -0400 Subject: [PATCH] Update docs, and ad dnew tutorial --- docs/tutorials/hello_world.md | 2 +- docs/tutorials/hello_world_2.md | 64 +++++++++++++++++++++++++++++++++ mkdocs.yml | 4 +-- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 docs/tutorials/hello_world_2.md diff --git a/docs/tutorials/hello_world.md b/docs/tutorials/hello_world.md index b746285..94cffe7 100644 --- a/docs/tutorials/hello_world.md +++ b/docs/tutorials/hello_world.md @@ -11,7 +11,7 @@ my_addons/ └── hello_world/ ├── __init__.py ├── manifest.yaml - ├── routers.py + └── routers.py ``` ### 2️⃣ Define the `manifest.yaml` diff --git a/docs/tutorials/hello_world_2.md b/docs/tutorials/hello_world_2.md new file mode 100644 index 0000000..2226c6e --- /dev/null +++ b/docs/tutorials/hello_world_2.md @@ -0,0 +1,64 @@ +## 🚀 Extending Your Addon: Lifecycle Hooks with AddonSetupHook + +Let’s continue working on our `hello_world` addon to demonstrate how to define custom behaviors during the addon lifecycle. When **pAPI** discovers an addon, you can control its **startup** and **shutdown** phases. This is especially useful when you need to perform certain actions before the system starts, or just before it shuts down. + +Each addon can, optionally, register one or more `AddonSetupHook` classes to manage its lifecycle events. In this tutorial, we’ll implement a basic `AddonSetupHook` to simulate tasks executed during addon startup and shutdown. + +--- + +### 1️⃣ Add the Setup File to Your Addon + +Inside your `hello_world` addon directory, create a new Python file named `addon_setup.py` (or any other name you prefer). Your addon structure should look like this: + +``` +my_addons/ +└── hello_world/ + ├── __init__.py + ├── manifest.yaml + ├── addon_setup.py + └── routers.py +``` + +--- + +### 2️⃣ Define the `AddonSetupHook` + +Now, import `AddonSetupHook` and subclass it. You’ll need to implement the asynchronous `startup` and `shutdown` methods. Here’s a basic example that simulates some heavy async tasks using `sleep`: + +```python +from asyncio import sleep +from logging import getLogger + +from papi.core.addons import AddonSetupHook + +logger = getLogger(__name__) + + +class HelloWorldAddonSetup(AddonSetupHook): + async def startup(self): + logger.info("Initializing 'Hello World' addon...") + await sleep(10) # Simulate a heavy async task at startup + logger.info("'Hello World' addon setup completed.") + + async def shutdown(self): + logger.info("Shutting down 'Hello World' addon...") + await sleep(5) # Simulate a heavy async task at shutdown + logger.info("'Hello World' addon shutdown completed.") +``` + +--- + +### 3️⃣ Register the Hook in `__init__.py` + +Finally, import your `HelloWorldAddonSetup` class in the addon's `__init__.py` so that pAPI can discover and register it automatically: + +```python +from .router import router as hello_router +from .addon_setup import HelloWorldAddonSetup +``` + +--- + +That’s all you need! + +When the addon is loaded, the `startup` method will be executed automatically. When the application is stopped (e.g. by pressing `Ctrl+C` while running with `uvicorn`), the `shutdown` method will be called. diff --git a/mkdocs.yml b/mkdocs.yml index f30be29..2185a99 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -8,6 +8,7 @@ nav: - Home: index.md - Getting started: - Your first Addon: tutorials/hello_world.md + - Addon Life cycle: tutorials/hello_world_2.md - The weather example: tutorials/weather.md - API Reference: - pAPI Core: @@ -15,9 +16,6 @@ nav: - Settings: reference/settings.md - DB: reference/db.md - Models: reference/models.md - - build in addons: - - Image Storage: build-in-addons/image_storage.md - - User Authentication: build-in-addons/user_auth_system.md theme: name: material