Put the application in maintenance with PAUSESERVER()
PAUSESERVER(name, true) pauses the server and PAUSESERVER(name, false) resumes it. While at least one name is paused, every request is answered with the status 503 and the built-in maintenance page (a request made with xhr gets a 503 error answer instead of the page). The name only identifies who paused it, so that two parts of the application can pause and resume independently: the server answers again when all the names were resumed.
Without the second argument the call toggles: PAUSESERVER(name) pauses when the name was not paused and resumes when it was, and returns the new state (true when paused).
Because a paused server answers 503 to everything, including the route that would resume it, resume from your own code: a timer, a signal, a task that finishes. The example pauses for a short time and resumes by itself.
exports.install = function() {
ROUTE('GET /maintenance/', maintenance);
ROUTE('GET /toggle/', toggle);
ROUTE('GET /ping/', ping);
};
function maintenance($) {
const state = PAUSESERVER('maintenance', true);
setTimeout(() => PAUSESERVER('maintenance', false), 600);
$.json({ paused: state });
}
function toggle($) {
const first = PAUSESERVER('toggled');
const second = PAUSESERVER('toggled');
$.json({ first: first, second: second });
}
function ping($) {
$.json({ ok: true });
}