-
Notifications
You must be signed in to change notification settings - Fork 64
/
ember-connection.js
62 lines (49 loc) · 1.83 KB
/
ember-connection.js
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
const nssocket = require('nssocket');
const _ = require('lodash');
const log = require('@cardstack/logger')('cardstack/ember-connection');
const HUB_HEARTBEAT_TIMEOUT = 7.5 * 1000; // longer than the heartbeat interval of 1 second
module.exports = class EmberConnector {
constructor({ orchestrator, heartbeat, ready }) {
this.orchestrator = orchestrator;
if (heartbeat) {
this.stopLater = _.debounce(this._stop.bind(this), HUB_HEARTBEAT_TIMEOUT);
}
let that = this;
this._server = nssocket.createServer(async function(socket) {
log.info('Connection established from ember-cli');
// Docker does some weird stuff for containers with published ports
// before they start actually listening on the port. Long story short,
// we need a handshake when we first establish a connection.
socket.data('hand', function() {
socket.send('shake');
});
// Ember-cli may shut us down manually.
socket.data('shutdown', function() {
log.info('Received shutdown message from ember-cli');
orchestrator.stop();
});
if (heartbeat) {
// Or, if it crashes or is killed it will stop sending the heartbeat
// and we can clean ourselves up.
socket.data('heartbeat', function() {
log.trace('Received a heartbeat from ember-cli');
that.stopLater();
});
}
socket.data('subscribeReady', async function() {
await ready;
socket.send('ready');
});
if (heartbeat) {
// We want to time out even if we never hear the initial heartbeat
that.stopLater();
}
});
this._server.listen(6785);
log.info('Listening for connections from ember-cli...');
}
_stop() {
log.info('No heartbeat from ember-cli! Shutting down.');
this.orchestrator.stop();
}
};