UNPKG

8.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const cluster = require("cluster");
4const stringify = require("json-stringify-safe");
5const applicationEvent_1 = require("./internal/env/applicationEvent");
6const ClusterMasterRequestProcessor_1 = require("./internal/transport/cluster/ClusterMasterRequestProcessor");
7const ClusterWorkerRequestProcessor_1 = require("./internal/transport/cluster/ClusterWorkerRequestProcessor");
8const EventStoringAutomationEventListener_1 = require("./internal/transport/EventStoringAutomationEventListener");
9const ExpressRequestProcessor_1 = require("./internal/transport/express/ExpressRequestProcessor");
10const ExpressServer_1 = require("./internal/transport/express/ExpressServer");
11const MetricEnabledAutomationEventListener_1 = require("./internal/transport/MetricEnabledAutomationEventListener");
12const showStartupMessages_1 = require("./internal/transport/showStartupMessages");
13const DefaultWebSocketRequestProcessor_1 = require("./internal/transport/websocket/DefaultWebSocketRequestProcessor");
14const payloads_1 = require("./internal/transport/websocket/payloads");
15const WebSocketClient_1 = require("./internal/transport/websocket/WebSocketClient");
16const string_1 = require("./internal/util/string");
17const BuildableAutomationServer_1 = require("./server/BuildableAutomationServer");
18const logger_1 = require("./util/logger");
19const statsd_1 = require("./util/statsd");
20class AutomationClient {
21 constructor(configuration) {
22 this.configuration = configuration;
23 this.defaultListeners = [
24 new MetricEnabledAutomationEventListener_1.MetricEnabledAutomationEventListener(),
25 new EventStoringAutomationEventListener_1.EventStoringAutomationEventListener(),
26 new showStartupMessages_1.StartupMessageAutomationEventListener(),
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 logger_1.configureLogging(logger_1.clientLoggingConfiguration(this.configuration));
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.raiseStartupEvent());
83 }
84 else {
85 return this.runHttp(() => this.setupExpressRequestHandler())
86 .then(() => this.setupApplicationEvents())
87 .then(() => this.raiseStartupEvent());
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.raiseStartupEvent());
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 this.raiseStartupEvent();
110 });
111 }
112 }
113 raiseStartupEvent() {
114 return [...this.configuration.listeners, ...this.defaultListeners].filter(l => l.startupSuccessful)
115 .map(l => () => l.startupSuccessful(this))
116 .reduce((p, f) => p.then(f), Promise.resolve());
117 }
118 configureStatsd() {
119 if (this.configuration.statsd.enabled === true) {
120 this.defaultListeners.push(new statsd_1.StatsdAutomationEventListener(this.configuration));
121 }
122 }
123 setupWebSocketClusterRequestHandler() {
124 return new ClusterMasterRequestProcessor_1.ClusterMasterRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners], this.configuration.cluster.workers);
125 }
126 setupWebSocketClusterWorkerRequestHandler() {
127 return ClusterWorkerRequestProcessor_1.startWorker(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]);
128 }
129 setupWebSocketRequestHandler() {
130 return new DefaultWebSocketRequestProcessor_1.DefaultWebSocketRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]);
131 }
132 setupApplicationEvents() {
133 if (this.configuration.applicationEvents.enabled) {
134 if (this.configuration.applicationEvents.workspaceId) {
135 return applicationEvent_1.registerApplicationEvents(this.configuration.applicationEvents.workspaceId);
136 }
137 else if (this.configuration.workspaceIds.length > 0) {
138 return applicationEvent_1.registerApplicationEvents(this.configuration.workspaceIds[0]);
139 }
140 }
141 return Promise.resolve();
142 }
143 setupExpressRequestHandler() {
144 return new ExpressRequestProcessor_1.ExpressRequestProcessor(this.automations, this.configuration, [...this.defaultListeners, ...this.configuration.listeners]);
145 }
146 runWs(handlerMaker) {
147 const payloadOptions = {};
148 if (this.configuration.ws && this.configuration.ws.compress) {
149 payloadOptions.accept_encoding = "gzip";
150 }
151 this.webSocketHandler = handlerMaker();
152 this.webSocketClient = new WebSocketClient_1.WebSocketClient(() => payloads_1.prepareRegistration(this.automations.automations, payloadOptions, this.configuration.metadata), this.configuration, this.webSocketHandler);
153 return this.webSocketClient.start();
154 }
155 runHttp(handlerMaker) {
156 if (!this.configuration.http.enabled) {
157 return;
158 }
159 this.httpHandler = handlerMaker();
160 this.httpServer = new ExpressServer_1.ExpressServer(this.automations, this.configuration, this.httpHandler);
161 return this.httpServer.run();
162 }
163}
164exports.AutomationClient = AutomationClient;
165function automationClient(configuration) {
166 const client = new AutomationClient(configuration);
167 configuration.commands.forEach(c => {
168 client.withCommandHandler(c);
169 });
170 configuration.events.forEach(e => {
171 client.withEventHandler(e);
172 });
173 configuration.ingesters.forEach(e => {
174 client.withIngester(e);
175 });
176 return client;
177}
178exports.automationClient = automationClient;
179//# sourceMappingURL=automationClient.js.map
\No newline at end of file