Skip to content

Commit

Permalink
cache logs to disc and reads in on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Jan 31, 2012
1 parent e4d4a06 commit 78f5df7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config.json
@@ -1,5 +1,5 @@
{
"apiVerson" : "0.2.2",
"apiVerson" : "0.2.3",
"webServerPort" : 8080,
"socketServerPort" : 5000,
"serverName" : "actionHero API",
Expand Down
11 changes: 11 additions & 0 deletions initializers/initCache.js
Expand Up @@ -54,6 +54,17 @@ var initCache = function(api, next){
}
};

// check for an existing cache file
try{
var fileData = api.fs.readFileSync(api.configData.logFolder + "/cache.json",'utf8');
api.cache.data = JSON.parse(fileData);
api.log("data cache from backup file.");
}catch(e){
api.log("no cache backup file found, continuing.");
// api.log(" > "+e);
}


next();
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Evan Tahler <evantahler@gmail.com>",
"name": "actionHero",
"description": "actionHero is a minimalist, multi-node, transactional API framework written in javaScript",
"version": "0.2.2",
"version": "0.2.3",
"homepage": "https://github.com/evantahler/actionHero",
"repository": {
"type": "git",
Expand Down
25 changes: 25 additions & 0 deletions tasks.js
Expand Up @@ -99,6 +99,31 @@ tasks.caclculateStats = function(api, next) {
process.nextTick(function () { task.run() });
};

////////////////////////////////////////////////////////////////////////////
// perioducally save cache to disc
tasks.saveCacheToDisk = function(api, next) {
var params = {
"name" : "saveCacheToDisk",
"desc" : "I will save the cache object, api.cache.data, to disc every so often"
};
var task = Object.create(api.tasks.Task);
task.init(api, params, next);
task.run = function() {
try{
var fs = api.fs.createWriteStream((api.configData.logFolder + "/cache.json"), {flags:"w"})
var encodedData = new Buffer(JSON.stringify(api.cache.data)).toString('utf8')
fs.write(encodedData);
fs.end();
task.end();
}catch(e){
api.log("Error writing to datalogFolder file: " + e, "red");
task.end();
}
};
//
process.nextTick(function () { task.run() });
};

////////////////////////////////////////////////////////////////////////////
// Export
exports.tasks = tasks;
8 changes: 8 additions & 0 deletions versions.markdown
@@ -1,5 +1,13 @@
# Action Hero API Versions

## Version 0.2.3

**Summary:** This is a general cleanup release

**Details:**

* The cache will now write its contents to disc periodically, as defined by the `tasks.saveCacheToDisk` task. The api will also attempt to load in a cache file on boot.

## Version 0.2.2

**Summary:** This release adds additional functionality to the actionCluster and Cache
Expand Down

0 comments on commit 78f5df7

Please sign in to comment.