kist
Version:
Package Pipeline Processor
165 lines (164 loc) • 6.74 kB
JavaScript
"use strict";
// ============================================================================
// Import
// ============================================================================
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PipelineManager = void 0;
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const AbstractProcess_1 = require("../abstract/AbstractProcess");
const ConfigStore_1 = require("../config/ConfigStore");
const Pipeline_1 = require("./Pipeline");
// ============================================================================
// Class
// ============================================================================
/**
* Manages the lifecycle of the pipeline process and integrates with the
* LiveServer for client notifications upon pipeline events.
*/
class PipelineManager extends AbstractProcess_1.AbstractProcess {
// Constructor
// ========================================================================
/**
* Initializes the PipelineManager with a LiveServer instance.
*
* @param liveServer - The LiveServer instance to notify when the
* pipeline restarts.
*/
constructor(liveServer) {
super();
this.liveServer = liveServer;
// Parameters
// ========================================================================
/**
* The current instance of the pipeline process.
*/
this.pipelineProcess = null;
/**
* Flag to prevent overlapping restarts.
*/
this.isRestarting = false;
this.logInfo("PipelineManager initialized.");
}
// Methods
// ========================================================================
/**
* Runs the pipeline using the configuration from the `ConfigStore`.
* This method executes the pipeline stages directly, bypassing the CLI.
*/
runPipeline() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const config = ConfigStore_1.ConfigStore.getInstance().getConfig();
if (!config.stages || !Array.isArray(config.stages)) {
throw new Error("Invalid configuration: 'stages' must be an array.");
}
this.logInfo("Initializing pipeline...");
const pipeline = new Pipeline_1.Pipeline(config);
try {
yield pipeline.run();
this.logInfo("Pipeline execution finished successfully.");
(_a = this.liveServer) === null || _a === void 0 ? void 0 : _a.reloadClients();
}
catch (error) {
this.logError("Error during pipeline execution.", error);
throw error;
}
});
}
/**
* Restarts the pipeline process, stopping any currently running process.
* Notifies connected clients via the LiveServer after restarting.
*/
restartPipeline() {
if (this.isRestarting) {
this.logWarn("Pipeline restart already in progress. Skipping...");
return;
}
this.isRestarting = true;
if (this.pipelineProcess) {
this.logInfo("Stopping current pipeline process...");
this.stopPipeline();
}
this.logInfo("Starting pipeline...");
const scriptPath = path_1.default.resolve(process.cwd(), "dist/js/cli.js");
this.pipelineProcess = (0, child_process_1.spawn)("node", [scriptPath], {
stdio: "inherit",
});
this.attachProcessListeners();
this.isRestarting = false;
}
/**
* Attaches event listeners to the pipeline process for logging and
* notification purposes.
*/
attachProcessListeners() {
if (!this.pipelineProcess)
return;
this.pipelineProcess.on("close", (code) => {
var _a;
if (code !== 0) {
this.logError(`Pipeline process exited with code ${code}`);
}
else {
this.logInfo("Pipeline process exited successfully.");
}
(_a = this.liveServer) === null || _a === void 0 ? void 0 : _a.reloadClients();
});
this.pipelineProcess.on("error", (error) => {
this.logError("Error starting pipeline process.", error);
});
this.pipelineProcess.on("exit", (code, signal) => {
if (signal) {
this.logWarn(`Pipeline process was terminated with signal: ${signal}`);
}
else {
this.logInfo(`Pipeline process exited with code: ${code}`);
}
});
}
/**
* Stops the currently running pipeline process, if any.
* Gracefully kills the process and handles cleanup.
*/
stopPipeline() {
if (this.pipelineProcess) {
this.logInfo("Stopping pipeline process...");
this.pipelineProcess.kill("SIGTERM");
this.pipelineProcess = null;
this.logInfo("Pipeline process stopped.");
}
else {
this.logWarn("No pipeline process is currently running.");
}
}
/**
* Checks if the pipeline process is currently running.
* @returns True if the pipeline process is running, false otherwise.
*/
isPipelineRunning() {
return this.pipelineProcess !== null && !this.pipelineProcess.killed;
}
/**
* Restarts the pipeline with a delay, useful for throttling restarts when
* changes occur rapidly.
* @param delay - The delay in milliseconds before restarting the pipeline.
*/
restartPipelineWithDelay(delay = 1000) {
this.logInfo(`Delaying pipeline restart by ${delay} milliseconds...`);
setTimeout(() => this.restartPipeline(), delay);
}
}
exports.PipelineManager = PipelineManager;