@grouparoo/core
Version:
The Grouparoo Core
133 lines (132 loc) • 5.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RetryExportsCLI = void 0;
const moment_1 = __importDefault(require("moment"));
const actionhero_1 = require("actionhero");
const cli_1 = require("../modules/cli");
const apiData_1 = require("../modules/apiData");
const Export_1 = require("../models/Export");
const Destination_1 = require("../models/Destination");
class RetryExportsCLI extends actionhero_1.CLI {
constructor() {
super();
this.name = "retryExports";
this.description = "Marks failed Exports that were created in a given time frame to be retried.";
this.inputs = {
start: {
required: false,
requiredValue: true,
formatter: apiData_1.APIData.ensureDate,
description: "Search for failed Exports created on or after this timestamp.",
},
startAgo: {
required: false,
requiredValue: true,
formatter: apiData_1.APIData.ensureNumber,
description: "Search for failed Exports created on or after a certain number of seconds ago. See --startAgoUnit to choose a different time unit.",
},
startAgoUnit: {
required: false,
requiredValue: true,
default: "second",
description: "Unit for --startAgo (e.g. seconds, minutes, hours, days...)",
formatter: (val) => {
const normalized = moment_1.default.normalizeUnits(val);
return normalized;
},
validator: (val) => {
if (!val)
throw new Error(`invalid unit of time`);
},
},
end: {
required: false,
requiredValue: true,
formatter: apiData_1.APIData.ensureDate,
description: "Search for failed Exports created on or before this timestamp. Defaults to the current time.",
},
preview: {
default: false,
formatter: (val) => val,
description: "When set, will not make any changes and only outputs the number of Exports that would be retried.",
flag: true,
letter: "p",
},
destinationIds: {
required: false,
requiredValue: true,
description: "Only retry Exports for specific Destinations. Defaults to all Destinations.",
letter: "d",
variadic: true,
},
};
this.preInitialize = () => {
cli_1.GrouparooCLI.setGrouparooRunMode(this);
cli_1.GrouparooCLI.setNextDevelopmentMode();
};
cli_1.GrouparooCLI.timestampOption(this);
}
async run({ params }) {
var _a, _b;
cli_1.GrouparooCLI.logCLI(this.name, false);
const hasRelativeStart = params.startAgo !== undefined;
const hasAbsoluteStart = params.start !== undefined;
if ((hasRelativeStart && hasAbsoluteStart) ||
(!hasRelativeStart && !hasAbsoluteStart)) {
return cli_1.GrouparooCLI.logger.fatal("One of --start or --startAgo must be specified");
}
const startDate = (_a = params.start) !== null && _a !== void 0 ? _a : (0, moment_1.default)().subtract(params.startAgo, params.startAgoUnit).toDate();
const endDate = (_b = params.end) !== null && _b !== void 0 ? _b : new Date();
cli_1.GrouparooCLI.logger.log(`Searching for failed Exports:\n`);
cli_1.GrouparooCLI.logger.log(`Start: ${startDate.toLocaleString()} ${params.startAgo
? `(${params.startAgo} ${params.startAgoUnit}s ago)`
: ""}`);
cli_1.GrouparooCLI.logger.log(`End: ${endDate.toLocaleString()}`);
const summaryItems = [];
const destinations = await this.getDestinations(params.destinationIds);
let totalCount = 0;
for (const destination of destinations) {
const count = await Export_1.Export.retryFailed(startDate, endDate, destination, !params.preview);
summaryItems.push({
header: `${destination.name} (${destination.id})`,
status: { "Failed Exports": [count] },
});
totalCount += count;
}
if (summaryItems.length) {
cli_1.GrouparooCLI.logger.status("Summary", summaryItems);
}
else {
cli_1.GrouparooCLI.logger.log("\nNo Destinations\n");
}
if (params.preview) {
cli_1.GrouparooCLI.logger.log(`ℹ️ (Preview) Found ${totalCount} failed Exports to retry.`);
}
else {
if (totalCount) {
cli_1.GrouparooCLI.logger.log(`✅ Success! ${totalCount} failed Exports marked to be retried. Run \`grouparoo run\` or \`grouparoo start\` to retry them.`);
}
else {
cli_1.GrouparooCLI.logger.log(`✨ Success! No failed Exports found to retry.`);
}
}
return true;
}
async getDestinations(destinationIds) {
const destinations = await Destination_1.Destination.findAll({
where: destinationIds ? { id: destinationIds } : {},
});
if (destinationIds) {
const foundDestinationIds = destinations.map((d) => d.id);
destinationIds.forEach((id) => {
if (!foundDestinationIds.includes(id))
return cli_1.GrouparooCLI.logger.fatal(`Destination with id "${id}" was not found`);
});
}
return destinations;
}
}
exports.RetryExportsCLI = RetryExportsCLI;