* Clean up door.py

* Implement trimMessageAreasScheduledEvent() method
* Misc cleanup
This commit is contained in:
Bryan Ashby 2016-06-20 14:10:12 -06:00
parent 6e6ebc9b18
commit f5a1a79777
3 changed files with 208 additions and 199 deletions

View File

@ -169,17 +169,19 @@ function createMessageBaseTables() {
'END;' 'END;'
); );
// :TODO: need SQL to ensure cleaned up if delete from message?
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS message_meta (' + 'CREATE TABLE IF NOT EXISTS message_meta (' +
' message_id INTEGER NOT NULL,' + ' message_id INTEGER NOT NULL,' +
' meta_category INTEGER NOT NULL,' + ' meta_category INTEGER NOT NULL,' +
' meta_name VARCHAR NOT NULL,' + ' meta_name VARCHAR NOT NULL,' +
' meta_value VARCHAR NOT NULL,' + ' meta_value VARCHAR NOT NULL,' +
' UNIQUE(message_id, meta_category, meta_name, meta_value),' + // why unique here? ' UNIQUE(message_id, meta_category, meta_name, meta_value),' + // :TODO:why unique here?
' FOREIGN KEY(message_id) REFERENCES message(message_id)' + ' FOREIGN KEY(message_id) REFERENCES message(message_id)' +
');' ');'
); );
// :TODO: need SQL to ensure cleaned up if delete from message?
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS hash_tag (' + 'CREATE TABLE IF NOT EXISTS hash_tag (' +
' hash_tag_id INTEGER PRIMARY KEY,' + ' hash_tag_id INTEGER PRIMARY KEY,' +
@ -188,6 +190,7 @@ function createMessageBaseTables() {
');' ');'
); );
// :TODO: need SQL to ensure cleaned up if delete from message?
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS message_hash_tag (' + 'CREATE TABLE IF NOT EXISTS message_hash_tag (' +
' hash_tag_id INTEGER NOT NULL,' + ' hash_tag_id INTEGER NOT NULL,' +

View File

@ -1,24 +1,24 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var spawn = require('child_process').spawn; const events = require('events');
var events = require('events');
var _ = require('lodash'); const _ = require('lodash');
var pty = require('ptyw.js'); const pty = require('ptyw.js');
var decode = require('iconv-lite').decode; const decode = require('iconv-lite').decode;
var net = require('net'); const createServer = require('net').createServer;
var async = require('async');
exports.Door = Door; exports.Door = Door;
function Door(client, exeInfo) { function Door(client, exeInfo) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
const self = this;
this.client = client; this.client = client;
this.exeInfo = exeInfo; this.exeInfo = exeInfo;
this.exeInfo.encoding = this.exeInfo.encoding || 'cp437'; this.exeInfo.encoding = this.exeInfo.encoding || 'cp437';
this.exeInfo.encoding = this.exeInfo.encoding.toLowerCase();
let restored = false;
// //
// Members of exeInfo: // Members of exeInfo:
@ -32,24 +32,16 @@ function Door(client, exeInfo) {
// node // node
// inhSocket // inhSocket
// //
}
require('util').inherits(Door, events.EventEmitter); this.doorDataHandler = function(data) {
if(self.client.term.outputEncoding === self.exeInfo.encoding) {
self.client.term.rawWrite(data);
} else {
Door.prototype.run = function() {
var self = this;
var doorData = function(data) {
// :TODO: skip decoding if we have a match, e.g. cp437 === cp437
self.client.term.write(decode(data, self.exeInfo.encoding)); self.client.term.write(decode(data, self.exeInfo.encoding));
}
}; };
var restored = false; this.restoreIo = function(piped) {
var restore = function(piped) {
if(!restored && self.client.term.output) { if(!restored && self.client.term.output) {
self.client.term.output.unpipe(piped); self.client.term.output.unpipe(piped);
self.client.term.output.resume(); self.client.term.output.resume();
@ -57,56 +49,63 @@ Door.prototype.run = function() {
} }
}; };
var sockServer; this.prepareSocketIoServer = function(cb) {
async.series(
[
function prepareServer(callback) {
if('socket' === self.exeInfo.io) { if('socket' === self.exeInfo.io) {
sockServer = net.createServer(function connected(conn) { const sockServer = createServer(conn => {
sockServer.getConnections(function connCount(err, count) { sockServer.getConnections( (err, count) => {
// We expect only one connection from our DOOR/emulator/etc. // We expect only one connection from our DOOR/emulator/etc.
if(!err && count <= 1) { if(!err && count <= 1) {
self.client.term.output.pipe(conn); self.client.term.output.pipe(conn);
conn.on('data', doorData); conn.on('data', self.doorDataHandler);
conn.once('end', function ended() { conn.once('end', () => {
restore(conn); return self.restoreIo(conn);
}); });
conn.once('error', function error(err) { conn.once('error', err => {
self.client.log.info('Door socket server connection error: ' + err.message); self.client.log.info( { error : err.toString() }, 'Door socket server connection');
restore(conn); return self.restoreIo(conn);
}); });
} }
}); });
}); });
sockServer.listen(0, function listening() { sockServer.listen(0, () => {
callback(null); return cb(null, sockServer);
}); });
} else { } else {
callback(null); return cb(null);
}
};
} }
},
function launch(callback) {
// Expand arg strings, e.g. {dropFile} -> DOOR32.SYS
var args = _.clone(self.exeInfo.args); // we need a copy so the original is not modified
for(var i = 0; i < args.length; ++i) { require('util').inherits(Door, events.EventEmitter);
Door.prototype.run = function() {
const self = this;
this.prepareSocketIoServer( (err, sockServer) => {
if(err) {
this.client.log.warn( { error : err.toString() }, 'Failed executing door');
return self.emit('finished');
}
// Expand arg strings, e.g. {dropFile} -> DOOR32.SYS
let args = _.clone(self.exeInfo.args); // we need a copy so the original is not modified
for(let i = 0; i < args.length; ++i) {
args[i] = self.exeInfo.args[i].format({ args[i] = self.exeInfo.args[i].format({
dropFile : self.exeInfo.dropFile, dropFile : self.exeInfo.dropFile,
node : self.exeInfo.node.toString(), node : self.exeInfo.node.toString(),
//inhSocket : self.exeInfo.inhSocket.toString(),
srvPort : sockServer ? sockServer.address().port.toString() : '-1', srvPort : sockServer ? sockServer.address().port.toString() : '-1',
userId : self.client.user.userId.toString(), userId : self.client.user.userId.toString(),
}); });
} }
var door = pty.spawn(self.exeInfo.cmd, args, { const door = pty.spawn(self.exeInfo.cmd, args, {
cols : self.client.term.termWidth, cols : self.client.term.termWidth,
rows : self.client.term.termHeight, rows : self.client.term.termHeight,
// :TODO: cwd // :TODO: cwd
@ -118,19 +117,17 @@ Door.prototype.run = function() {
self.client.term.output.pipe(door); self.client.term.output.pipe(door);
door.on('data', doorData); door.on('data', self.doorDataHandler);
door.once('close', function closed() { door.once('close', () => {
restore(door); return self.restoreIo(door);
}); });
} else if('socket' === self.exeInfo.io) { } else if('socket' === self.exeInfo.io) {
self.client.log.debug( self.client.log.debug( { port : sockServer.address().port }, 'Using temporary socket server for door I/O');
{ port : sockServer.address().port },
'Using temporary socket server for door I/O');
} }
door.once('exit', function exited(code) { door.once('exit', exitCode => {
self.client.log.info( { code : code }, 'Door exited'); self.client.log.info( { exitCode : exitCode }, 'Door exited');
if(sockServer) { if(sockServer) {
sockServer.close(); sockServer.close();
@ -138,19 +135,12 @@ Door.prototype.run = function() {
// we may not get a close // we may not get a close
if('stdio' === self.exeInfo.io) { if('stdio' === self.exeInfo.io) {
restore(door); return self.restoreIo(door);
} }
door.removeAllListeners(); door.removeAllListeners();
self.emit('finished'); self.emit('finished');
}); });
} });
],
function complete(err) {
if(err) {
self.client.log.warn( { error : err.toString() }, 'Failed executing door');
}
}
);
}; };

View File

@ -1,16 +1,17 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
let msgDb = require('./database.js').dbs.message; const msgDb = require('./database.js').dbs.message;
let Config = require('./config.js').config; const Config = require('./config.js').config;
let Message = require('./message.js'); const Message = require('./message.js');
let Log = require('./logger.js').log; const Log = require('./logger.js').log;
let checkAcs = require('./acs_util.js').checkAcs; const checkAcs = require('./acs_util.js').checkAcs;
let msgNetRecord = require('./msg_network.js').recordMessage; const msgNetRecord = require('./msg_network.js').recordMessage;
let async = require('async'); const async = require('async');
let _ = require('lodash'); const _ = require('lodash');
let assert = require('assert'); const assert = require('assert');
const moment = require('moment');
exports.getAvailableMessageConferences = getAvailableMessageConferences; exports.getAvailableMessageConferences = getAvailableMessageConferences;
exports.getSortedAvailMessageConferences = getSortedAvailMessageConferences; exports.getSortedAvailMessageConferences = getSortedAvailMessageConferences;
@ -466,65 +467,58 @@ function persistMessage(message, cb) {
); );
} }
function trimMessagesToMax(areaTag, maxMessages, archivePath, cb) { // method exposed for event scheduler
async.waterfall( function trimMessageAreasScheduledEvent(args, cb) {
[
function getRemoteCount(callback) { function trimMessageAreaByMaxMessages(areaInfo, cb) {
let removeCount = 0; if(0 === areaInfo.maxMessages) {
msgDb.get( return cb(null);
`SELECT COUNT(area_tag) AS msgCount }
msgDb.run(
`DELETE FROM message
WHERE message_id IN
(SELECT message_id
FROM message FROM message
WHERE area_tag = ?`, WHERE area_tag = ?
[ areaTag ], ORDER BY message_id
(err, row) => { LIMIT (MAX(0, (SELECT COUNT()
if(!err) { FROM message
if(row.msgCount >= maxMessages) { WHERE area_tag = ?) - ${areaInfo.maxMessages}
removeCount = row.msgCount - maxMessages; ))
} );`,
} [ areaInfo.areaTag, areaInfo.areaTag],
return callback(err, removeCount);
}
);
},
function trimMessages(removeCount, callback) {
if(0 === removeCount) {
return callback(null);
}
if(archivePath) {
} else {
// just delete 'em
}
}
],
err => { err => {
if(err) {
Log.warn( { areaInfo : areaInfo, error : err.toString(), type : 'maxMessages' }, 'Error trimming message area');
} else {
Log.debug( { areaInfo : areaInfo, type : 'maxMessages' }, 'Area trimmed successfully');
}
return cb(err); return cb(err);
} }
); );
} }
// method exposed for event scheduler function trimMessageAreaByMaxAgeDays(areaInfo, cb) {
function trimMessageAreasScheduledEvent(args, cb) { if(0 === areaInfo.maxAgeDays) {
// return cb(null);
// Available args: }
// - archive:/path/to/archive/dir/
// msgDb.run(
let archivePath; `DELETE FROM message
if(args) { WHERE area_tag = ? AND modified_timestamp < date('now', '-${areaInfo.maxAgeDays} days');`,
args.forEach(a => { [ areaInfo.areaTag ],
if(a.startsWith('archive:')) { err => {
archivePath = a.split(':')[1]; if(err) {
} Log.warn( { areaInfo : areaInfo, error : err.toString(), type : 'maxAgeDays' }, 'Error trimming message area');
}); } else {
Log.debug( { areaInfo : areaInfo, type : 'maxAgeDays' }, 'Area trimmed successfully');
}
return cb(err);
}
);
} }
//
// Find all area_tag's in message. We don't rely on user configurations
// in case one is no longer available. From there we can trim messages
// that meet the criteria (too old, too many, ...) and optionally archive
// them via moving them to a new DB with the same layout
//
async.waterfall( async.waterfall(
[ [
function getAreaTags(callback) { function getAreaTags(callback) {
@ -543,13 +537,16 @@ function trimMessageAreasScheduledEvent(args, cb) {
} }
); );
}, },
function trimAreas(areaTags, callback) { function prepareAreaInfo(areaTags, callback) {
let areaInfos = [];
// determine maxMessages & maxAgeDays per area
areaTags.forEach(areaTag => { areaTags.forEach(areaTag => {
let maxMessages = Config.messageAreaDefaults.maxMessages; let maxMessages = Config.messageAreaDefaults.maxMessages;
let maxAgeDays = Config.messageAreaDefaults.maxAgeDays; let maxAgeDays = Config.messageAreaDefaults.maxAgeDays;
const area = getMessageAreaByTag(areaTag); // note: we don't know the conf const area = getMessageAreaByTag(areaTag); // note: we don't know the conf here
if(area) { if(area) {
if(area.maxMessages) { if(area.maxMessages) {
maxMessages = area.maxMessages; maxMessages = area.maxMessages;
@ -559,17 +556,36 @@ function trimMessageAreasScheduledEvent(args, cb) {
} }
} }
if(maxMessages) { areaInfos.push( {
trimMessagesToMax(areaTag, maxMessages, archivePath, err => { areaTag : areaTag,
maxMessages : maxMessages,
maxAgeDays : maxAgeDays,
} ); } );
});
return callback(null, areaInfos);
},
function trimAreas(areaInfos, callback) {
async.each(
areaInfos,
(areaInfo, next) => {
trimMessageAreaByMaxMessages(areaInfo, err => {
if(err) {
return next(err);
} }
trimMessageAreaByMaxAgeDays(areaInfo, err => {
return next(err);
}); });
});
},
callback
);
}
],
err => {
return cb(err);
} }
]
); );
console.log('trimming messages from scheduled event') // :TODO: remove me!!!
} }