提交 3d4b01de authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Check settings.

上级 9a2d8e14
...@@ -12,9 +12,27 @@ debugerror.log = console.error.bind(console); ...@@ -12,9 +12,27 @@ debugerror.log = console.error.bind(console);
const check = require('check-types'); const check = require('check-types');
const randomString = require('random-string'); const randomString = require('random-string');
const merge = require('merge');
const WORKER_BIN_PATH = path.join(__dirname, '..', '..', 'mediasoup-worker', 'bin', 'mediasoup-worker');
const DEFAULT_NUM_WORKERS = Object.keys(os.cpus()).length; const DEFAULT_NUM_WORKERS = Object.keys(os.cpus()).length;
const MEDIASOUP_WORKER_BIN_PATH = path.join(__dirname, '..', '..', 'mediasoup-worker', 'bin', 'mediasoup-worker'); const DEFAULT_OPTIONS =
{
logLevel : 'info',
useSyslog : false,
syslogFacility : 'user',
rtcListenIPv4 : true,
rtcListenIPv6 : true,
rtcMinPort : 10000,
rtcMaxPort : 59999,
dtlsCertificateFile : null,
dtlsPrivateKeyFile : null
};
const VALID_OPTIONS =
{
logLevel : [ 'debug', 'info', 'notice', 'warn', 'error' ],
syslogFacility : [ 'user', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7' ]
};
var servers = new Set(); var servers = new Set();
...@@ -33,29 +51,64 @@ class Server ...@@ -33,29 +51,64 @@ class Server
debug('constructor() [options:%o]', options); debug('constructor() [options:%o]', options);
let serverId = randomString({ numeric: false, length: 6 }).toLowerCase(); let serverId = randomString({ numeric: false, length: 6 }).toLowerCase();
let numWorkers; let numWorkers = DEFAULT_NUM_WORKERS;
options = options || {}; options = merge(true, DEFAULT_OPTIONS, options);
if (check.integer(options.numWorkers) && check.positive(options.numWorkers)) if (check.integer(options.numWorkers) && check.positive(options.numWorkers))
{ {
numWorkers = options.numWorkers; numWorkers = options.numWorkers;
} }
else
if (!check.includes(VALID_OPTIONS.logLevel, options.logLevel))
{
throw new Error(`invalid option value [logLevel:${options.logLevel}]`);
}
if (!check.boolean(options.useSyslog))
{
throw new Error(`invalid option value [useSyslog:${options.useSyslog}]`);
}
if (!check.includes(VALID_OPTIONS.syslogFacility, options.syslogFacility))
{
throw new Error(`invalid option value [syslogFacility:${options.syslogFacility}]`);
}
if (!check.integer(options.rtcMinPort) || !check.positive(options.rtcMinPort))
{ {
numWorkers = DEFAULT_NUM_WORKERS; throw new Error(`invalid option value [rtcMinPort:${options.rtcMinPort}]`);
} }
debug('constructor() [serverId:%s, numWorkers:%d]', serverId, numWorkers); if (!check.integer(options.rtcMaxPort) || !check.positive(options.rtcMaxPort))
{
throw new Error(`invalid option value [rtcMaxPort:${options.rtcMaxPort}]`);
}
// mediasoup-worker child processes within this room // Map of mediasoup-worker child processes
this._workers = new Map(); this._workers = new Map();
// Create mediasoup worker child processes // Create mediasoup-worker child processes
for (let i = 1; i <= numWorkers; i++) for (let i = 1; i <= numWorkers; i++)
{ {
let workerId = serverId + '#' + i; let workerId = serverId + '#' + i;
let worker = spawn(MEDIASOUP_WORKER_BIN_PATH, [ workerId ]); let worker;
let workerOptions = merge(true, options);
let parameters = [];
for (let key of Object.keys(workerOptions))
{
if (DEFAULT_OPTIONS.hasOwnProperty(key))
{
let value = String(workerOptions[key]);
parameters.push(`--${key}=${value}`);
}
}
debug('constructor() creating worker [workerId:%s, parameters:"%s"]', workerId, parameters.join(' '));
worker = spawn(WORKER_BIN_PATH, [ workerId ].concat(parameters));
// Store worker id within the worker process // Store worker id within the worker process
worker.workerId = workerId; worker.workerId = workerId;
...@@ -82,14 +135,14 @@ class Server ...@@ -82,14 +135,14 @@ class Server
}); });
}); });
worker.on('close', (code) => worker.on('exit', (code) =>
{ {
debug('worker process closed [workerId:%s, code:%s]', worker.workerId, code); debug('worker process closed [workerId:%s, code:%s]', worker.workerId, code);
}); });
worker.on('error', (error) => worker.on('error', (error) =>
{ {
debugerror('worker process failed to start [workerId:%s]: %s', worker.workerId, error); debugerror('worker process error [workerId:%s]: %s', worker.workerId, error);
}); });
// Add the worker to the map // Add the worker to the map
...@@ -104,11 +157,17 @@ class Server ...@@ -104,11 +157,17 @@ class Server
{ {
debug('close()'); debug('close()');
// Remove this server from the set
servers.delete(this);
// Kill every worker process
for (let worker of this._workers.values()) for (let worker of this._workers.values())
{ {
// TODO: set proper signal worker.kill('SIGTERM');
worker.kill();
} }
// Clear the workers map
this._workers.clear();
} }
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
"bluebird": "^3.0.6", "bluebird": "^3.0.6",
"check-types": "^5.1.0", "check-types": "^5.1.0",
"debug": "^2.2.0", "debug": "^2.2.0",
"merge": "^1.2.0",
"random-string": "^0.1.2" "random-string": "^0.1.2"
}, },
"devDependencies": { "devDependencies": {
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论