A set of helpers to perform an orderly shutdown of a collector process.
The following example illustrates a tyipcal flow of a program using the managed-resource package:
const testResource = new TestResource();
const resources = [testResource];
try {
await Promise.race([runMainBackgroundJob(testResource), waitForSignal('SIGINT'), waitForSignal('SIGHUP')]);
} catch (e) {
console.error('Caught unexpected error', e);
} finally {
const cleanShutdown = await shutdownAll(resources, 60_0000);
console.log('Shutdown complete');
process.exit(cleanShutdown ? 0 : 1);
}
Resources passed to shutdownAll()
need to implement the ManagedResource
interface - which mainly requires to provide a shutdown()
method.
class TestResource implements ManagedResource {
// ...
async shutdown() {
console.log('Shutting down test resource');
await sleep(500);
console.log('Test resource shutdown complete');
}
}