mirror of https://github.com/calzoneman/sync.git
Generalize background tasks, rework aliases a bit
This commit is contained in:
parent
2f813c1d11
commit
6bf11a57b3
12
changelog
12
changelog
|
@ -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
|
||||
* 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'
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
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.
|
||||
*/
|
||||
|
||||
/*
|
||||
bgtask.js
|
||||
|
||||
Registers background jobs to run periodically while the server is
|
||||
running.
|
||||
*/
|
||||
|
||||
var Logger = require("./logger");
|
||||
|
||||
const STAT_INTERVAL = 60 * 60 * 1000;
|
||||
const STAT_EXPIRE = 24 * STAT_INTERVAL;
|
||||
var init = null;
|
||||
|
||||
module.exports = function (Server) {
|
||||
var db = Server.db;
|
||||
/* Stats */
|
||||
function initStats(Server) {
|
||||
const STAT_INTERVAL = 60 * 60 * 1000;
|
||||
const STAT_EXPIRE = 24 * STAT_INTERVAL;
|
||||
|
||||
setInterval(function () {
|
||||
var db = Server.db;
|
||||
var chancount = Server.channels.length;
|
||||
var usercount = 0;
|
||||
Server.channels.forEach(function (chan) {
|
||||
|
@ -31,3 +41,28 @@ module.exports = function (Server) {
|
|||
});
|
||||
}, 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);
|
||||
};
|
|
@ -1273,22 +1273,16 @@ Database.prototype.recordVisit = function (ip, name, callback) {
|
|||
var query = "DELETE FROM aliases WHERE ip=? AND name=?;" +
|
||||
"INSERT INTO aliases VALUES (NULL, ?, ?, ?)";
|
||||
|
||||
self.query(query, [ip, name, ip, name, time], function (err, res) {
|
||||
if(err) {
|
||||
callback(err, null);
|
||||
return;
|
||||
}
|
||||
self.query(query, [ip, name, ip, name, time], callback);
|
||||
};
|
||||
|
||||
callback(null, res);
|
||||
query = "DELETE FROM aliases WHERE ip=? AND visit_id NOT IN (" +
|
||||
"SELECT visit_id FROM (" +
|
||||
"SELECT visit_id, time FROM aliases WHERE ip=?" +
|
||||
"ORDER BY time DESC LIMIT 5" +
|
||||
") foo" + // The 'foo' here is actually necessary
|
||||
")";
|
||||
Database.prototype.cleanOldAliases = function (expiration, callback) {
|
||||
var self = this;
|
||||
if (typeof callback === "undefined")
|
||||
callback = blackHole;
|
||||
|
||||
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) {
|
||||
|
@ -1296,7 +1290,7 @@ Database.prototype.listAliases = function (ip, callback) {
|
|||
if(typeof callback !== "function")
|
||||
return;
|
||||
|
||||
var query = "SELECT name FROM aliases WHERE ip";
|
||||
var query = "SELECT name,time FROM aliases WHERE ip";
|
||||
// Range
|
||||
if(ip.match(/^\d+\.\d+\.\d+$/)) {
|
||||
query += " LIKE ?";
|
||||
|
@ -1305,6 +1299,8 @@ Database.prototype.listAliases = function (ip, callback) {
|
|||
query += "=?";
|
||||
}
|
||||
|
||||
query += " ORDER BY time DESC LIMIT 5";
|
||||
|
||||
self.query(query, [ip], function (err, res) {
|
||||
var names = null;
|
||||
if(!err) {
|
||||
|
|
|
@ -241,8 +241,8 @@ var Server = {
|
|||
// init ACP
|
||||
self.acp = require("./acp")(self);
|
||||
|
||||
// init stats
|
||||
require("./stats")(self);
|
||||
// init background tasks
|
||||
require("./bgtask")(self);
|
||||
|
||||
// init media retriever
|
||||
self.infogetter = require("./get-info")(self);
|
||||
|
|
Loading…
Reference in New Issue