Generalize background tasks, rework aliases a bit

This commit is contained in:
calzoneman 2013-09-26 23:36:00 -05:00
parent 2f813c1d11
commit 6bf11a57b3
4 changed files with 64 additions and 21 deletions

View File

@ -1,3 +1,15 @@
Thu Sep 26 23:33 2013 CDT
* lib/stats.js: Remove this file, move the statistics tracking
interval to the new bgtask.js
* lib/bgtask.js: Generalize background tasks to this module. Add
tasks for statistics tracking and clearing old aliases.
* lib/database.js: Modify the way aliases work a bit. Rather than
constantly deleting to keep 5 aliases per person, have a function
that is periodically called to delete aliases more than one month old,
and have the listAliases function explicitly sort and request the most
recent 5 aliases.
* lib/server.js: Update references accordingly
Thu Sep 26 23:08 2013 CDT Thu Sep 26 23:08 2013 CDT
* lib/acp.js, lib/api.js, lib/server.js, lib/stats.js, www/acp.html, * lib/acp.js, lib/api.js, lib/server.js, lib/stats.js, www/acp.html,
www/assets/js/acp.js: Remove the [realtime] 'connection stats' www/assets/js/acp.js: Remove the [realtime] 'connection stats'

View File

@ -1,3 +1,4 @@
/* /*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery Copyright (c) 2013 Calvin Montgomery
@ -9,15 +10,24 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
/*
bgtask.js
Registers background jobs to run periodically while the server is
running.
*/
var Logger = require("./logger"); var Logger = require("./logger");
const STAT_INTERVAL = 60 * 60 * 1000; var init = null;
const STAT_EXPIRE = 24 * STAT_INTERVAL;
module.exports = function (Server) { /* Stats */
var db = Server.db; function initStats(Server) {
const STAT_INTERVAL = 60 * 60 * 1000;
const STAT_EXPIRE = 24 * STAT_INTERVAL;
setInterval(function () { setInterval(function () {
var db = Server.db;
var chancount = Server.channels.length; var chancount = Server.channels.length;
var usercount = 0; var usercount = 0;
Server.channels.forEach(function (chan) { Server.channels.forEach(function (chan) {
@ -31,3 +41,28 @@ module.exports = function (Server) {
}); });
}, STAT_INTERVAL); }, STAT_INTERVAL);
} }
/* Alias cleanup */
function initAliasCleanup(Server) {
const CLEAN_INTERVAL = 60 * 60 * 1000;
const CLEAN_EXPIRE = 30 * 24 * 60 * 60 * 1000; // 1 month
setInterval(function () {
console.log("cleaning aliases");
Server.db.cleanOldAliases(CLEAN_EXPIRE, function (err) {
Logger.syslog.log("Cleaned old aliases");
if (err)
Logger.errlog.log(err);
});
}, CLEAN_INTERVAL);
}
module.exports = function (Server) {
if (init === Server) {
Logger.errlog.log("WARNING: Attempted to re-init background tasks");
return;
}
initStats(Server);
initAliasCleanup(Server);
};

View File

@ -1273,22 +1273,16 @@ Database.prototype.recordVisit = function (ip, name, callback) {
var query = "DELETE FROM aliases WHERE ip=? AND name=?;" + var query = "DELETE FROM aliases WHERE ip=? AND name=?;" +
"INSERT INTO aliases VALUES (NULL, ?, ?, ?)"; "INSERT INTO aliases VALUES (NULL, ?, ?, ?)";
self.query(query, [ip, name, ip, name, time], function (err, res) { self.query(query, [ip, name, ip, name, time], callback);
if(err) { };
callback(err, null);
return;
}
callback(null, res); Database.prototype.cleanOldAliases = function (expiration, callback) {
query = "DELETE FROM aliases WHERE ip=? AND visit_id NOT IN (" + var self = this;
"SELECT visit_id FROM (" + if (typeof callback === "undefined")
"SELECT visit_id, time FROM aliases WHERE ip=?" + callback = blackHole;
"ORDER BY time DESC LIMIT 5" +
") foo" + // The 'foo' here is actually necessary
")";
self.query(query, [ip, ip]); var query = "DELETE FROM aliases WHERE time < ?";
}); self.query(query, [Date.now() - expiration], callback);
}; };
Database.prototype.listAliases = function (ip, callback) { Database.prototype.listAliases = function (ip, callback) {
@ -1296,7 +1290,7 @@ Database.prototype.listAliases = function (ip, callback) {
if(typeof callback !== "function") if(typeof callback !== "function")
return; return;
var query = "SELECT name FROM aliases WHERE ip"; var query = "SELECT name,time FROM aliases WHERE ip";
// Range // Range
if(ip.match(/^\d+\.\d+\.\d+$/)) { if(ip.match(/^\d+\.\d+\.\d+$/)) {
query += " LIKE ?"; query += " LIKE ?";
@ -1305,6 +1299,8 @@ Database.prototype.listAliases = function (ip, callback) {
query += "=?"; query += "=?";
} }
query += " ORDER BY time DESC LIMIT 5";
self.query(query, [ip], function (err, res) { self.query(query, [ip], function (err, res) {
var names = null; var names = null;
if(!err) { if(!err) {

View File

@ -241,8 +241,8 @@ var Server = {
// init ACP // init ACP
self.acp = require("./acp")(self); self.acp = require("./acp")(self);
// init stats // init background tasks
require("./stats")(self); require("./bgtask")(self);
// init media retriever // init media retriever
self.infogetter = require("./get-info")(self); self.infogetter = require("./get-info")(self);