what does shutdown method for JobContext do?
# ask-ai
w
what does shutdown method for JobContext do?
t
Thanks for reaching out, Arthur. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
shutdown
method in
JobContext
is there to help you wrap up a job smoothly and run any shutdown callbacks you've set up. Here’s what it does for you: • Graceful Shutdown: Makes sure the job finishes cleanly, taking care of all the necessary cleanup. • Execution of Shutdown Callbacks: Runs all the callbacks you’ve added with
add_shutdown_callback()
, like freeing resources or saving your progress. • Disconnect from Room: Safely disconnects the agent from the room so others can keep going without a hitch. • Optional Reason Parameter: Lets you pass in a reason for the shutdown, which is super helpful for logging or debugging. Here’s a quick example to show you how it works:
Copy code
async def entrypoint(ctx: JobContext):
    try:
        # Do some work...
        await ctx.connect()
        # ...
    finally:
        ctx.shutdown(reason="Session completed")
This way, even if something goes wrong, the shutdown callbacks still run, and the agent disconnects nicely.
w
Would I use delete room and shutdown together, or would they be used seperately? Whenever trying to use it in the context for example, having my AI agent end the call.
t
Hey there! Just a quick heads-up about the methods
delete_room
and `shutdown`—they each have their own role depending on what you’re aiming to do: • `delete_room`: This one wraps up the call for everyone right away. It’s perfect if you want to completely end the session for all participants. For example:
await ctx.api.room.delete_room(api.DeleteRoomRequest(room=ctx.room.name))
. • `shutdown`: This method only disconnects the AI agent from the room, letting everyone else keep chatting. For instance:
ctx.shutdown(reason="Session ended")
. Here’s a little tip: • If you want to end the call for everyone, go with
delete_room
. • If you just want the AI agent to leave but keep the call going for others,
shutdown
is your friend. Usually, you’ll use one or the other depending on whether you want to close out the whole session or just have the agent step away. Hope that clears things up! Sources: Agents telephony integration | LiveKit Docs | Inside a session | LiveKit Docs | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs
w
is shutdown a coroutine, and if so is await used?
t
Just a quick heads-up: the
shutdown
method in
JobContext
isn’t a coroutine, so you don’t need to use
await
with it. It’s a regular method you can call synchronously. Here’s a quick example:
Copy code
ctx.shutdown("Optional shutdown reason")
Even though this method will trigger any shutdown callbacks you’ve registered (and some of those might be asynchronous), the
shutdown
method itself doesn’t require an
await
. Hope that clears things up! Sources: livekit-agents/livekit/agents/worker.py | agents/src/ipc/job_proc_lazy_main.ts