Lifecycle Hooks
FastPluggyBaseModule exposes two startup hooks. Both receive the FastPluggy instance via
automatic injection.
Call order
FastPluggy.__init__()
└─ load_app()
├─ discover_plugins()
├─ initialize_plugins_in_order()
├─ configure_templates()
├─ execute_all_module_hook("after_setup_templates") ← hook 1
└─ execute_all_module_hook("on_load_complete") ← hook 2
Both hooks support sync and async implementations.
Reloads while the app is serving
load_app() runs again on every settings save, plugin enable/disable and admin reload, while other
threads keep answering requests:
- One load at a time. A second
load_app()call waits for the running one. The lock is re-entrant: a hook or an event listener may callload_app()from the loading thread. fast_pluggy.module_manager/get_manager()never return a manager that is still being filled to another thread. During discovery and initialization, other threads keep the manager of the previous load; the new one replaces it once every module is discovered and initialized, beforefp.all_plugins_loadedand before both hooks run. Code running on the loading thread at that stage (afp.plugin.loadedlistener, for example) sees the manager being filled.- Each load builds a plugin's settings object once, and calls a
module_routergiven as a function once, beforefp.plugin.loadedis emitted. - A reload does not import the plugin's code again. Modules already imported stay as they are
in
sys.modules, so new plugin code (agit pullin the plugin folder, a package upgrade) takes a restart of the application.
after_setup_templates(self, fast_pluggy)
Called after the Jinja2 environment is built. Use it to:
- Add custom template filters or globals
- Create parent sidebar menu items (menu items registered with
@menu_entryneed their parent to exist first)
from fastpluggy.core.menu.schema import MenuItem
def after_setup_templates(self, fast_pluggy):
fast_pluggy.menu_manager.add_parent_item(
menu_type="main",
item=MenuItem(label="My Plugin", url="", icon="ti ti-star", parent_name="my_plugin", position=20),
)
# custom Jinja2 filter
fast_pluggy.templates.env.filters["my_filter"] = my_filter_fn
on_load_complete(self, fast_pluggy)
Called last, after every plugin has been initialized. Use it to:
- Register topbar actions
- Read other plugins' globals from
GlobalRegistry - Set up cross-plugin integrations
from fastpluggy.core.global_registry import GlobalRegistry
from fastpluggy.core.topbar import TopbarAction
def on_load_complete(self, fast_pluggy):
GlobalRegistry.extend_globals('topbar_actions', items=[
TopbarAction(id='my-topbar', icon='ti ti-star', tooltip='My Plugin',
position=20, module_name=self.module_name)
])
Async hooks
Both hooks can be async def. The framework detects coroutines and awaits them correctly: