@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
212 lines • 11.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const console_1 = require("console");
const fs = require("fs");
const __1 = require("../..");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const path = require("path");
let color = (0, command_utils_1.getColor)("blue");
exports.default = (program) => {
program
.command("schedules")
.alias("js")
.option("-m, --mode [list|create|start|stop|template|info|delete]", "list | create | stop | template | info", "list")
.option("-f, --file <file>", ".mdsp.json file with job schedule definition", "jobmanager.schedule.mdsp.json")
.option("-i, --scheduleid <scheduleid>", "the schedule id")
.option("-n, --name <name>", "the name filter (contains) for list command")
.option("-s, --status <status>", "the status filter (equals, e.g. STOPPED, FAILED...) for list command")
.option("-d, --modelid <modelid>", "the modelid filter (equals) for list command")
.option("-o, --overwrite", "overwrite template file if it already exists")
.option("-k, --passkey <passkey>", "passkey")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color("list, create, start, stop or delete job schedules *"))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkRequiredParamaters(options);
const sdk = (0, command_utils_1.getSdk)(options);
color = (0, command_utils_1.adjustColor)(color, options, true);
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
switch (options.mode) {
case "list":
yield listSchedules(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
break;
case "stop":
yield stopSchedule(options, sdk);
break;
case "start":
yield startSchedule(options, sdk);
break;
case "delete":
yield deleteSchedule(options, sdk);
break;
case "create":
yield createSchedule(options, sdk);
break;
case "info":
yield scheduleInfo(options, sdk);
break;
default:
throw Error(`no such option: ${options.mode}`);
}
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp schedules --mode list \t\t\t\t\t list all job schedules`);
(0, console_1.log)(` mdsp schedules --mode list --status RUNNING \t list all RUNNING job schedules`);
(0, console_1.log)(` mdsp schedules --mode list --modelid <modelid> \t\t\t list all job schedules for specified model`);
(0, console_1.log)(` mdsp schedules --mode template \t\t\t\t\t create template file for job schedule creation`);
(0, console_1.log)(` mdsp schedules --mode create --file <templatefile> \t\t create job schedule`);
(0, console_1.log)(` mdsp schedules --mode info --scheduleid <scheduleid> \t\t\t get infos about the job schedule`);
(0, console_1.log)(` mdsp schedules --mode start --scheduleid <scheduleid> \t\t\t start job schedule with job id`);
(0, console_1.log)(` mdsp schedules --mode stop --scheduleid <scheduleid> \t\t\t stop job schedule with job id`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function createSchedule(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const schedule = JSON.parse(file.toString());
const result = yield sdk.GetJobManagerClient().PostSchedule(schedule);
console.log(`Created schedule ${color(result.id)} with status ${color(result.status)}.`);
});
}
function createTemplate(options) {
const templateType = {
inputFolderId: "<data-exchange-folderid>",
outputFolderId: "<data-exchange-folderid>",
configurationId: "<folderid>",
modelId: "<modelid>",
name: "<name>",
maximumExecutionTime: 3600,
scheduleString: "1 1 * * *",
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writescheduleToFile(options, templateType);
}
function writescheduleToFile(options, job) {
const fileName = options.file || `jobmanager.schedule.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(filePath, JSON.stringify(job, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp schedules --mode create --file ${fileName} \n\nto create the job schedule`);
}
function stopSchedule(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const id = options.scheduleid;
const result = yield sdk.GetJobManagerClient().StopSchedule(id);
console.log(`Sent stop signal to job schedule with id ${color(id)}. Schedule status: ${color(result.status)}.`);
});
}
function startSchedule(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const id = options.scheduleid;
const result = yield sdk.GetJobManagerClient().StartSchedule(id);
console.log(`Sent start signal to job schedule with id ${color(id)}. Schedule status: ${color(result.status)}.`);
});
}
function deleteSchedule(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const id = options.scheduleid;
yield sdk.GetJobManagerClient().DeleteSchedule(id);
console.log(`Deleted schedule with id ${color(id)}.`);
});
}
function listSchedules(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
const jobManagerClient = sdk.GetJobManagerClient();
let page = 0;
let schedules;
const filter = buildFilter(options);
(0, command_utils_1.verboseLog)(JSON.stringify(filter, null, 2), options.verbose);
console.log(`id ${color("status")} [${"scheduleString"}] ${color("name")} createdBy modelId`);
let scheduleCount = 0;
do {
schedules = (yield (0, __1.retry)(options.retry, () => jobManagerClient.GetSchedules({
pageNumber: page,
pageSize: 100,
filter: Object.keys(filter).length === 0 ? undefined : JSON.stringify(filter),
})));
schedules.schedules = schedules.schedules || [];
schedules.page = schedules.page || { totalPages: 0 };
for (const schedule of schedules.schedules || []) {
scheduleCount++;
console.log(`${schedule.id} ${color(schedule.status)} [${schedule.scheduleString}] ${color(schedule.name)} ${schedule.createdBy} ${schedule.modelId}`);
(0, command_utils_1.verboseLog)(JSON.stringify(schedule, null, 2), options.verbose);
}
} while (page++ < (schedules.page.totalPages || 0));
console.log(`${color(scheduleCount)} schedules listed.\n`);
});
}
function buildFilter(options) {
const filter = {};
if (options.name) {
filter.name = { contains: `${options.name}` };
}
if (options.status) {
filter.status = { eq: `${options.status}` };
}
if (options.modelid) {
filter.modelId = { eq: `${options.modelid}` };
}
return filter;
}
function scheduleInfo(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const id = options.scheduleid;
const schedule = yield sdk.GetJobManagerClient().GetSchedule(id);
console.log(`Id: ${color(schedule.id)}`);
console.log(`Name: ${color(schedule.name)}`);
console.log(`Schedule String: ${color(schedule.scheduleString)}`);
console.log(`Input FolderId: ${schedule.inputFolderId}`);
console.log(`Output FolderId: ${schedule.outputFolderId}`);
console.log(`Configuration Id: ${schedule.configurationId}`);
console.log(`Max execution time: ${schedule.maximumExecutionTime}`);
console.log(`Model Id: ${schedule.modelId}`);
console.log(`Status: ${color(schedule.status)}`);
console.log(`Created by: ${schedule.createdBy} on date ${schedule.creationDate}`);
(0, command_utils_1.verboseLog)(JSON.stringify(schedule, null, 2), options.verbose);
});
}
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.file &&
(0, command_utils_1.errorLog)("you have to provide a file with schedule parameters to create a schedule (see mdsp schedules --help for more details)", true);
options.mode === "stop" &&
!options.scheduleid &&
(0, command_utils_1.errorLog)("you have to provide the scheduleid of the schedule you want to stop (see mdsp schedules --help for more details)", true);
options.mode === "start" &&
!options.scheduleid &&
(0, command_utils_1.errorLog)("you have to provide the scheduleid of the schedule you want to start (see mdsp schedules --help for more details)", true);
options.mode === "info" &&
!options.scheduleid &&
(0, command_utils_1.errorLog)("you have to provide the scheduleid to get infos about the schedule (see mdsp schedules --help for more details)", true);
options.mode === "delete" &&
!options.scheduleid &&
(0, command_utils_1.errorLog)("you have to provide the scheduleid to delete the schedule (see mdsp schedules --help for more details)", true);
}
//# sourceMappingURL=jobmanager-schedules.js.map