nice-fish-21757
08/04/2025, 5:49 AMtall-airline-16756
08/04/2025, 5:49 AMtall-airline-16756
08/04/2025, 5:50 AMshutdown() with a reason:
ctx.shutdown('Custom shutdown reason');
1. Add a shutdown callback to receive the reason:
ctx.addShutdownCallback(async (reason) => {
console.log('Shutdown reason:', reason);
// Perform cleanup tasks here
});
The shutdown callbacks are executed in the order they were added, and they must return a Promise<void> to ensure proper cleanup. For example:
async function main(ctx: JobContext) {
// Add shutdown callback
ctx.addShutdownCallback(async (reason) => {
console.log('Cleaning up with reason:', reason);
await cleanupOperations();
});
// Later, shutdown with a reason
ctx.shutdown('Normal shutdown');
}
All callbacks are awaited sequentially, ensuring proper execution before the job terminates.nice-fish-21757
08/04/2025, 5:50 AMtall-airline-16756
08/04/2025, 5:51 AMreason parameter in your shutdown callback function for LiveKit agents using Python, here’s a simple way to do it:
1. First, make sure your shutdown callback function is set up to accept the reason parameter:
async def my_shutdown_callback(reason: str):
print(f"Shutdown reason: {reason}")
1. Next, register your callback with JobContext.add_shutdown_callback like this:
ctx.add_shutdown_callback(my_shutdown_callback)
1. Finally, when you want to shut down, call shutdown with a reason:
ctx.shutdown("Session ended")
And that’s it! When the job shuts down, your callback gets triggered and the reason string is passed along. This way, you can easily handle whatever shutdown reason you need.