I have added a function call to send some data to ...
# ask-ai
s
I have added a function call to send some data to the database. For now, I agent send data before ending the call. If an agent gets disconnected, is there any way to send data? Is there any way, even after the interruption, the agent first sends data, then only gets disconnected?
t
Thanks for reaching out, Rishabh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To ensure data is sent to the database before an agent disconnects, even after an interruption, you can use shutdown hooks. These hooks are executed after the session ends, allowing you to perform tasks like saving data to the database. Here is an example implementation:
Copy code
async 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