-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogs.js
More file actions
147 lines (136 loc) · 4.12 KB
/
logs.js
File metadata and controls
147 lines (136 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const assert = require('assert');
const debug = require('debug')('buildshuttle-logging');
const stream = require('stream');
let kafka = require('kafka-node');
const common = require('./common.js');
if (process.env.TEST_MODE) {
console.log('Running in test mode (using test kafka broker)');
kafka = require('./test/support/kafka-mock.js'); // eslint-disable-line
}
class Logs extends stream.Writable {
constructor(kafkaHost, app, appUUID, space, buildUUID, buildNumber) {
super();
this.kafkaHost = kafkaHost;
this.app = app;
this.app_uuid = appUUID;
this.space = space;
this.build_uuid = buildUUID;
this.build_number = buildNumber;
this._open = false;
this.kafkaConnection = null;
this.logInterval = null;
this.logStream = '';
}
_write(chunk, enc, next) {
debug('logs._write called');
if(!this._open) {
debug('logs_write called before it was opened.');
}
this.send(chunk.toString()).then(next).catch(next);
}
open() {
if (this._open) {
return new Promise((res) => res);
}
debug('opening logging end points');
return new Promise((resolve, reject) => {
try {
if (this.kafkaHost) {
debug(`connecting to kafka on ${this.kafkaHost}`);
this.kafkaConnection = new kafka.Producer(
new kafka.KafkaClient({ kafkaHost: this.kafkaHost }), { requireAcks: 0 },
);
this.kafkaConnection.on('ready', () => {
debug(`connected to kafka on ${this.kafkaHost}`);
this._open = true;
resolve();
});
this.kafkaConnection.on('error', (e) => {
this._open = false;
console.log(`A kafka error occured: ${e.message}\n${e.stack}`);
process.exit(1);
});
} else {
this._open = true;
debug('kafka was not provided, not streaming logs');
resolve();
}
} catch (e) {
this._open = false;
console.log(e);
reject(e);
}
});
}
sendLogsToKafka(event) {
debug('sending logs to kafka');
return new Promise((resolve, reject) => {
if (this.kafkaConnection) {
const msgs = event.trim().split('\n').map((x) => {
if (process.env.SHOW_BUILD_LOGS) {
console.log(x);
}
return {
topic: (process.env.KAFKA_TOPIC || 'alamobuildlogs'),
messages: [JSON.stringify({
metadata: `${this.app}-${this.space}`,
build: this.build_number,
job: this.build_number.toString(),
message: x,
})],
};
});
this.kafkaConnection.send(msgs, (err) => {
if (err) {
console.log(`Unable to send traffic to kafka: ${err.message}\n${err.stack}`);
reject(err);
} else {
resolve();
}
});
} else {
if (process.env.SHOW_BUILD_LOGS) {
event.trim().split('\n').map((x) => console.log(x.trim()));
}
resolve();
}
});
}
async flushLogsToS3() {
try {
debug('flushing logs to s3');
if (this.logInterval) {
clearInterval(this.logInterval);
}
this.logInterval = null;
if (this.logStream) {
await common.putObject(
`${this.app}-${this.app_uuid}-${this.build_number}.logs`,
this.logStream.split('\r\n').join('\n'),
);
}
} catch (e) {
console.log(`Error, unable to flush logs: ${e.message}\n${e.stack}`);
}
}
async sendLogsToS3(event) {
if (!this.logInterval) {
this.logInterval = setTimeout(this.flushLogsToS3.bind(this), 2000);
}
this.logStream += event;
}
_final(next) {
debug(`closing and flushing logs for ${this.build_uuid}`);
this.flushLogsToS3().then(() => this.sendLogsToKafka('Build finished').then(next).catch(next)).catch(next);
}
async send(event) {
try {
debug('sending logs');
await this.sendLogsToKafka(event);
await this.sendLogsToS3(event);
} catch (e) {
console.log(`Error sending logs ${e.message}\n${e.stack}`);
}
}
}
module.exports = Logs;