Introduction
Every code service has a global process object. Two of its members are supported for use in your own code:
|
Member |
Purpose |
|---|---|
|
|
A handler you assign to run cleanup when the platform stops a long-running service |
|
|
Schedules a callback to run as soon as the current work finishes, before the next event is processed |
Both members work the same way in the V8 and Duktape engines. Other properties that may exist on process (for example process.env) are internal compatibility placeholders and are not supported.
process.onShutdown
process.onShutdown is a function that the platform calls when it stops a running service. It is a no-op by default. Assign your own function to run cleanup logic such as publishing a final message or closing external resources.
process.onShutdown = function () {}
The handler takes no arguments and its return value is ignored.
When it runs
The handler is intended for stream services and preloaded services, which run continuously. It runs when the platform stops a service instance, for example when:
-
an administrator stops the service
-
auto balance or auto scale reduces the number of running instances
It does not run if the service process is terminated abruptly, such as a node crash.
Behavior
-
Grace period. After the platform starts stopping a service, it waits for a grace period before forcefully terminating it. The default is 15 seconds and is set by the
termination_grace_period_secondsengine configuration value. -
Async work is awaited. After the handler returns, the service exits once it has no pending asynchronous calls, queued events, or pending callbacks. Asynchronous work started in the handler, such as an MQTT publish, is allowed to finish. If it is still running when the grace period ends, the service is terminated.
-
Errors do not propagate. If the handler throws, the error is logged as a warning on the platform and shutdown continues.
-
Runs as the starting user. The handler runs with the permissions of the user who started the service, not the user or process that stopped it.
-
Assign it inside the service. The assignment must run before the service is stopped, for example inside the service function.
Example
This stream service publishes a message to the shutdown topic when it is stopped.
var client = new MQTT.Client();
function MyStreamService(req, resp) {
process.onShutdown = function () {
client.publish("shutdown", "I was shutdown");
};
}
process.nextTick
process.nextTick(callback, ...args) schedules callback to run once the code that is currently executing has finished, and before the next event, such as a timer or an incoming MQTT message, is processed. Any extra arguments are passed to the callback.
process.nextTick(function (a, b) {
log(a + b);
}, 1, 2);
This is useful for deferring work until the current function returns without waiting for a timer. A callback scheduled with process.nextTick runs before a callback scheduled with setTimeout(fn, 0).
setTimeout(function () { log("timeout"); }, 0);
process.nextTick(function () { log("tick"); });
log("sync");
// Logs: sync, tick, timeout