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