remove cruft, mostly

This commit is contained in:
AJ ONeal 2015-07-24 16:11:10 -06:00
parent 89e957af0f
commit 1edb1de382
5 changed files with 71 additions and 48 deletions

View File

@ -26,25 +26,17 @@ function startServer(opts) {
function getConnection(opts) {
return new Promise(function (resolve) {
setTimeout(function () {
//setTimeout(function () {
var WebSocket = require('ws');
var ws = new WebSocket('ws+unix:' + opts.sock);
if (opts.serve) {
console.log("[EXPLICIT SERVER] #################################################");
return startServer(opts).then(function (client) {
// ws.masterClient = client;
resolve({ masterClient: client });
});
}
ws.on('error', function (err) {
console.error('[ERROR] ws connection failed, retrying');
console.error(err);
function retry() {
setTimeout(function () {
getConnection(opts).then(resolve);
getConnection(opts).then(resolve, retry);
}, 100 + (Math.random() * 250));
}
@ -64,8 +56,8 @@ function getConnection(opts) {
ws.on('open', function () {
resolve(ws);
});
//}, 100 + (Math.random() * 250));
});
}, 100 + (Math.random() * 250));
}
function create(opts) {
@ -83,13 +75,14 @@ function create(opts) {
return startServer(opts).then(function (client) {
// ws.masterClient = client;
return { masterClient: client };
}, function () {
}, function (err) {
console.error('[ERROR] retryServe()');
console.error(err);
retryServe();
});
}
if (opts.serve) {
console.log('[EXPLICIT]');
promise = retryServe();
} else {
promise = getConnection(opts);
@ -102,7 +95,6 @@ function create(opts) {
// TODO maybe use HTTP POST instead?
return promise.then(function (ws) {
if (ws.masterClient) {
console.log('[MASTER CLIENT] found');
return ws.masterClient;
}
@ -119,9 +111,6 @@ function create(opts) {
cb = args.pop();
}
console.log('fname, args');
console.log(fname, args);
ws.send(JSON.stringify({
type: 'rpc'
, func: fname
@ -153,9 +142,6 @@ function create(opts) {
return;
}
//console.log('onMessage data');
//console.log(cmd);
cb.apply(cmd.this, cmd.args);
if ('on' !== fname) {
@ -185,6 +171,8 @@ function create(opts) {
try {
fn(data);
} catch(e) {
console.error("[ERROR] ws.on('message', fn) (multi-callback)");
console.error(e);
// ignore
}
});

2
install-sqlcipher.bash Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/bin/bash
#brew options sqlcipher
#brew install sqlcipher --with-fts
echo STOP

View File

@ -4,19 +4,24 @@
var wsses = {};
function createApp(server, options) {
console.log('Create App');
if (wsses[options.filename]) {
return Promise.resolve(wsses[options.filename]);
}
return require('./wrapper').create(options).then(function (db) {
var url = require('url');
var express = require('express');
var app = express();
//var express = require('express');
//var app = express();
var wss = server.wss;
function app(req, res) {
res.end('NOT IMPLEMENTED');
}
wss.on('connection', function (ws) {
var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312
@ -24,7 +29,6 @@ function createApp(server, options) {
ws.__session_id = location.query.session_id || Math.random();
ws.on('message', function (buffer) {
console.log('[SERVER MESSAGE]', buffer);
var cmd;
try {
@ -57,7 +61,8 @@ function createApp(server, options) {
break;
default:
break;
throw new Error('UNKNOWN TYPE');
//break;
}
});
@ -73,29 +78,29 @@ function createApp(server, options) {
}
function create(options) {
console.log('Create Server');
return new Promise(function (resolve) {
var server = require('http').createServer();
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server });
//var port = process.env.PORT || process.argv[0] || 4080;
console.log('options.sock');
console.log(options.sock);
var fs = require('fs');
var ps = [];
ps.push(new Promise(function (resolve) {
fs.unlink(options.sock, function () {
// ignore error when socket doesn't exist
server.listen(options.sock, function () {
console.log('Listening');
});
server.listen(options.sock, resolve);
});
}));
createApp({ server: server, wss: wss }, options).then(function (app) {
ps.push(createApp({ server: server, wss: wss }, options).then(function (app) {
server.on('request', app);
resolve({ masterClient: app.masterClient });
});
return { masterClient: app.masterClient };
}));
return Promise.all(ps).then(function (results) {
return results[1];
});
}

View File

@ -1,6 +1,7 @@
'use strict';
var cluster = require('cluster');
//var numCores = 2;
var numCores = require('os').cpus().length;
var i;
@ -16,7 +17,7 @@ function run() {
, serve: null
, connect: null
}).then(function (client) {
client.run("SELECT 1", [], function (err) {
client.get("SELECT ?", ['Hello World!'], function (err, result) {
if (err) {
console.error('[ERROR]', cluster.isMaster && '0' || cluster.worker.id);
console.error(err);
@ -25,14 +26,29 @@ function run() {
console.log('[this]', cluster.isMaster && '0' || cluster.worker.id);
console.log(this);
console.log('[result]', cluster.isMaster && '0' || cluster.worker.id);
console.log(result);
});
});
}
if (cluster.isMaster) {
// not a bad idea to setup the master before forking the workers
run().then(function () {
for (i = 1; i <= numCores; i += 1) {
cluster.fork();
}
});
} else {
run();
}
run();
// The native Promise implementation ignores errors because... dumbness???
process.on('unhandledPromiseRejection', function (err) {
console.error('Unhandled Promise Rejection');
console.error(err);
console.error(err.stack);
process.exit(1);
});

View File

@ -12,7 +12,7 @@ function run() {
, serve: null
, connect: null
}).then(function (client) {
client.run("SELECT 1", [], function (err) {
client.all("SELECT ?", ['Hello World!'], function (err, result) {
if (err) {
console.error('[ERROR] standalone');
console.error(err);
@ -21,8 +21,20 @@ function run() {
console.log('[this] standalone');
console.log(this);
console.log('[result] standalone');
console.log(result);
});
});
}
run();
// The native Promise implementation ignores errors because... dumbness???
process.on('unhandledPromiseRejection', function (err) {
console.error('Unhandled Promise Rejection');
console.error(err);
console.error(err.stack);
process.exit(1);
});