What conditions initiate a job to shutdown?
# ask-ai
m
What conditions initiate a job to shutdown?
t
Thanks for reaching out, Kush. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
In LiveKit, a job can be initiated to shutdown under the following conditions: • When the
shutdown
method is explicitly called on the JobContext. This method gracefully shuts down the job and runs all shutdown promises. Optionally, a reason for the shutdown can be provided. • When LiveKit receives termination signals such as
SIGTERM
,
SIGINT
, or
SIGQUIT
. If participants are connected, LiveKit enters a "draining mode" and shuts down once all participants have disconnected. Sources: JobContext | LiveKit Agents | Distributed Setup | LiveKit Docs
m
How can a shutdown be initiated from a backend or frontend React/NodeJS? If it's possible?
t
Yes, a shutdown can be initiated from both the backend and frontend in LiveKit. Here are the methods: • From the Backend: You can use the
shutdown
method on the
JobContext
object to gracefully shut down the job. For example:
Copy code
// Using the JobContext.shutdown method
ctx.shutdown(reason="Session ended")
This method disconnects the agent from the room while allowing other participants to continue. Additionally, you can add shutdown callbacks for cleanup tasks using
addShutdownCallback
. • To End the Entire Session: Use the server API's
deleteRoom
method to disconnect all participants and remove the room:
Copy code
// Using the LiveKit API to delete the room
const api_client = new api.LiveKitAPI(
  process.env.LIVEKIT_URL,
  process.env.LIVEKIT_API_KEY,
  process.env.LIVEKIT_API_SECRET
);

await api_client.room.deleteRoom({
  room: ctx.job.room.name
});
Let me know if you need further assistance with implementing these methods! Sources: JobContext | LiveKit Agents | Inside a session | LiveKit Docs
m
I don't understand the "From the backend", because the backend doesn't have the JobContext, that is in the agent. The delete room method makes sense. For clarity, by deleting the room, the shutdown of the job is initiated?
t
You are correct that the backend does not have direct access to the
JobContext
, as it is specific to the agent. I appreciate your clarification. Regarding the
deleteRoom
method, deleting a room causes all participants to be disconnected and removes the room from the server. However, it does not explicitly initiate the shutdown of the job for agents. If you need to ensure that the agent's shutdown hooks are triggered, you would need to explicitly call the
shutdown
method on the
JobContext
within the agent itself. Let me know if you need further clarification or assistance! Sources: Managing rooms | LiveKit Docs | Inside a session | LiveKit Docs