-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
71 lines (57 loc) · 1.61 KB
/
index.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
'use strict';
//Executes the logical flow of backups in a procedural fashion (using async/await to format the code nicely)
let Runner = require('./lib/Runner');
let runner = new Runner(process.argv);
let execute = async () => {
try {
await runner.validateFlags();
runner.configureRsync();
await runner.configureLogger();
await runner.executePrepare();
await runner.executePreHooks();
runner.executeRsync();
await runner.configureCallbacks();
} catch(e){
mainProcessError(e);
}
};
function quit (code) { //Handle killing rsync process
let errMsg = code === 0 ? '' : `Process Received Exit Code: ${code}`;
let err = new Error(errMsg);
err.code = code;
delete err.stack;
mainProcessError(err);
}
async function mainProcessError(err){
if(err.code !== 0) {
let logged = false;
try {
if(runner.logger) {
await runner.logger.logger.log('stderr')(err);
await runner.logger.logStateChange(`${runner.backupStr} Failed`);
logged = true;
}
} catch (newErr) {
console.error('Error logging with logger:', newErr);
}
if(!logged) {
if(err.stack)
console.error(err.stack);
else
console.error(err.toString());
}
}
runner.killRsync();
//Exit with specified code or 99
if(err.code !== undefined)
process.exit(err.code);
else
process.exit(99);
}
//Reasons to exit (gracefully or not)
process.on('SIGINT', quit);
process.on('SIGTERM', quit);
process.on('unhandledRejection', mainProcessError);
process.on('uncaughtException', mainProcessError);
//Execute Backup
execute().catch(mainProcessError);