UNPKG

8.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const cluster = require("cluster");
4const stringify = require("json-stringify-safe");
5const p = require("path");
6const applicationEvent_1 = require("./internal/env/applicationEvent");
7const ClusterMasterRequestProcessor_1 = require("./internal/transport/cluster/ClusterMasterRequestProcessor");
8const ClusterWorkerRequestProcessor_1 = require("./internal/transport/cluster/ClusterWorkerRequestProcessor");
9const EventStoringAutomationEventListener_1 = require("./internal/transport/EventStoringAutomationEventListener");
10const ExpressRequestProcessor_1 = require("./internal/transport/express/ExpressRequestProcessor");
11const ExpressServer_1 = require("./internal/transport/express/ExpressServer");
12const MetricEnabledAutomationEventListener_1 = require("./internal/transport/MetricEnabledAutomationEventListener");
13const showStartupMessages_1 = require("./internal/transport/showStartupMessages");
14const DefaultWebSocketRequestProcessor_1 = require("./internal/transport/websocket/DefaultWebSocketRequestProcessor");
15const payloads_1 = require("./internal/transport/websocket/payloads");
16const WebSocketClient_1 = require("./internal/transport/websocket/WebSocketClient");
17const logger_1 = require("./internal/util/logger");
18const string_1 = require("./internal/util/string");
19const BuildableAutomationServer_1 = require("./server/BuildableAutomationServer");
20const statsd_1 = require("./util/statsd");
21class AutomationClient {
22 constructor(configuration) {
23 this.configuration = configuration;
24 this.defaultListeners = [
25 new MetricEnabledAutomationEventListener_1.MetricEnabledAutomationEventListener(),
26 new EventStoringAutomationEventListener_1.EventStoringAutomationEventListener(),
27 ];
28 this.automations = new BuildableAutomationServer_1.BuildableAutomationServer(configuration);
29 global.__runningAutomationClient = this;
30 }
31 get automationServer() {
32 return this.automations;
33 }
34 withCommandHandler(chm) {
35 this.automations.registerCommandHandler(chm);
36 return this;
37 }
38 withEventHandler(event) {
39 this.automations.registerEventHandler(event);
40 return this;
41 }
42 withIngester(ingester) {
43 this.automations.registerIngester(ingester);
44 return this;
45 }
46 processCommand(command, callback) {
47 if (this.webSocketHandler) {
48 return this.webSocketHandler.processCommand(command, callback);
49 }
50 else if (this.httpHandler) {
51 return this.httpHandler.processCommand(command, callback);
52 }
53 else {
54 throw new Error("No request processor available");
55 }
56 }
57 processEvent(event, callback) {
58 if (this.webSocketHandler) {
59 return this.webSocketHandler.processEvent(event, callback);
60 }
61 else if (this.httpHandler) {
62 return this.httpHandler.processEvent(event, callback);
63 }
64 else {
65 throw new Error("No request processor available");
66 }
67 }
68 run() {
69 this.configureLogging();
70 this.configureStatsd();
71 const clientSig = `${this.configuration.name}:${this.configuration.version}`;
72 const clientConf = stringify(this.configuration, string_1.obfuscateJson);
73 if (!this.configuration.cluster.enabled) {
74 logger_1.logger.info(`Starting Atomist automation client ${clientSig}`);
75 logger_1.logger.debug(`Using automation client configuration: ${clientConf}`);
76 if (this.configuration.ws.enabled) {
77 return Promise.all([
78 this.runWs(() => this.setupWebSocketRequestHandler()),
79 this.runHttp(() => this.setupExpressRequestHandler()),
80 ])
81 .then(() => this.setupApplicationEvents())
82 .then(() => this.printStartupMessage());
83 }
84 else {
85 return this.runHttp(() => this.setupExpressRequestHandler())
86 .then(() => this.setupApplicationEvents())
87 .then(() => this.printStartupMessage());
88 }
89 }
90 else if (cluster.isMaster) {
91 logger_1.logger.info(`Starting Atomist automation client master ${clientSig}`);
92 logger_1.logger.debug(`Using automation client configuration: ${clientConf}`);
93 this.webSocketHandler = this.setupWebSocketClusterRequestHandler();
94 return this.webSocketHandler.run()
95 .then(() => {
96 return Promise.all([
97 this.runWs(() => this.webSocketHandler),
98 this.runHttp(() => this.setupExpressRequestHandler()),
99 ])
100 .then(() => this.setupApplicationEvents())
101 .then(() => this.printStartupMessage());
102 });
103 }
104 else if (cluster.isWorker) {
105 logger_1.logger.info(`Starting Atomist automation client worker ${clientSig}`);
106 return Promise.resolve(this.setupWebSocketClusterWorkerRequestHandler())
107 .then(workerProcessor => {
108 this.webSocketHandler = workerProcessor;
109 return Promise.resolve();
110 });
111 }
112 }
113 configureStatsd() {
114 if (this.configuration.statsd.enabled === true) {
115 this.defaultListeners.push(new statsd_1.StatsdAutomationEventListener(this.configuration));
116 }
117 }
118 configureLogging() {
119 logger_1.setLogLevel(this.configuration.logging.level);
120 if (this.configuration.logging.file.enabled === true) {
121 let filename = p.join(".", "log", `${this.configuration.name.replace(/^.*\//, "")}.log`);
122 if (this.configuration.logging.file.name) {
123 filename = this.configuration.logging.file.name;
124 }
125 logger_1.addFileTransport(filename, this.configuration.logging.file.level || this.configuration.logging.level);
126 }
127 }
128 setupWebSocketClusterRequestHandler() {
129 return new ClusterMasterRequestProcessor_1.ClusterMasterRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners], this.configuration.cluster.workers);
130 }
131 setupWebSocketClusterWorkerRequestHandler() {
132 return ClusterWorkerRequestProcessor_1.startWorker(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]);
133 }
134 setupWebSocketRequestHandler() {
135 return new DefaultWebSocketRequestProcessor_1.DefaultWebSocketRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]);
136 }
137 setupApplicationEvents() {
138 if (this.configuration.applicationEvents.enabled) {
139 if (this.configuration.applicationEvents.workspaceId) {
140 return applicationEvent_1.registerApplicationEvents(this.configuration.applicationEvents.workspaceId);
141 }
142 else if (this.configuration.workspaceIds.length > 0) {
143 return applicationEvent_1.registerApplicationEvents(this.configuration.workspaceIds[0]);
144 }
145 }
146 return Promise.resolve();
147 }
148 setupExpressRequestHandler() {
149 return new ExpressRequestProcessor_1.ExpressRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]);
150 }
151 runWs(handlerMaker) {
152 const payloadOptions = {};
153 if (this.configuration.ws && this.configuration.ws.compress) {
154 payloadOptions.accept_encoding = "gzip";
155 }
156 this.webSocketHandler = handlerMaker();
157 this.webSocketClient = new WebSocketClient_1.WebSocketClient(() => payloads_1.prepareRegistration(this.automations.automations, payloadOptions, this.configuration.metadata), this.configuration, this.webSocketHandler);
158 return this.webSocketClient.start();
159 }
160 runHttp(handlerMaker) {
161 if (!this.configuration.http.enabled) {
162 return;
163 }
164 this.httpHandler = handlerMaker();
165 this.httpServer = new ExpressServer_1.ExpressServer(this.automations, this.configuration, this.httpHandler);
166 return Promise.resolve();
167 }
168 printStartupMessage() {
169 return showStartupMessages_1.showStartupMessages(this.configuration, this.automations.automations);
170 }
171}
172exports.AutomationClient = AutomationClient;
173function automationClient(configuration) {
174 const client = new AutomationClient(configuration);
175 configuration.commands.forEach(c => {
176 client.withCommandHandler(c);
177 });
178 configuration.events.forEach(e => {
179 client.withEventHandler(e);
180 });
181 configuration.ingesters.forEach(e => {
182 client.withIngester(e);
183 });
184 return client;
185}
186exports.automationClient = automationClient;
187//# sourceMappingURL=automationClient.js.map
\No newline at end of file