浏览代码

automatically delete old collections from anonymous people

master
mort 8 年前
父节点
当前提交
1e4449fde9
共有 2 个文件被更改,包括 31 次插入4 次删除
  1. 4
    1
      conf.json.example
  2. 27
    3
      server.js

+ 4
- 1
conf.json.example 查看文件

"imgs": "imgs" "imgs": "imgs"
}, },
"debug": false, "debug": false,
"maxRuns": 9999
"max_runs": 9999,
"cache_max_age": 604800,
"purge_collections_timeout": "2 days",
"purge_collections_interval": 1800000
} }

+ 27
- 3
server.js 查看文件

else else
throw err; throw err;
} }
currentRun = (currentRun >= conf.maxRuns ? 0 : currentRun);
currentRun = (currentRun >= conf.max_runs ? 0 : currentRun);
currentRun = (currentRun || 0) + 1; currentRun = (currentRun || 0) + 1;
conf.web.currentRun = currentRun.toString(); conf.web.currentRun = currentRun.toString();
fs.writeFileSync(".currentRun", currentRun, "utf8"); fs.writeFileSync(".currentRun", currentRun, "utf8");
ep(ctx); ep(ctx);
} else { } 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 //Gzip and such
if (ctx.shouldGzip && gzipCache[req.url]) { if (ctx.shouldGzip && gzipCache[req.url]) {
server.listen(conf.port); server.listen(conf.port);


console.log("Listening on port "+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. //We don't want to crash even if something throws an uncaught exception.
if (!conf.debug) { if (!conf.debug) {
var d = domain.create(); var d = domain.create();

正在加载...
取消
保存