From 78f5df75831fe55bd66fa8e9dea6cdda5dcd4c6e Mon Sep 17 00:00:00 2001 From: Evan Tahler Date: Mon, 30 Jan 2012 19:52:32 -0800 Subject: [PATCH] cache logs to disc and reads in on boot --- config.json | 2 +- initializers/initCache.js | 11 +++++++++++ package.json | 2 +- tasks.js | 25 +++++++++++++++++++++++++ versions.markdown | 8 ++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index ad2f31b93..53afa61d0 100755 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "apiVerson" : "0.2.2", + "apiVerson" : "0.2.3", "webServerPort" : 8080, "socketServerPort" : 5000, "serverName" : "actionHero API", diff --git a/initializers/initCache.js b/initializers/initCache.js index 21ae76107..94a0fdb87 100644 --- a/initializers/initCache.js +++ b/initializers/initCache.js @@ -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(); } diff --git a/package.json b/package.json index 7188339f4..8ee36e9aa 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Evan Tahler ", "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", diff --git a/tasks.js b/tasks.js index 0bd282823..995dbb1b3 100755 --- a/tasks.js +++ b/tasks.js @@ -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; \ No newline at end of file diff --git a/versions.markdown b/versions.markdown index 6b8d462b9..2e14c791b 100644 --- a/versions.markdown +++ b/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