forked from Unity-Technologies/unity-cache-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·152 lines (123 loc) · 5.2 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
const helpers = require('./lib/helpers');
helpers.initConfigDir(__dirname);
const config = require('config');
const { Server } = require('./lib');
const cluster = require('cluster');
const consts = require('./lib/constants');
const program = require('commander');
const ip = require('ip');
const VERSION = require('./package.json').version;
const fs = require('fs-extra');
const path = require('path');
function myParseInt(val, def) {
val = parseInt(val);
return (!val && val !== 0) ? def : val;
}
function zeroOrMore(val) {
return Math.max(0, val);
}
function collect(val, memo) {
memo.push(val);
return memo;
}
const defaultCacheModule = config.get("Cache.defaultModule");
program.description("Unity Cache Server")
.version(VERSION)
.allowUnknownOption(true)
.option('-p, --port <n>', 'Specify the server port, only apply to new cache server', myParseInt, consts.DEFAULT_PORT)
.option('-c --cache-module [path]', 'Use cache module at specified path', defaultCacheModule)
.option('-P, --cache-path [path]', 'Specify the path of the cache directory')
.option('-l, --log-level <n>', 'Specify the level of log verbosity. Valid values are 0 (silent) through 5 (debug)', myParseInt, consts.DEFAULT_LOG_LEVEL)
.option('-w, --workers <n>', 'Number of worker threads to spawn', zeroOrMore, consts.DEFAULT_WORKERS)
.option('-m --mirror [host:port]', 'Mirror transactions to another cache server. Can be repeated for multiple mirrors', collect, [])
.option('--dump-config', 'Write the active configuration to the console')
.option('--save-config [path]', 'Write the active configuration to the specified file and exit. Defaults to ./default.yml')
.option('--NODE_CONFIG_DIR=<path>', 'Specify the directory to search for config files. This is equivalent to setting the NODE_CONFIG_DIR environment variable. Without this option, the built-in configuration is used.');
program.parse(process.argv);
if(program.saveConfig || program.dumpConfig) {
const configs = config.util.getConfigSources();
const configData = configs.length > 0 ? configs[configs.length - 1].original : '';
if(program.dumpConfig) {
console.log(configData);
}
if(program.saveConfig) {
let configFile = (typeof(program.saveConfig) === 'boolean') ? 'default.yml' : program.saveConfig;
configFile = path.resolve(configFile);
if (fs.pathExistsSync(configFile)) {
helpers.log(consts.LOG_ERR, `${configFile} already exists - will not overwrite.`);
process.exit(1);
}
fs.ensureDirSync(path.dirname(configFile));
fs.writeFileSync(configFile, configData);
helpers.log(consts.LOG_INFO, `config saved to ${configFile}`);
}
process.exit(0);
}
helpers.setLogLevel(program.logLevel);
helpers.setLogger(program.workers > 0 ? helpers.defaultClusterLogger : helpers.defaultLogger);
const errHandler = function () {
helpers.log(consts.LOG_ERR, "Unable to start Cache Server");
process.exit(1);
};
const CacheModule = helpers.resolveCacheModule(program.cacheModule, __dirname);
const Cache = new CacheModule();
if(program.workers > 0 && !CacheModule.properties.clustering) {
program.workers = 0;
helpers.log(consts.LOG_INFO, `Clustering disabled, ${program.cacheModule} module does not support it.`);
}
let server = null;
const cacheOpts = {};
if(program.cachePath !== null) {
cacheOpts.cachePath = program.cachePath;
}
const getMirrors = () => new Promise((resolve, reject) => {
const defaultPort = consts.DEFAULT_PORT;
const myIp = ip.address();
const mirrors = program.mirror.map(async m => {
const result = await helpers.parseAndValidateAddressString(m, defaultPort);
if((ip.isEqual(myIp, result.host) || ip.isEqual("127.0.0.1", result.host)) && program.port === port) {
throw new Error(`Cannot mirror to self!`);
}
return result;
});
Promise.all(mirrors)
.then(m => resolve(m))
.catch(err => reject(err));
});
Cache.init(cacheOpts)
.then(() => getMirrors())
.then(mirrors => {
const opts = {
port: program.port,
mirror: mirrors
};
server = new Server(Cache, opts);
if(cluster.isMaster) {
helpers.log(consts.LOG_INFO, `Cache Server version ${VERSION}; Cache module is ${program.cacheModule}`);
if(program.workers === 0) {
server.start(errHandler).then(() => {
helpers.log(consts.LOG_INFO, `Cache Server ready on port ${server.port}`);
});
}
for(let i = 0; i < program.workers; i++) {
const worker = cluster.fork();
Cache.registerClusterWorker(worker);
}
}
else {
server.start(errHandler).then(() => {
helpers.log(consts.LOG_INFO, `Cache Server worker ${cluster.worker.id} ready on port ${server.port}`);
});
}
})
.catch(err => {
helpers.log(consts.LOG_ERR, err);
process.exit(1);
});
process.on('SIGINT', async () => {
helpers.log(consts.LOG_INFO, "Shutting down...");
await Cache.shutdown();
await server.stop();
process.exit(0);
});