2015-11-06 11:05:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var cluster = require('cluster');
|
|
|
|
var PromiseA = require('bluebird');
|
2017-05-05 05:09:56 +00:00
|
|
|
var path = require('path');
|
|
|
|
var os = require('os');
|
2015-11-06 11:05:32 +00:00
|
|
|
|
2016-03-31 06:39:15 +00:00
|
|
|
function init(conf, state) {
|
2016-04-09 23:14:00 +00:00
|
|
|
var newConf = {};
|
2017-05-05 05:09:56 +00:00
|
|
|
|
|
|
|
function rand(n) {
|
|
|
|
var HEX = 16;
|
|
|
|
var BASE_36 = 36;
|
|
|
|
var rnd = require('crypto').randomBytes(n || 16).toString('hex');
|
|
|
|
return parseInt(rnd, HEX).toString(BASE_36);
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:05:32 +00:00
|
|
|
if (!conf.ipcKey) {
|
2017-05-05 05:09:56 +00:00
|
|
|
conf.ipcKey = newConf.ipcKey = rand(16);
|
2015-11-06 11:05:32 +00:00
|
|
|
}
|
2015-11-12 11:14:59 +00:00
|
|
|
if (!conf.sqlite3Sock) {
|
2017-05-05 05:09:56 +00:00
|
|
|
conf.sqlite3Sock = newConf.sqlite3Sock = path.join(os.tmpdir(), 'sqlite3.' + rand(8) + '.sock');
|
2015-11-12 11:14:59 +00:00
|
|
|
}
|
|
|
|
if (!conf.memstoreSock) {
|
2017-05-05 05:09:56 +00:00
|
|
|
conf.memstoreSock = newConf.memstoreSock = path.join(os.tmpdir(), 'memstore.' + rand(8) + '.sock');
|
2015-11-12 11:14:59 +00:00
|
|
|
}
|
2015-11-06 11:05:32 +00:00
|
|
|
|
|
|
|
try {
|
2015-11-12 11:14:59 +00:00
|
|
|
require('fs').unlinkSync(conf.memstoreSock);
|
|
|
|
} catch(e) {
|
|
|
|
if ('ENOENT' !== e.code) {
|
|
|
|
console.error(e.stack);
|
|
|
|
console.error(JSON.stringify(e));
|
|
|
|
}
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
require('fs').unlinkSync(conf.sqlite3Sock);
|
2015-11-06 11:05:32 +00:00
|
|
|
} catch(e) {
|
|
|
|
if ('ENOENT' !== e.code) {
|
|
|
|
console.error(e.stack);
|
|
|
|
console.error(JSON.stringify(e));
|
|
|
|
}
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
var cstore = require('cluster-store');
|
2015-11-12 11:14:59 +00:00
|
|
|
var sqlite3 = require('sqlite3-cluster/server');
|
2017-05-10 23:26:25 +00:00
|
|
|
var cstoreOpts = {
|
|
|
|
sock: conf.memstoreSock
|
|
|
|
, serve: cluster.isMaster && conf.memstoreSock
|
|
|
|
, store: cluster.isMaster && null //new require('express-session/session/memory')()
|
|
|
|
// TODO implement
|
|
|
|
, key: conf.ipcKey
|
|
|
|
};
|
|
|
|
var cstorePromise = cstore.create(cstoreOpts);
|
2015-11-12 11:14:59 +00:00
|
|
|
var promise = PromiseA.all([
|
2017-05-10 23:26:25 +00:00
|
|
|
cstorePromise.then(function (store) {
|
|
|
|
console.log('[walnut] [master] cstore created');
|
|
|
|
//console.log(cstoreOpts);
|
|
|
|
//console.log(store);
|
|
|
|
return store;
|
2015-11-12 11:14:59 +00:00
|
|
|
})
|
|
|
|
, sqlite3.createServer({
|
|
|
|
verbose: null
|
2015-11-28 05:06:19 +00:00
|
|
|
, sock: conf.sqlite3Sock
|
2015-11-12 11:14:59 +00:00
|
|
|
, ipcKey: conf.ipcKey
|
2016-04-09 23:14:00 +00:00
|
|
|
})/*.then(function () {
|
|
|
|
var sqlite3 = require('sqlite3-cluster/client');
|
|
|
|
return sqliet3.createClientFactory(...);
|
|
|
|
})*/
|
2016-03-31 06:39:15 +00:00
|
|
|
]).then(function (args) {
|
|
|
|
state.memstore = args[0];
|
2016-04-09 23:14:00 +00:00
|
|
|
//state.sqlstore = args[1];
|
2017-05-10 23:26:25 +00:00
|
|
|
newConf.addWorker = function (w) {
|
|
|
|
return cstorePromise.addWorker(w);
|
|
|
|
};
|
2016-04-09 23:14:00 +00:00
|
|
|
return newConf;
|
2015-11-06 11:05:32 +00:00
|
|
|
});
|
|
|
|
|
2015-11-12 11:14:59 +00:00
|
|
|
return promise;
|
2015-11-06 11:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function touch(conf, state) {
|
|
|
|
if (!state.initialize) {
|
|
|
|
state.initialize = init(conf, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO if no xyz worker, start on xyz worker (unlock, for example)
|
2016-04-09 23:14:00 +00:00
|
|
|
return state.initialize.then(function (newConf) {
|
2015-11-06 11:05:32 +00:00
|
|
|
// TODO conf.locked = true|false;
|
|
|
|
conf.initialized = true;
|
2016-04-09 23:14:00 +00:00
|
|
|
return newConf;
|
2015-11-06 11:05:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.init = init;
|
|
|
|
module.exports.touch = touch;
|