2013-09-27 04:36:00 +00:00
|
|
|
/*
|
|
|
|
bgtask.js
|
|
|
|
|
|
|
|
Registers background jobs to run periodically while the server is
|
|
|
|
running.
|
|
|
|
*/
|
|
|
|
|
2013-07-18 02:55:50 +00:00
|
|
|
var Logger = require("./logger");
|
2014-01-22 23:11:26 +00:00
|
|
|
var Config = require("./config");
|
2014-02-01 19:03:08 +00:00
|
|
|
var db = require("./database");
|
2013-07-18 02:55:50 +00:00
|
|
|
|
2013-09-27 04:36:00 +00:00
|
|
|
var init = null;
|
2013-07-18 02:55:50 +00:00
|
|
|
|
2013-09-27 04:36:00 +00:00
|
|
|
/* Stats */
|
|
|
|
function initStats(Server) {
|
2014-03-10 03:36:39 +00:00
|
|
|
var STAT_INTERVAL = parseInt(Config.get("stats.interval"));
|
|
|
|
var STAT_EXPIRE = parseInt(Config.get("stats.max-age"));
|
2013-08-20 04:53:33 +00:00
|
|
|
|
2013-07-16 18:58:29 +00:00
|
|
|
setInterval(function () {
|
|
|
|
var chancount = Server.channels.length;
|
|
|
|
var usercount = 0;
|
|
|
|
Server.channels.forEach(function (chan) {
|
|
|
|
usercount += chan.users.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
var mem = process.memoryUsage().rss;
|
|
|
|
|
2013-08-17 23:44:48 +00:00
|
|
|
db.addStatPoint(Date.now(), usercount, chancount, mem, function () {
|
|
|
|
db.pruneStats(Date.now() - STAT_EXPIRE);
|
|
|
|
});
|
2013-07-18 02:55:50 +00:00
|
|
|
}, STAT_INTERVAL);
|
2013-07-16 18:58:29 +00:00
|
|
|
}
|
2013-09-27 04:36:00 +00:00
|
|
|
|
|
|
|
/* Alias cleanup */
|
|
|
|
function initAliasCleanup(Server) {
|
2014-03-10 03:36:39 +00:00
|
|
|
var CLEAN_INTERVAL = parseInt(Config.get("aliases.purge-interval"));
|
|
|
|
var CLEAN_EXPIRE = parseInt(Config.get("aliases.max-age"));
|
2013-09-27 04:36:00 +00:00
|
|
|
|
|
|
|
setInterval(function () {
|
2014-02-01 19:03:08 +00:00
|
|
|
db.cleanOldAliases(CLEAN_EXPIRE, function (err) {
|
2013-09-27 04:36:00 +00:00
|
|
|
Logger.syslog.log("Cleaned old aliases");
|
|
|
|
if (err)
|
|
|
|
Logger.errlog.log(err);
|
|
|
|
});
|
|
|
|
}, CLEAN_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2014-02-01 19:03:08 +00:00
|
|
|
/* Password reset cleanup */
|
|
|
|
function initPasswordResetCleanup(Server) {
|
|
|
|
var CLEAN_INTERVAL = 8*60*60*1000;
|
|
|
|
|
|
|
|
setInterval(function () {
|
|
|
|
db.cleanOldPasswordResets(function (err) {
|
|
|
|
if (err)
|
|
|
|
Logger.errlog.log(err);
|
|
|
|
});
|
|
|
|
}, CLEAN_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2013-10-14 21:39:41 +00:00
|
|
|
function initChannelDumper(Server) {
|
2014-03-10 03:36:39 +00:00
|
|
|
var CHANNEL_SAVE_INTERVAL = parseInt(Config.get("channel-save-interval"))
|
|
|
|
* 60000;
|
2013-10-14 21:39:41 +00:00
|
|
|
setInterval(function () {
|
|
|
|
for (var i = 0; i < Server.channels.length; i++) {
|
|
|
|
var chan = Server.channels[i];
|
|
|
|
if (!chan.dead && chan.users && chan.users.length > 0) {
|
2014-01-08 04:47:00 +00:00
|
|
|
chan.saveState();
|
2013-10-14 21:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, CHANNEL_SAVE_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2013-09-27 04:36:00 +00:00
|
|
|
module.exports = function (Server) {
|
|
|
|
if (init === Server) {
|
|
|
|
Logger.errlog.log("WARNING: Attempted to re-init background tasks");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-14 21:32:03 +00:00
|
|
|
init = Server;
|
2013-09-27 04:36:00 +00:00
|
|
|
initStats(Server);
|
|
|
|
initAliasCleanup(Server);
|
2013-10-14 21:39:41 +00:00
|
|
|
initChannelDumper(Server);
|
2014-02-01 19:03:08 +00:00
|
|
|
initPasswordResetCleanup(Server);
|
2013-09-27 04:36:00 +00:00
|
|
|
};
|