-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
47 lines (44 loc) · 1.13 KB
/
db.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
'use strict'
const kaf = require('kafjs')
const kc = require('./kafclient.js')
const loc = require('./loc.js')
const util = require('./util.js')
/* way/
* ensure that the database directory exists then start the kaf db
*/
function start(log, cb) {
util.ensureExists(loc.db(), err => {
if(err) cb(err)
else startKaf(log, cb)
})
}
/* problem/
* We support multiple instances of the desktop avatar. At the same
* time, we don't want them stomping over each other's data.
*
* way/
* Each avatar attempts to start up a database (actually an event store)
* on a standard port. If it succeeds, it serves the rest of the
* instances (including itself). If it fails, it goes to sleep and tries
* again after sometime - just in case the existing instance is shut
* down.
*/
function startKaf(log, cb) {
kaf.startServer(kc.PORT, loc.db(), err => {
if(err) {
if(err.code === "EADDRINUSE") {
setTimeout(start, 5 * 1000)
cb && cb()
} else {
if(cb) cb(err)
else console.error(err)
}
} else {
log("db/started")
cb && cb()
}
})
}
module.exports = {
start,
}