* Clean up door.py
* Implement trimMessageAreasScheduledEvent() method * Misc cleanup
This commit is contained in:
parent
6e6ebc9b18
commit
f5a1a79777
|
@ -169,17 +169,19 @@ function createMessageBaseTables() {
|
|||
'END;'
|
||||
);
|
||||
|
||||
// :TODO: need SQL to ensure cleaned up if delete from message?
|
||||
dbs.message.run(
|
||||
'CREATE TABLE IF NOT EXISTS message_meta (' +
|
||||
' message_id INTEGER NOT NULL,' +
|
||||
' meta_category INTEGER NOT NULL,' +
|
||||
' meta_name 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)' +
|
||||
');'
|
||||
);
|
||||
|
||||
// :TODO: need SQL to ensure cleaned up if delete from message?
|
||||
dbs.message.run(
|
||||
'CREATE TABLE IF NOT EXISTS hash_tag (' +
|
||||
' 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(
|
||||
'CREATE TABLE IF NOT EXISTS message_hash_tag (' +
|
||||
' hash_tag_id INTEGER NOT NULL,' +
|
||||
|
|
114
core/door.js
114
core/door.js
|
@ -1,24 +1,24 @@
|
|||
/* jslint node: true */
|
||||
'use strict';
|
||||
|
||||
var spawn = require('child_process').spawn;
|
||||
var events = require('events');
|
||||
const events = require('events');
|
||||
|
||||
var _ = require('lodash');
|
||||
var pty = require('ptyw.js');
|
||||
var decode = require('iconv-lite').decode;
|
||||
var net = require('net');
|
||||
var async = require('async');
|
||||
const _ = require('lodash');
|
||||
const pty = require('ptyw.js');
|
||||
const decode = require('iconv-lite').decode;
|
||||
const createServer = require('net').createServer;
|
||||
|
||||
exports.Door = Door;
|
||||
|
||||
function Door(client, exeInfo) {
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
const self = this;
|
||||
this.client = client;
|
||||
this.exeInfo = exeInfo;
|
||||
|
||||
this.exeInfo.encoding = this.exeInfo.encoding || 'cp437';
|
||||
this.exeInfo.encoding = this.exeInfo.encoding.toLowerCase();
|
||||
let restored = false;
|
||||
|
||||
//
|
||||
// Members of exeInfo:
|
||||
|
@ -32,24 +32,16 @@ function Door(client, exeInfo) {
|
|||
// node
|
||||
// inhSocket
|
||||
//
|
||||
}
|
||||
|
||||
require('util').inherits(Door, events.EventEmitter);
|
||||
|
||||
|
||||
|
||||
Door.prototype.run = function() {
|
||||
|
||||
var self = this;
|
||||
|
||||
var doorData = function(data) {
|
||||
// :TODO: skip decoding if we have a match, e.g. cp437 === cp437
|
||||
this.doorDataHandler = function(data) {
|
||||
if(self.client.term.outputEncoding === self.exeInfo.encoding) {
|
||||
self.client.term.rawWrite(data);
|
||||
} else {
|
||||
self.client.term.write(decode(data, self.exeInfo.encoding));
|
||||
}
|
||||
};
|
||||
|
||||
var restored = false;
|
||||
|
||||
var restore = function(piped) {
|
||||
this.restoreIo = function(piped) {
|
||||
if(!restored && self.client.term.output) {
|
||||
self.client.term.output.unpipe(piped);
|
||||
self.client.term.output.resume();
|
||||
|
@ -57,56 +49,63 @@ Door.prototype.run = function() {
|
|||
}
|
||||
};
|
||||
|
||||
var sockServer;
|
||||
|
||||
async.series(
|
||||
[
|
||||
function prepareServer(callback) {
|
||||
this.prepareSocketIoServer = function(cb) {
|
||||
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.
|
||||
if(!err && count <= 1) {
|
||||
self.client.term.output.pipe(conn);
|
||||
|
||||
conn.on('data', doorData);
|
||||
conn.on('data', self.doorDataHandler);
|
||||
|
||||
conn.once('end', function ended() {
|
||||
restore(conn);
|
||||
conn.once('end', () => {
|
||||
return self.restoreIo(conn);
|
||||
});
|
||||
|
||||
conn.once('error', function error(err) {
|
||||
self.client.log.info('Door socket server connection error: ' + err.message);
|
||||
restore(conn);
|
||||
conn.once('error', err => {
|
||||
self.client.log.info( { error : err.toString() }, 'Door socket server connection');
|
||||
return self.restoreIo(conn);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
sockServer.listen(0, function listening() {
|
||||
callback(null);
|
||||
sockServer.listen(0, () => {
|
||||
return cb(null, sockServer);
|
||||
});
|
||||
} 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({
|
||||
dropFile : self.exeInfo.dropFile,
|
||||
node : self.exeInfo.node.toString(),
|
||||
//inhSocket : self.exeInfo.inhSocket.toString(),
|
||||
srvPort : sockServer ? sockServer.address().port.toString() : '-1',
|
||||
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,
|
||||
rows : self.client.term.termHeight,
|
||||
// :TODO: cwd
|
||||
|
@ -118,19 +117,17 @@ Door.prototype.run = function() {
|
|||
|
||||
self.client.term.output.pipe(door);
|
||||
|
||||
door.on('data', doorData);
|
||||
door.on('data', self.doorDataHandler);
|
||||
|
||||
door.once('close', function closed() {
|
||||
restore(door);
|
||||
door.once('close', () => {
|
||||
return self.restoreIo(door);
|
||||
});
|
||||
} else if('socket' === self.exeInfo.io) {
|
||||
self.client.log.debug(
|
||||
{ port : sockServer.address().port },
|
||||
'Using temporary socket server for door I/O');
|
||||
self.client.log.debug( { port : sockServer.address().port }, 'Using temporary socket server for door I/O');
|
||||
}
|
||||
|
||||
door.once('exit', function exited(code) {
|
||||
self.client.log.info( { code : code }, 'Door exited');
|
||||
door.once('exit', exitCode => {
|
||||
self.client.log.info( { exitCode : exitCode }, 'Door exited');
|
||||
|
||||
if(sockServer) {
|
||||
sockServer.close();
|
||||
|
@ -138,19 +135,12 @@ Door.prototype.run = function() {
|
|||
|
||||
// we may not get a close
|
||||
if('stdio' === self.exeInfo.io) {
|
||||
restore(door);
|
||||
return self.restoreIo(door);
|
||||
}
|
||||
|
||||
door.removeAllListeners();
|
||||
|
||||
self.emit('finished');
|
||||
});
|
||||
}
|
||||
],
|
||||
function complete(err) {
|
||||
if(err) {
|
||||
self.client.log.warn( { error : err.toString() }, 'Failed executing door');
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
|
@ -1,16 +1,17 @@
|
|||
/* jslint node: true */
|
||||
'use strict';
|
||||
|
||||
let msgDb = require('./database.js').dbs.message;
|
||||
let Config = require('./config.js').config;
|
||||
let Message = require('./message.js');
|
||||
let Log = require('./logger.js').log;
|
||||
let checkAcs = require('./acs_util.js').checkAcs;
|
||||
let msgNetRecord = require('./msg_network.js').recordMessage;
|
||||
const msgDb = require('./database.js').dbs.message;
|
||||
const Config = require('./config.js').config;
|
||||
const Message = require('./message.js');
|
||||
const Log = require('./logger.js').log;
|
||||
const checkAcs = require('./acs_util.js').checkAcs;
|
||||
const msgNetRecord = require('./msg_network.js').recordMessage;
|
||||
|
||||
let async = require('async');
|
||||
let _ = require('lodash');
|
||||
let assert = require('assert');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
const assert = require('assert');
|
||||
const moment = require('moment');
|
||||
|
||||
exports.getAvailableMessageConferences = getAvailableMessageConferences;
|
||||
exports.getSortedAvailMessageConferences = getSortedAvailMessageConferences;
|
||||
|
@ -466,65 +467,58 @@ function persistMessage(message, cb) {
|
|||
);
|
||||
}
|
||||
|
||||
function trimMessagesToMax(areaTag, maxMessages, archivePath, cb) {
|
||||
async.waterfall(
|
||||
[
|
||||
function getRemoteCount(callback) {
|
||||
let removeCount = 0;
|
||||
msgDb.get(
|
||||
`SELECT COUNT(area_tag) AS msgCount
|
||||
// method exposed for event scheduler
|
||||
function trimMessageAreasScheduledEvent(args, cb) {
|
||||
|
||||
function trimMessageAreaByMaxMessages(areaInfo, cb) {
|
||||
if(0 === areaInfo.maxMessages) {
|
||||
return cb(null);
|
||||
}
|
||||
|
||||
msgDb.run(
|
||||
`DELETE FROM message
|
||||
WHERE message_id IN
|
||||
(SELECT message_id
|
||||
FROM message
|
||||
WHERE area_tag = ?`,
|
||||
[ areaTag ],
|
||||
(err, row) => {
|
||||
if(!err) {
|
||||
if(row.msgCount >= maxMessages) {
|
||||
removeCount = row.msgCount - maxMessages;
|
||||
}
|
||||
}
|
||||
return callback(err, removeCount);
|
||||
}
|
||||
);
|
||||
},
|
||||
function trimMessages(removeCount, callback) {
|
||||
if(0 === removeCount) {
|
||||
return callback(null);
|
||||
}
|
||||
|
||||
if(archivePath) {
|
||||
|
||||
} else {
|
||||
// just delete 'em
|
||||
}
|
||||
}
|
||||
],
|
||||
WHERE area_tag = ?
|
||||
ORDER BY message_id
|
||||
LIMIT (MAX(0, (SELECT COUNT()
|
||||
FROM message
|
||||
WHERE area_tag = ?) - ${areaInfo.maxMessages}
|
||||
))
|
||||
);`,
|
||||
[ areaInfo.areaTag, areaInfo.areaTag],
|
||||
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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function trimMessageAreaByMaxAgeDays(areaInfo, cb) {
|
||||
if(0 === areaInfo.maxAgeDays) {
|
||||
return cb(null);
|
||||
}
|
||||
|
||||
msgDb.run(
|
||||
`DELETE FROM message
|
||||
WHERE area_tag = ? AND modified_timestamp < date('now', '-${areaInfo.maxAgeDays} days');`,
|
||||
[ areaInfo.areaTag ],
|
||||
err => {
|
||||
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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// method exposed for event scheduler
|
||||
function trimMessageAreasScheduledEvent(args, cb) {
|
||||
//
|
||||
// Available args:
|
||||
// - archive:/path/to/archive/dir/
|
||||
//
|
||||
let archivePath;
|
||||
if(args) {
|
||||
args.forEach(a => {
|
||||
if(a.startsWith('archive:')) {
|
||||
archivePath = a.split(':')[1];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
// 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(
|
||||
[
|
||||
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 => {
|
||||
|
||||
let maxMessages = Config.messageAreaDefaults.maxMessages;
|
||||
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.maxMessages) {
|
||||
maxMessages = area.maxMessages;
|
||||
|
@ -559,17 +556,36 @@ function trimMessageAreasScheduledEvent(args, cb) {
|
|||
}
|
||||
}
|
||||
|
||||
if(maxMessages) {
|
||||
trimMessagesToMax(areaTag, maxMessages, archivePath, err => {
|
||||
|
||||
areaInfos.push( {
|
||||
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!!!
|
||||
|
||||
}
|
Loading…
Reference in New Issue