Skip to content

Server startup and shutdown

The server accepts connections by default, including while its entry script is waiting on a top-level await. Call revo.allowConnections(false) before starting asynchronous initialization, then revo.allowConnections(true) once your data and handlers are ready.

This example assumes your mod uses PostgreSQL through pg. Install pg alongside your server scripts and set the DATABASE_URL environment variable to your database’s connection string.

The entry script checks the connection before accepting players and closes the pool when the server stops:

Server entry
// @ts-check
/// <reference types="@revomp/types-server" />
import * as revo from 'revo-server';
import { Pool } from 'pg';
revo.allowConnections(false);
const database = new Pool({
connectionString: process.env.DATABASE_URL,
connectionTimeoutMillis: 5000,
});
database.on('error', (error) => {
revo.log('Database connection error:', error);
});
let stopping = false;
revo.on('serverStop', async () => {
stopping = true;
await database.end();
});
try {
await database.query('SELECT 1');
if (!stopping) {
revo.allowConnections(true);
}
} catch (error) {
revo.log('Server startup failed:', error);
process.exitCode = 1;
revo.shutdown();
}

Finish loading world data and registering gameplay handlers before calling allowConnections(true). The stopping flag prevents reopening connections if shutdown begins during initialization.

Static imports evaluate their dependencies first. For dependencies that perform asynchronous setup during evaluation, use a dynamic import() after closing the connection gate.

If initialization fails, the example logs the error, sets exit code 1 and requests graceful shutdown. The deployment guide covers service restart policies.

Call revo.allowConnections(false) to reject new connections during maintenance. Players who have completed the handshake stay connected. New attempts receive Server is not accepting connections and must reconnect after you reopen access.

With overload_guard enabled, admission also depends on the server’s load. Both the script gate and the overload guard must allow a new connection. See load-protection messages when diagnosing a rejection.

Use an async handler for serverStop and await any saves or cleanup work. In the example above, the handler waits for database.end() to close the pool. Await pending player or world saves before that call in the same handler.

All handlers share a 10-second deadline. The server waits for their returned Promises. If one handler throws or rejects, the remaining handlers still run. An error or timeout is logged and sets exit code 1.

Existing players, script callbacks and the server loop keep running while cleanup is pending. Stop your mod’s timers and block actions that change persistent state before the final save.

Use revo.shutdown() to request a graceful stop from a script. It returns immediately, and serverStop runs as shutdown proceeds. Ctrl+C and SIGTERM also run this cleanup. Successful shutdown uses process.exitCode, which defaults to 0.

process.exit() ends the process without waiting for asynchronous cleanup. Keep shutdown work asynchronous so the server can process it within the deadline.