-
Notifications
You must be signed in to change notification settings - Fork 0
Clusters
Efra Espada edited this page Jun 16, 2018
·
3 revisions
Turbine is a single process that manages a JSON database for you. It allows to work with the same data on different clusters or processes avoiding fatal errors writing on database. It has been designed for Rotor server but can be used as a query engine.
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const express = require('express');
const bodyParser = require('body-parser');
const timeout = require('connect-timeout');
const Turbine = require('@efraespada/turbine');
let turbine = new Turbine({
"turbine_port": 4004,
"turbine_ip": "http://localhost",
"db_name": "database",
"debug": true
});
let port = 3003;
if (cluster.isMaster) {
turbine.server();
let workers = [];
let spawn = function (i) {
workers[i] = cluster.fork();
workers[i].on('exit', function (code, signal) {
console.log('respawning worker ' + i);
spawn(i);
});
};
for (let i = 0; i < numCPUs; i++) {
spawn(i);
}
} else {
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({limit: '50mb'}));
app.use(timeout('120s'));
app.route('/')
// turbine server has been started, start managing data
.get(async function (req, res) {
if (req.query.query !== undefined) {
let object = await turbine.query(req.query.path, req.query.query);
res.json(object);
} else {
let object = await turbine.get(req.query.path);
res.json(object);
}
})
.post(async function (req, res) {
await turbine.post(req.body.path, req.body.content);
res.json({});
});
app.listen(port, function () {
console.log("cluster started on port " + port
+ " | worker => " + cluster.worker.id);
});
}
Notice server()
method is called on master and client methods are used on slaves.
Small databases (probably your dev environment) load in seconds but big databases will take much more time. Consider control if databases are ready before start processing requests.
Copyright 2018 Efraín Espada
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.