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

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

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

View File

@ -4,19 +4,24 @@
var wsses = {}; var wsses = {};
function createApp(server, options) { function createApp(server, options) {
console.log('Create App');
if (wsses[options.filename]) { if (wsses[options.filename]) {
return Promise.resolve(wsses[options.filename]); return Promise.resolve(wsses[options.filename]);
} }
return require('./wrapper').create(options).then(function (db) { return require('./wrapper').create(options).then(function (db) {
var url = require('url'); var url = require('url');
var express = require('express'); //var express = require('express');
var app = express(); //var app = express();
var wss = server.wss; var wss = server.wss;
function app(req, res) {
res.end('NOT IMPLEMENTED');
}
wss.on('connection', function (ws) { wss.on('connection', function (ws) {
var location = url.parse(ws.upgradeReq.url, true); var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions // you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312 // 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.__session_id = location.query.session_id || Math.random();
ws.on('message', function (buffer) { ws.on('message', function (buffer) {
console.log('[SERVER MESSAGE]', buffer);
var cmd; var cmd;
try { try {
@ -57,7 +61,8 @@ function createApp(server, options) {
break; break;
default: default:
break; throw new Error('UNKNOWN TYPE');
//break;
} }
}); });
@ -73,29 +78,29 @@ function createApp(server, options) {
} }
function create(options) { function create(options) {
console.log('Create Server');
return new Promise(function (resolve) {
var server = require('http').createServer(); var server = require('http').createServer();
var WebSocketServer = require('ws').Server; var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server }); var wss = new WebSocketServer({ server: server });
//var port = process.env.PORT || process.argv[0] || 4080; //var port = process.env.PORT || process.argv[0] || 4080;
console.log('options.sock');
console.log(options.sock);
var fs = require('fs'); var fs = require('fs');
var ps = [];
ps.push(new Promise(function (resolve) {
fs.unlink(options.sock, function () { fs.unlink(options.sock, function () {
// ignore error when socket doesn't exist // ignore error when socket doesn't exist
server.listen(options.sock, function () { server.listen(options.sock, resolve);
console.log('Listening');
});
}); });
}));
createApp({ server: server, wss: wss }, options).then(function (app) { ps.push(createApp({ server: server, wss: wss }, options).then(function (app) {
server.on('request', 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'; 'use strict';
var cluster = require('cluster'); var cluster = require('cluster');
//var numCores = 2;
var numCores = require('os').cpus().length; var numCores = require('os').cpus().length;
var i; var i;
@ -16,7 +17,7 @@ function run() {
, serve: null , serve: null
, connect: null , connect: null
}).then(function (client) { }).then(function (client) {
client.run("SELECT 1", [], function (err) { client.get("SELECT ?", ['Hello World!'], function (err, result) {
if (err) { if (err) {
console.error('[ERROR]', cluster.isMaster && '0' || cluster.worker.id); console.error('[ERROR]', cluster.isMaster && '0' || cluster.worker.id);
console.error(err); console.error(err);
@ -25,14 +26,29 @@ function run() {
console.log('[this]', cluster.isMaster && '0' || cluster.worker.id); console.log('[this]', cluster.isMaster && '0' || cluster.worker.id);
console.log(this); console.log(this);
console.log('[result]', cluster.isMaster && '0' || cluster.worker.id);
console.log(result);
}); });
}); });
} }
if (cluster.isMaster) { 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) { for (i = 1; i <= numCores; i += 1) {
cluster.fork(); 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 , serve: null
, connect: null , connect: null
}).then(function (client) { }).then(function (client) {
client.run("SELECT 1", [], function (err) { client.all("SELECT ?", ['Hello World!'], function (err, result) {
if (err) { if (err) {
console.error('[ERROR] standalone'); console.error('[ERROR] standalone');
console.error(err); console.error(err);
@ -21,8 +21,20 @@ function run() {
console.log('[this] standalone'); console.log('[this] standalone');
console.log(this); console.log(this);
console.log('[result] standalone');
console.log(result);
}); });
}); });
} }
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);
});