/
LocalDaemon.js
115 lines (91 loc) · 3.06 KB
/
LocalDaemon.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
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
var util = require('util')
var Autowire = require('wantsit').Autowire
var async = require('async')
var DaemonConnection = require('../common/DaemonConnection')
var LocalDaemon = function () {
DaemonConnection.call(this)
this._localDaemonStarter = Autowire
this._localDaemonAdminConnection = Autowire
this._localDaemonUserConnection = Autowire
}
util.inherits(LocalDaemon, DaemonConnection)
LocalDaemon.prototype._connect = function (callback) {
if (this._localDaemonUserConnection.connected) {
this._logger.debug('Already connected to daemon, executing callback')
process.nextTick(callback.bind(callback, undefined, this))
return
}
// try to connect to daemon
this._localDaemonUserConnection.connect(this._api, function (error, serverApi) {
if (error) {
callback(error)
return
}
// expose server user methods
for (var key in serverApi) {
this[key] = serverApi[key]
}
// reset process and app lists
this._processStore.removeAll()
this._appStore.removeAll()
this._overrideProcessInfoMethods()
this._overrideAppMethods()
this._localDaemonAdminConnection.connect(this._api, function (error, serverApi) {
if (error) {
if (error.code === 'EACCES') {
// this user cannot connect to the admin socket
this._logger.debug('Access to admin socket denied')
return callback(undefined, this)
} else {
return callback(error)
}
}
// expose server admin methods
for (var key in serverApi) {
this[key] = serverApi[key]
}
return callback(undefined, this)
}.bind(this))
}.bind(this))
}
LocalDaemon.prototype.connectOrStart = function (callback) {
this.connect(function (error, daemon) {
if (error && error.code === 'DAEMON_NOT_RUNNING') {
this._logger.debug('Daemon was not running so will start it')
this._localDaemonStarter.start(function (error) {
if (error) {
return callback(error)
}
this._logger.debug('Daemon started')
this.connect(callback)
}.bind(this))
return
}
return callback(error, daemon)
}.bind(this))
}
LocalDaemon.prototype.disconnect = function (callback) {
async.parallel([
this._localDaemonStarter.disconnect.bind(this._localDaemonStarter),
this._localDaemonUserConnection.disconnect.bind(this._localDaemonUserConnection),
this._localDaemonAdminConnection.disconnect.bind(this._localDaemonAdminConnection)
], function (error) {
if (callback) {
callback(error)
}
})
}
LocalDaemon.prototype._connectToProcess = function (id, callback) {
if (!this._localDaemonUserConnection.connected) {
return callback(new Error('Not connected to remote daemon'))
}
this.findProcessInfoById(id, function (error, processInfo) {
if (error) return callback(error)
if (!processInfo) return callback()
if (!processInfo.socket) {
return callback(new Error('Process ' + processInfo.name + ' is not ready yet'))
}
processInfo.connect(callback)
})
}
module.exports = LocalDaemon