-
Notifications
You must be signed in to change notification settings - Fork 27
/
node-app.js
86 lines (75 loc) · 1.86 KB
/
node-app.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
const App = require("./app");
const path = require("path");
const fse = require("fs-extra");
const os = require("os");
const utils = require("./lib/utils");
class NodeApp extends App{
constructor(options={}){
super(options);
this.args = utils.args();
}
async main() {
if(!this.dataFolder)
return Promise.resolve();
const argv = process.argv.slice(2);
if(this.args.purge || this.args.reset || argv.includes('reset') || argv.includes('purge')) {
try {
console.log('purging'.brightMagenta,this.dataFolder.brightWhite);
await fse.remove(this.dataFolder);
await fse.remove(path.join(os.homedir(),'.kdx'));
console.log('done'.brightGreen);
process.exit(0);
} catch(err) {
console.log(err.toString().brightRed);
process.exit(0);
}
return;
}
const KaspaProcessManager = require(path.join(this.appFolder, "lib/manager.js"));
this.manager = new KaspaProcessManager(null, this.dataFolder, this.appFolder);
return new Promise((resolve, reject) => {
this.initDaemons();
resolve();
})
}
/**
* initlizing data folder error handler
*/
dataDirInitError(){
console.error(`Please start app with --init=/path/to/data/dir or --init for default (~/.kdx/data)`.red);
this.exit();
}
initDaemons(){
let modules = this.getModulesConfig();
console.log("initDaemons", modules)
this.startDaemons(modules);
}
startDaemons(daemons={}){
this.daemons = daemons;
this.manager.start(daemons);
}
async stopDaemons(){
try{
await this.manager.stop();
}catch(e){
console.log("manager.stop:error", e)
return false;
}
return true;
}
async restartDaemons(){
try{
await this.manager.stop();
console.log("initDaemons....")
dpc(1000, ()=>{
this.initDaemons();
});
}catch(e){
console.log("restartDaemons:error", e)
dpc(1000, ()=>{
this.initDaemons();
});
}
}
}
module.exports = NodeApp;