UNPKG

@atomist/automation-client

Version:
187 lines • 8.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cluster = require("cluster"); const stringify = require("json-stringify-safe"); const p = require("path"); const applicationEvent_1 = require("./internal/env/applicationEvent"); const ClusterMasterRequestProcessor_1 = require("./internal/transport/cluster/ClusterMasterRequestProcessor"); const ClusterWorkerRequestProcessor_1 = require("./internal/transport/cluster/ClusterWorkerRequestProcessor"); const EventStoringAutomationEventListener_1 = require("./internal/transport/EventStoringAutomationEventListener"); const ExpressRequestProcessor_1 = require("./internal/transport/express/ExpressRequestProcessor"); const ExpressServer_1 = require("./internal/transport/express/ExpressServer"); const MetricEnabledAutomationEventListener_1 = require("./internal/transport/MetricEnabledAutomationEventListener"); const showStartupMessages_1 = require("./internal/transport/showStartupMessages"); const DefaultWebSocketRequestProcessor_1 = require("./internal/transport/websocket/DefaultWebSocketRequestProcessor"); const payloads_1 = require("./internal/transport/websocket/payloads"); const WebSocketClient_1 = require("./internal/transport/websocket/WebSocketClient"); const logger_1 = require("./internal/util/logger"); const string_1 = require("./internal/util/string"); const BuildableAutomationServer_1 = require("./server/BuildableAutomationServer"); const statsd_1 = require("./util/statsd"); class AutomationClient { constructor(configuration) { this.configuration = configuration; this.defaultListeners = [ new MetricEnabledAutomationEventListener_1.MetricEnabledAutomationEventListener(), new EventStoringAutomationEventListener_1.EventStoringAutomationEventListener(), ]; this.automations = new BuildableAutomationServer_1.BuildableAutomationServer(configuration); global.__runningAutomationClient = this; } get automationServer() { return this.automations; } withCommandHandler(chm) { this.automations.registerCommandHandler(chm); return this; } withEventHandler(event) { this.automations.registerEventHandler(event); return this; } withIngester(ingester) { this.automations.registerIngester(ingester); return this; } processCommand(command, callback) { if (this.webSocketHandler) { return this.webSocketHandler.processCommand(command, callback); } else if (this.httpHandler) { return this.httpHandler.processCommand(command, callback); } else { throw new Error("No request processor available"); } } processEvent(event, callback) { if (this.webSocketHandler) { return this.webSocketHandler.processEvent(event, callback); } else if (this.httpHandler) { return this.httpHandler.processEvent(event, callback); } else { throw new Error("No request processor available"); } } run() { this.configureLogging(); this.configureStatsd(); const clientSig = `${this.configuration.name}:${this.configuration.version}`; const clientConf = stringify(this.configuration, string_1.obfuscateJson); if (!this.configuration.cluster.enabled) { logger_1.logger.info(`Starting Atomist automation client ${clientSig}`); logger_1.logger.debug(`Using automation client configuration: ${clientConf}`); if (this.configuration.ws.enabled) { return Promise.all([ this.runWs(() => this.setupWebSocketRequestHandler()), this.runHttp(() => this.setupExpressRequestHandler()), ]) .then(() => this.setupApplicationEvents()) .then(() => this.printStartupMessage()); } else { return this.runHttp(() => this.setupExpressRequestHandler()) .then(() => this.setupApplicationEvents()) .then(() => this.printStartupMessage()); } } else if (cluster.isMaster) { logger_1.logger.info(`Starting Atomist automation client master ${clientSig}`); logger_1.logger.debug(`Using automation client configuration: ${clientConf}`); this.webSocketHandler = this.setupWebSocketClusterRequestHandler(); return this.webSocketHandler.run() .then(() => { return Promise.all([ this.runWs(() => this.webSocketHandler), this.runHttp(() => this.setupExpressRequestHandler()), ]) .then(() => this.setupApplicationEvents()) .then(() => this.printStartupMessage()); }); } else if (cluster.isWorker) { logger_1.logger.info(`Starting Atomist automation client worker ${clientSig}`); return Promise.resolve(this.setupWebSocketClusterWorkerRequestHandler()) .then(workerProcessor => { this.webSocketHandler = workerProcessor; return Promise.resolve(); }); } } configureStatsd() { if (this.configuration.statsd.enabled === true) { this.defaultListeners.push(new statsd_1.StatsdAutomationEventListener(this.configuration)); } } configureLogging() { logger_1.setLogLevel(this.configuration.logging.level); if (this.configuration.logging.file.enabled === true) { let filename = p.join(".", "log", `${this.configuration.name.replace(/^.*\//, "")}.log`); if (this.configuration.logging.file.name) { filename = this.configuration.logging.file.name; } logger_1.addFileTransport(filename, this.configuration.logging.file.level || this.configuration.logging.level); } } setupWebSocketClusterRequestHandler() { return new ClusterMasterRequestProcessor_1.ClusterMasterRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners], this.configuration.cluster.workers); } setupWebSocketClusterWorkerRequestHandler() { return ClusterWorkerRequestProcessor_1.startWorker(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]); } setupWebSocketRequestHandler() { return new DefaultWebSocketRequestProcessor_1.DefaultWebSocketRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]); } setupApplicationEvents() { if (this.configuration.applicationEvents.enabled) { if (this.configuration.applicationEvents.workspaceId) { return applicationEvent_1.registerApplicationEvents(this.configuration.applicationEvents.workspaceId); } else if (this.configuration.workspaceIds.length > 0) { return applicationEvent_1.registerApplicationEvents(this.configuration.workspaceIds[0]); } } return Promise.resolve(); } setupExpressRequestHandler() { return new ExpressRequestProcessor_1.ExpressRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]); } runWs(handlerMaker) { const payloadOptions = {}; if (this.configuration.ws && this.configuration.ws.compress) { payloadOptions.accept_encoding = "gzip"; } this.webSocketHandler = handlerMaker(); this.webSocketClient = new WebSocketClient_1.WebSocketClient(() => payloads_1.prepareRegistration(this.automations.automations, payloadOptions, this.configuration.metadata), this.configuration, this.webSocketHandler); return this.webSocketClient.start(); } runHttp(handlerMaker) { if (!this.configuration.http.enabled) { return; } this.httpHandler = handlerMaker(); this.httpServer = new ExpressServer_1.ExpressServer(this.automations, this.configuration, this.httpHandler); return Promise.resolve(); } printStartupMessage() { return showStartupMessages_1.showStartupMessages(this.configuration, this.automations.automations); } } exports.AutomationClient = AutomationClient; function automationClient(configuration) { const client = new AutomationClient(configuration); configuration.commands.forEach(c => { client.withCommandHandler(c); }); configuration.events.forEach(e => { client.withEventHandler(e); }); configuration.ingesters.forEach(e => { client.withIngester(e); }); return client; } exports.automationClient = automationClient; //# sourceMappingURL=automationClient.js.map