Skip to content

Conversation

@Kolanar
Copy link
Contributor

@Kolanar Kolanar commented Aug 14, 2025

Description

docs: add lifecycle hooks schema with order and resource availability

Added a visual schema/table to the Lifespan Hooks documentation showing:

  • All existing hooks (on_startup, after_startup, on_shutdown, after_shutdown)
  • Their execution order
  • Available resources (CLI args, context, broker) for each hook

This makes it easier to understand when hooks are called and what can be accessed at each stage.

Fixes #2285

Type of change

Please delete options that are not relevant.

  • Documentation (typos, code examples, or any documentation updates)

Checklist

  • My code adheres to the style guidelines of this project (just lint shows no errors)
  • I have conducted a self-review of my own code
  • I have made the necessary changes to the documentation
  • My changes do not generate any new warnings
  • I have added tests to validate the effectiveness of my fix or the functionality of my new feature
  • Both new and existing unit tests pass successfully on my local environment by running just test-coverage
  • I have ensured that static analysis tests are passing by running just static-analysis
  • I have included code examples to illustrate the modifications

Kolanar added 6 commits August 8, 2025 23:08
… command

- Added instruction to upgrade pip to fix `pip install --group` error
- Fixed mkdocs serve errors by ensuring dependencies and environment setup
- Verified changes do not affect other parts of the system
- Ready to discuss any proposed changes and feedback
… command

- Added instruction to upgrade pip to fix `pip install --group` error
- Fixed mkdocs serve errors by ensuring dependencies and environment setup
- Verified changes do not affect other parts of the system
- Ready to discuss any proposed changes and feedback
Changed the documentation server startup instructions to use the
`just docs-serve` command instead of directly running `mkdocs serve`.
…mmands

- Changed documentation to use `just docs-serve` for starting the local docs server
- Added note about using `just docs-serve --full` for a full build with all dependencies
…mmands

- Changed documentation to use `just docs-serve` for starting the local docs server
- Added note about using `just docs-serve --full` for a full build with all dependencies
Added a visual schema/table to the Lifespan Hooks documentation showing:
- All existing hooks (`on_startup`, `after_startup`, `on_shutdown`, `after_shutdown`)
- Their execution order
- Available resources (CLI args, context, broker) for each hook

This makes it easier to understand when hooks are called and what can be accessed at each stage.
@Kolanar Kolanar requested a review from kumaranvpl as a code owner August 14, 2025 19:10
@CLAassistant
Copy link

CLAassistant commented Aug 14, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Aug 14, 2025
@Kolanar Kolanar closed this Aug 14, 2025
@Kolanar Kolanar reopened this Aug 14, 2025
@Kolanar Kolanar closed this Aug 14, 2025
@Kolanar Kolanar reopened this Aug 14, 2025
Comment on lines 32 to 37
| Hook | CLI args | Context | Broker |
| ------------------- | -------- | ------ | ----- |
| **on\_startup** ||||
| **after\_startup** ||||
| **on\_shutdown** ||||
| **after\_shutdown** ||||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the Broker column if we are talking about whether the broker is running or not, then please add a mention of "Broker is running" or "Broker life" to make it clear.

If we are talking about whether we can get an instance of Broker, then we can in any hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maclovi @Kolanar I am afraid, we can't get access to Broker from Context in any hook now. @Kolanar can you please check it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lancetnik In this example, I would like to indicate whether there is a connection to the broker. From the context, there is a possibility to obtain broker data, but I tried to indicate the broker connection. Perhaps I should change the line with the broker

Comment on lines 46 to 56
@app.after_startup
async def after_startup(logger: Logger):
logger.info("after_startup called")

@app.on_startup
async def first_start(logger: Logger):
logger.info("first_start called")

@app.on_startup
async def second_start(logger: Logger):
logger.info("second_start called")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add please full example:
@app.on_startup
@app.after_startup
@app.on_shutdown
@app.after_shutdown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add it now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kolanar does logger is accessible here in 0.6? I don't think so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lancetnik The documentation for version 0.6 provides an illustration of how to obtain a logger. In my example, this was done to demonstrate the sequence of operations. If this example confuses you, please inform me, and I will provide an alternative example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lancetnik I changed the example.

Added a complete example in the Lifespan Hooks documentation showing:
- All four hook types (`on_startup`, `after_startup`, `on_shutdown`, `after_shutdown`)
- Multiple functions per hook type
- Execution order with formatted console output

This improves clarity on how hooks are registered, executed, and in what order they appear in logs.
@Kolanar
Copy link
Contributor Author

Kolanar commented Aug 15, 2025

@Maclovi Please check the changes

@Kolanar Kolanar requested a review from Lancetnik August 27, 2025 16:23
@Lancetnik Lancetnik requested a review from theseriff August 27, 2025 18:46
logger.info("startup_first called")

@app.on_startup
async def startup_second():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove the duplicates? just add a note at the end that you can create many hooks


# --- Shutdown hooks ---
@app.on_shutdown
async def shutdown_first():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here


# --- After shutdown hooks ---
@app.after_shutdown
async def after_shutdown():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

shutdown_second called
after_shutdown called
```

Copy link
Collaborator

@theseriff theseriff Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write here that it is possible to create multiple hooks of each type without examples, just text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maclovi Yes, well, I'll change the wording and the example.

Copy link
Collaborator

@theseriff theseriff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need some changes

@Kolanar Kolanar requested a review from theseriff August 29, 2025 20:18
Copy link
Collaborator

@theseriff theseriff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a mention of the possibility of adding multiple hooks

@Lancetnik Lancetnik enabled auto-merge September 2, 2025 18:12
@Lancetnik Lancetnik added this pull request to the merge queue Sep 2, 2025
Merged via the queue into ag2ai:dev with commit 2b64134 Sep 2, 2025
27 checks passed
@Kolanar Kolanar deleted the feature/update-docs branch September 2, 2025 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: Add a schema, when hooks are called and in what order

4 participants