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

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

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

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


exports.clearExistingTimeoutFunction = () => {
  if (timeoutId){
    clearTimeout(timeoutId);
  }
};
