Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Latest commit

 

History

History

managed-resource

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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