1.1.0 • Published 3 years ago

@splunkdlt/managed-resource v1.1.0

Weekly downloads
56
License
Apache-2.0
Repository
github
Last release
3 years ago

@splunkdlt/managed-resource

A set of helpers to perform an orderly shutdown of a collector process.

Example

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');
    }
}