Browse Source

automatically delete old collections from anonymous people

master
mort 8 years ago
parent
commit
1e4449fde9
2 changed files with 31 additions and 4 deletions
  1. 4
    1
      conf.json.example
  2. 27
    3
      server.js

+ 4
- 1
conf.json.example View File

@@ -25,5 +25,8 @@
"imgs": "imgs"
},
"debug": false,
"maxRuns": 9999
"max_runs": 9999,
"cache_max_age": 604800,
"purge_collections_timeout": "2 days",
"purge_collections_interval": 1800000
}

+ 27
- 3
server.js View File

@@ -67,7 +67,7 @@ try {
else
throw err;
}
currentRun = (currentRun >= conf.maxRuns ? 0 : currentRun);
currentRun = (currentRun >= conf.max_runs ? 0 : currentRun);
currentRun = (currentRun || 0) + 1;
conf.web.currentRun = currentRun.toString();
fs.writeFileSync(".currentRun", currentRun, "utf8");
@@ -102,8 +102,8 @@ function onRequest(req, res) {
ep(ctx);
} else {

//Cache content for a year
ctx.setHeader("Cache-Control", "public, max-age=31536000");
//Cache content for a while
ctx.setHeader("Cache-Control", "public, max-age="+conf.cache_max_age);

//Gzip and such
if (ctx.shouldGzip && gzipCache[req.url]) {
@@ -132,8 +132,32 @@ db.connect(function() {
server.listen(conf.port);

console.log("Listening on port "+conf.port+".");

purgeCollections();
});

//On an interval, delete old collections from anonymous users
function purgeCollections() {
var timeout = conf.purge_collections_timeout;
db.query(
"DELETE FROM collections "+
"WHERE user_id IS NULL "+
"AND date_created < NOW() - INTERVAL '"+timeout+"'",
function(err, res) {
if (err)
throw err;

if (res.rowCount > 0) {
console.log(
"Deleted "+res.rowCount+" collections "+
"from over "+timeout+" ago."
);
}
}
);
}
setTimeout(purgeCollections, conf.purge_collections_interval);

//We don't want to crash even if something throws an uncaught exception.
if (!conf.debug) {
var d = domain.create();

Loading…
Cancel
Save