straight-toddler-27716
05/09/2025, 10:33 AMtall-airline-16756
05/09/2025, 10:33 AMtall-airline-16756
05/09/2025, 10:33 AMasync def entrypoint(ctx: JobContext):
async def my_shutdown_hook():
# save user state to database
# your database connection and saving logic here
...
ctx.add_shutdown_callback(my_shutdown_hook)
# rest of your agent logic
...
# when ready to disconnect
ctx.shutdown(reason="Session ended")
The shutdown hook will run after the shutdown function is called, ensuring your database operations are completed before the agent fully disconnects. Please note that the shutdown hook should complete within a reasonable time frame, typically 60 seconds by default, which can be adjusted using the shutdown_process_timeout parameter in WorkerOptions.
Sources: Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs