const { activeProcesses } = require('./UtilProcess');

let shouldExit = false;

exports.checkExitSignal = () => {
    if (shouldExit
        && !activeProcesses.scheduleDocuments
        && !activeProcesses.systemAction) {
        process.exit(0);
    }
};

exports.promiseTimeout = (ms, promise) => {
    let timeout = new Promise((resolve, reject) => {
        let id = setTimeout(() => {
            clearTimeout(id);
            shouldExit = true;
            const error = new Error('Timed out in ' + ms + ' ms.');
            reject(error);
        }, ms);
    });

    return Promise.race([
        promise,
        timeout
    ]);
};