-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbase.js
More file actions
97 lines (82 loc) · 1.93 KB
/
base.js
File metadata and controls
97 lines (82 loc) · 1.93 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
'use strict'
const EventEmitter = require('events')
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const async = require('async')
class Facility extends EventEmitter {
constructor (caller, opts, ctx) {
super()
this.name = 'facility'
this.caller = caller
this.opts = _.extend({ ns: this.name }, opts)
this.ctx = ctx
}
_getConfPath () {
const cal = this.caller
const fprefix = this.ctx.env
const dirname = path.join(cal.ctx.root, 'config', 'facs')
let confPath = path.join(dirname, `${this.name}.config.json`)
const envConfPath = path.join(dirname, `${fprefix}.${this.name}.config.json`)
if (fprefix && fs.existsSync(envConfPath)) {
confPath = envConfPath
}
return confPath
}
init () {
if (this._hasConf) {
const confPath = this._getConfPath()
const conf = JSON.parse(fs.readFileSync(confPath, 'utf8'))
this.conf = conf[this.opts.ns]
}
}
set (k, v) {
this[k] = v
}
start (cb) {
async.series([
next => {
this._start0(next)
},
next => {
this.active = 1
next()
},
next => {
this._start(next)
},
next => {
const start = this.opts.start
if (!start) return next()
if (typeof start !== 'function') {
return next(new Error('opts.start must be of type function'))
}
start(this, next)
}
], cb)
}
_start0 (cb) { cb() }
_start (cb) { cb() }
stop (cb) {
async.series([
next => {
this._stop(next)
},
next => {
this.active = 0
if (!this.working) return next()
const itv = setInterval(() => {
if (this.working) return
clearInterval(itv)
next()
}, 1000)
},
next => {
this._stop9(next)
}
], cb)
}
_stop (cb) { cb() }
_stop9 (cb) { cb() }
}
module.exports = Facility