Graceful Shutdown Process¶
Colyseus listens for SIGTERM
and SIGINT
signals to gracefully shut down the process.
The Graceful Shutdown behaviour has been improved on @colyseus/core
version 0.15.55
The step to exclude from matchmaking, lock rooms and calling a custom room.onBeforeShutdown()
method has been introduced on version @colyseus/core@0.15.55
.
These actions will be performed, in order, before the process is killed:
- The custom
gameServer.onBeforeShutdown()
is called, if defined. - The process is excluded from match-making.
- All existing rooms are locked (
room.lock()
), - All rooms
room.onBeforeShutdown()
is called.- You may override
room.onBeforeShutdown()
to perform custom actions before the room is disposed. - By default,
room.onBeforeShutdown()
simply callsroom.disconnect()
, which will triggerroom.onLeave()
for all clients, and thenroom.onDispose()
.
- You may override
- The server waits for all rooms to be disposed (room count must be zero).
- The Transport, Presence, and Driver are closed and disconnected.
- The custom
gameServer.onShutdown()
is called, if defined.
You may use async
functions or return a Promise
to perform asynchronous operations on onLeave
and onDispose
methods, as well as gameServer.onBeforeShutdown()
and gameServer.onShutdown()
.
Returning a Promise
¶
By returning a Promise
, the server will wait for them to be completed before killing the worker process.
import { Room } from "colyseus";
class MyRoom extends Room {
onLeave (client) {
return new Promise((resolve, reject) => {
doDatabaseOperation((err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
onDispose () {
return new Promise((resolve, reject) => {
doDatabaseOperation((err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
}
Using async
¶
The async
keyword will make your function return a Promise
under the hood. Read more about Async / Await.
import { Room } from "colyseus";
class MyRoom extends Room {
async onLeave (client) {
await doDatabaseOperation(client);
}
async onDispose () {
await removeRoomFromDatabase();
}
}
Process shutdown callback¶
You can also listen for process shutdown by setting a onShutdown
callback.