UNPKG

@wocker/cron-plugin

Version:
156 lines (155 loc) 6.56 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; 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 }); exports.CronController = void 0; const core_1 = require("@wocker/core"); const CronService_1 = require("../services/CronService"); let CronController = class CronController { constructor(appConfigService, cronService, dockerService, projectService) { this.appConfigService = appConfigService; this.cronService = cronService; this.dockerService = dockerService; this.projectService = projectService; this.containerName = "cron.ws"; } start(build, restart) { return __awaiter(this, void 0, void 0, function* () { console.info("Starting cron..."); yield this.cronService.start(restart, build); }); } stop() { return __awaiter(this, void 0, void 0, function* () { console.info("Stopping cron..."); yield this.cronService.stop(); }); } logs() { return __awaiter(this, void 0, void 0, function* () { yield this.cronService.logs(); }); } crontab(filename, name, list, edit, remove) { return __awaiter(this, void 0, void 0, function* () { if (name) { yield this.projectService.cdProject(name); } const project = yield this.projectService.get(); if (edit) { yield this.cronService.edit(project.containerName); } else if (list) { return this.cronService.getCrontab(project.containerName); } else if (filename) { const fs = new core_1.FileSystem(this.appConfigService.pwd()); const file = fs.readFile(filename); yield this.cronService.setCrontab(project.containerName, file.toString()); } else if (remove) { yield this.cronService.setCrontab(project.containerName, ""); } else if (!filename && !process.stdin.isTTY) { const crontab = yield new Promise((resolve, reject) => { let res = ""; process.stdin.on("data", (data) => { res += data.toString(); }); process.stdin.on("end", () => { resolve(res); }); process.stdin.on("error", reject); }); yield this.cronService.setCrontab(project.containerName, crontab); } yield this.dockerService.exec(this.containerName, [ "bash", "-c", [ "docker-gen -notify \"crontab /root/app/crontab.txt\" /root/app/crontab.tmpl /root/app/crontab.txt" ].join(" ") ]); }); } }; exports.CronController = CronController; __decorate([ (0, core_1.Command)("cron:start"), __param(0, (0, core_1.Option)("build", { type: "boolean", alias: "b", description: "Build image" })), __param(1, (0, core_1.Option)("restart", { type: "boolean", alias: "r", description: "Restart service" })), __metadata("design:type", Function), __metadata("design:paramtypes", [Boolean, Boolean]), __metadata("design:returntype", Promise) ], CronController.prototype, "start", null); __decorate([ (0, core_1.Command)("cron:stop"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], CronController.prototype, "stop", null); __decorate([ (0, core_1.Command)("cron:logs"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], CronController.prototype, "logs", null); __decorate([ (0, core_1.Command)("crontab [filename]"), __param(0, (0, core_1.Param)("filename")), __param(1, (0, core_1.Option)("name", { type: "string", alias: "n", description: "Project name" })), __param(2, (0, core_1.Option)("list", { type: "boolean", alias: "l", description: "Show crontab" })), __param(3, (0, core_1.Option)("edit", { type: "boolean", alias: "e", description: "Edit current crontab" })), __param(4, (0, core_1.Option)("remove", { type: "boolean", alias: "r", description: "Remove current crontab" })), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, Boolean, Boolean, Boolean]), __metadata("design:returntype", Promise) ], CronController.prototype, "crontab", null); exports.CronController = CronController = __decorate([ (0, core_1.Controller)(), __metadata("design:paramtypes", [core_1.AppConfigService, CronService_1.CronService, core_1.DockerService, core_1.ProjectService]) ], CronController);