@grouparoo/core
Version:
The Grouparoo Core
166 lines (165 loc) • 7.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatusTask = void 0;
const actionhero_1 = require("actionhero");
const cli_1 = require("../../../modules/cli");
const apm_1 = require("../../../modules/apm");
const status_1 = require("../../../modules/status");
const plugin_1 = require("../../../modules/plugin");
const clsTask_1 = require("../../../classes/tasks/clsTask");
const apiData_1 = require("../../../modules/apiData");
const runMode_1 = require("../../../modules/runMode");
class StatusTask extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "status";
this.description = "Calculate and store status. If we are running via the CLI, log it there too";
this.frequency = 1000 * 5; // every 5 seconds by default, but will be modified by `updateTaskFrequency` after the first run
this.queue = "system";
this.inputs = {
toStop: { required: false, formatter: apiData_1.APIData.ensureBoolean },
};
}
async runWithinTransaction({ toStop }, worker) {
return apm_1.APM.wrap(this.name, "task", worker, async () => {
if ((0, runMode_1.getGrouparooRunMode)() === "cli:run") {
// calculate stats inline
await status_1.Status.setAll();
}
else {
// distribute stats calculation
status_1.Status.statusSampleReporters.forEach(async (_, idx) => {
await actionhero_1.task.enqueue("status:sample", { index: idx });
});
}
const samples = await this.getSamples();
if ((0, runMode_1.getGrouparooRunMode)() === "cli:run")
this.logSamples(samples);
const complete = await this.checkForComplete(samples);
if ((0, runMode_1.getGrouparooRunMode)() === "cli:run" && complete) {
await this.logFinalSummary();
await this.stopServer(toStop);
}
await this.updateTaskFrequency();
});
}
async logFinalSummary() {
if (actionhero_1.env === "test")
return;
const finalSummaryLog = await status_1.FinalSummary.getFinalSummary();
cli_1.GrouparooCLI.logger.finalSummary(finalSummaryLog);
}
async checkForComplete(samples) {
let pendingItems = 0;
let pendingCollections = 0;
for (const topic in samples) {
for (const collection in samples[topic]) {
const metrics = samples[topic][collection];
const { metric } = metrics[metrics.length - 1];
if (metric.collection === "pending" ||
metric.collection === "deleted") {
pendingItems += metric.count;
pendingCollections++;
}
}
}
if (pendingCollections < 4)
return false; // not every required model has been checked yet (PENDING: record, runs, import, export)
return pendingItems > 0 ? false : true;
}
async stopServer(toStop = true) {
(0, actionhero_1.log)("All Tasks Complete!", "notice");
if (!toStop)
return false;
// do not await so the promise can end so the server can shut down
new Promise(async () => {
await actionhero_1.api.process.stop();
process.nextTick(() => process.exit(0));
});
}
async getSamples() {
const samples = await status_1.Status.get();
return samples;
}
logSamples(samples) {
var _a, _b, _c;
if (actionhero_1.env === "test")
return;
const totalItems = [];
const pendingItems = [];
const pendingRuns = [];
const pendingDeletions = [];
const pendingCollection = samples["Run"]
? samples["Run"]["pending"]
: undefined;
const activeRunIds = pendingCollection
? (_c = JSON.parse((_b = (_a = pendingCollection[pendingCollection.length - 1]) === null || _a === void 0 ? void 0 : _a.metric) === null || _b === void 0 ? void 0 : _b.value)) !== null && _c !== void 0 ? _c : []
: [];
for (const topic in samples) {
for (const collection in samples[topic]) {
const metrics = samples[topic][collection];
const { metric: latestMetric, timestamp: latestTimestamp } = metrics[metrics.length - 1];
if (latestMetric.collection === "totals") {
totalItems.push({
[latestMetric.topic]: [latestMetric.count],
});
}
if (latestMetric.collection === "pending") {
pendingItems.push({
[latestMetric.topic]: [latestMetric.count],
});
}
if (latestMetric.collection === "deleted" && latestMetric.count > 0) {
pendingDeletions.push({
[latestMetric.topic]: [latestMetric.count],
});
}
if (latestMetric.topic === "Run" &&
latestMetric.collection === "percentComplete") {
metrics.forEach(({ metric, timestamp }) => {
if (activeRunIds.includes(metric.key) &&
latestTimestamp === timestamp) {
pendingRuns.push({
[metric.value]: [
`${metric.count}%${metric.metadata ? ` (${metric.metadata})` : ""}`,
],
});
}
});
}
}
}
const logItems = [];
if (totalItems.length > 0) {
logItems.push({
header: "Total Items",
status: totalItems.reduce((s, arr) => Object.assign(s, arr), {}),
});
}
if (pendingItems.length > 0) {
logItems.push({
header: "Pending Items",
status: pendingItems.reduce((s, arr) => Object.assign(s, arr), {}),
});
}
if (pendingRuns.length > 0) {
logItems.push({
header: "Active Runs",
status: pendingRuns.reduce((s, arr) => Object.assign(s, arr), {}),
});
}
if (pendingDeletions.length > 0) {
logItems.push({
header: "Pending Deletions",
status: pendingDeletions.reduce((s, arr) => Object.assign(s, arr), {}),
});
}
if (logItems.length > 0)
cli_1.GrouparooCLI.logger.status("Status", logItems);
}
async updateTaskFrequency() {
const frequency = parseInt((await plugin_1.plugin.readSetting("interface", "status-calculation-frequency-seconds")).value) * 1000;
actionhero_1.api.tasks.tasks["status"].frequency = frequency;
}
}
exports.StatusTask = StatusTask;