climax
Installing
npm install climax
Configuration
Configuration of resources
Configuration of resources should be like the example below.
There are some configuration elements for basic connection these are;
- autostart : Connect to resource databases at startup
- type : resource database adapter name
- user : username for dbms
- password : password for dbms
- port : dbms specific port
- host : dbms host (IP/TCP is possible)
- database : database name
- max : maximum connection per delta request
- log : logging
- querylist : list of queries which is requested on database, number is for idle timeout of database
{
"serverlist": {
"mysqlserver": {
"autostart": true,
"type": "mysql",
"user": "root",
"password": "",
"port": 3306,
"host": "127.0.0.1",
"database": "climax",
"max": 10,
"log": true,
"querylist": {
"select * from testing": 20000,
"select * from vertexclique": 30000
}
},
"mongodbserver": {
"type": "mongodb",
"port": 27017,
"host": "172.10.1.32",
"log": false,
"querylist": {
"select * from vertexclique": 50000
}
}
}
}
Configuration of connections
mysql
For mysql connection you do not need to edit anything if you need to edit the frequency of update or other settings you can just implement the exports.looper
. You must define the error case with;
if (err) {
throw new Error('foobarbaz');
}
an example for the implementation is like that;
var looper = exports.looper = function(poolQueue){
try{
inx = 0;
while(true){
poolQueue.forEach(function(pool) {
pool.acquire(function(err, client) {
if (err) {
logger.error("Check your resource list. Error on connection try with another argument");
// or throw error
}
else {
if(pool.type == "mysql"){
logger.info("MYSQL");
client.query(pool.query, function (error, rows, fields) {
index=inx+'', inx+=1;
redisconnect(pool.type+index, JSON.stringify(rows));
pool.release(client);
});
}else if(pool.type == "mongodb"){
logger.info("MONGO");
//TODO: this
}else{
logger.info("FAILURE");
throw new Error("your adapter currently not included in climax");
}
}
slp.usleep(500);
});
});
}
}catch(e){
logger.error('looper exception: ' + e.message);
}
}
redis
For redis connection you can edit exports.redisconnect
These things gives you complete flexibility about key-object storing.
var redisconnect = exports.redisconnect = function(objkey, objstr) {
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.set(objkey, objstr);
}
autostart feature
If you want to auto connect to database adapter you must declare it in resource list. If this feature is enabled in resource list with true
climax will connect your resource database at the beginning.
Usage
Simple usage is like that
-
Firstly install with
npm install climax
then -
climax-test.js
var climax = require('climax'),
resourcelist = climax.reslist(__dirname+'/reslist.json');
poolQueue = climax.poolQueue(resourcelist);
climax.start(poolQueue);
- reslist.json
{
"serverlist": {
"mysqlserver": {
"autostart": true,
"type": "mysql",
"user": "root",
"password": "",
"port": 3306,
"host": "127.0.0.1",
"database": "climax",
"max": 10,
"log": true,
"querylist": {
"select * from testing": 20000,
"select * from vertexclique": 30000
}
},
"mongodbserver": {
"type": "mongodb",
"port": 27017,
"host": "172.10.1.32",
"log": false,
"querylist": {
"select * from vertexclique": 50000
}
}
}
}
Pitfalls
If your console has printing out incremented connections but no active connections like that
INFO pool mysqlserver1 - dispense() clients=605999 available=0
your one or more connections are not properly working. Check your adapters, resource list and reimplement connection functions if it is needed.
Proper response is something like this:
INFO pool mysqlserver1 - dispense() clients=24 available=4