UNPKG

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