All files / Cognigy-CLI/build/lib playbooks.js

0% Statements 0/83
0% Branches 0/64
0% Functions 0/16
0% Lines 0/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                                                                                                                                                                                                                                     
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runPlaybooks = void 0;
const config_1 = __importDefault(require("../utils/config"));
const cognigyClient_1 = __importDefault(require("../utils/cognigyClient"));
const fs = __importStar(require("fs"));
const checks_1 = require("../utils/checks");
const runPlaybooks = async (playbookFile) => {
    const playbooksFile = playbookFile || './playbooks.json';
    if (!fs.existsSync(playbooksFile)) {
        console.log(`File ${playbookFile} not found. Exiting.`);
        process.exit(99);
    }
    const playbooksArray = JSON.parse(fs.readFileSync(playbooksFile).toString());
    const scheduledPlaybookRuns = [];
    for (const playbook of playbooksArray) {
        const result = await cognigyClient_1.default.schedulePlaybook(playbook);
        scheduledPlaybookRuns.push({
            playbookId: playbook.playbookId,
            taskId: result._id,
        });
    }
    const playbookRunResults = await checkPlaybookRuns(scheduledPlaybookRuns, 0);
    fs.writeFileSync('playbookRunResults.json', JSON.stringify(playbookRunResults.playbookRunStatuses, null, 2));
    if (playbookRunResults.status === 'successful') {
        console.log(`SUCCESS - all ${playbookRunResults.playbookRunStatuses.length} playbooks were executed successfully.`);
        process.exit(0);
    }
    else if (playbookRunResults.status === 'failure') {
        console.log(`FAILURE - see playbookRunResults.json for details`);
        process.exit(1);
    }
    else {
        console.log(`TIMEOUT - couldn't process all playbooks within the allotted time. You can adjust the timeout in the config.json file.`);
        process.exit(2);
    }
};
exports.runPlaybooks = runPlaybooks;
async function checkPlaybookRuns(scheduledPlaybookRuns, counter, realResolve) {
    counter++;
    return new Promise((resolve) => {
        const resolver = realResolve || resolve;
        setTimeout(async () => {
            const playbookRunStatuses = [];
            for (const scheduledPlaybookRun of scheduledPlaybookRuns) {
                await (0, checks_1.checkTask)(scheduledPlaybookRun.taskId);
                const task = await cognigyClient_1.default.readTask({
                    taskId: scheduledPlaybookRun.taskId,
                });
                const playbookRun = await cognigyClient_1.default.readPlaybookRun({
                    playbookId: scheduledPlaybookRun.playbookId,
                    playbookRunId: task.data.playbookRunId,
                });
                playbookRunStatuses.push({
                    playbookId: scheduledPlaybookRun.playbookId,
                    playbookRunId: playbookRun._id,
                    status: playbookRun.status,
                    steps: playbookRun.stepResults,
                });
            }
            if (playbookRunStatuses.every((pbRun) => pbRun.status === 'successful')) {
                resolver({
                    status: 'successful',
                    playbookRunStatuses,
                });
            }
            else if (playbookRunStatuses.every((pbRun) => pbRun.status === 'successful' || pbRun.status === 'failed')) {
                resolver({
                    status: 'failure',
                    playbookRunStatuses,
                });
            }
            else {
                if (counter < (config_1.default.playbookTimeoutSeconds || 10)) {
                    await checkPlaybookRuns(scheduledPlaybookRuns, counter, resolve);
                }
                else {
                    resolver({
                        status: 'timeout',
                        playbookRunStatuses,
                    });
                }
            }
        }, 1000);
    });
}