i want to run some code when the session is finish...
# ask-ai
m
i want to run some code when the session is finished or when the remote participants left. when using
add_shutdown_callback
is VERY slow and we can't rely on that to end recordings. what can i do?
t
Thanks for reaching out, Droxy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When you want to run some code as a session ends or when remote participants leave, here are a few handy ways to do it: • For cleanup after shutdown: You can use
add_shutdown_callback
. It’s great for tasks that happen after everything’s wrapped up, though keep in mind there might be a slight delay because of a default 60-second timeout. If you want, you can tweak this timeout using
shutdown_process_timeout
in
WorkerOptions
. • For a quicker, real-time response: Try the
participant_disconnected
event on the
Room
object. This lets you run code right when someone leaves. For example, you could check if the last participant has left and then kick off your cleanup logic. • To end the whole session: Use the
delete_room
API to close the room and disconnect everyone. If you’d like some detailed examples or a hand setting this up, just give me a shout—I’m here to help! Sources: Inside a session | LiveKit Docs | Job lifecycle | LiveKit Docs | Session recording and transcripts | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
m
what is the timeout?
t
Just so you know, the default timeout for
add_shutdown_callback
in LiveKit is set to 60 seconds. This means the shutdown process will wait up to 60 seconds for the callback to finish before it forcefully stops. If you'd like, you can change this timeout by tweaking the
shutdown_process_timeout
setting in
WorkerOptions
. And hey, if you want a hand with setting this up, just give me a shout!