Added Background Agent (git resync + cache purge)

This commit is contained in:
NGPixel
2016-08-31 22:45:28 -04:00
parent e8ebf9d231
commit 59feacd846
6 changed files with 156 additions and 4 deletions

View File

@@ -6,7 +6,8 @@ var Promise = require('bluebird'),
_ = require('lodash'),
farmhash = require('farmhash'),
BSONModule = require('bson'),
BSON = new BSONModule.BSONPure.BSON();
BSON = new BSONModule.BSONPure.BSON(),
moment = require('moment');
/**
* Entries Model
@@ -259,6 +260,17 @@ module.exports = {
return path.join(this._cachePath, farmhash.fingerprint32(entryPath) + '.bson');
},
/**
* Gets the entry path from full path.
*
* @param {String} fullPath The full path
* @return {String} The entry path
*/
getEntryPathFromFullPath(fullPath) {
let absRepoPath = path.resolve(ROOTPATH, this._repoPath);
return _.chain(fullPath).replace(absRepoPath, '').replace('.md', '').replace(new RegExp('\\\\', 'g'),'/').value();
},
/**
* Update an existing document
*
@@ -344,6 +356,35 @@ module.exports = {
return _.replace(contents, new RegExp('{TITLE}', 'g'), formattedTitle);
});
},
purgeStaleCache() {
let self = this;
let cacheJobs = [];
fs.walk(self._repoPath)
.on('data', function (item) {
if(path.extname(item.path) === '.md') {
let entryPath = self.parsePath(self.getEntryPathFromFullPath(item.path));
let cachePath = self.getCachePath(entryPath);
cacheJobs.push(fs.statAsync(cachePath).then((st) => {
if(moment(st.mtime).isBefore(item.stats.mtime)) {
return fs.unlinkAsync(cachePath);
} else {
return true;
}
}).catch((err) => {
return (err.code !== 'EEXIST') ? err : true;
}));
}
});
return Promise.all(cacheJobs);
}
};

View File

@@ -37,10 +37,12 @@ module.exports = {
* @param {Object} appconfig The application config
* @return {Object} Git model instance
*/
init(appconfig) {
init(appconfig, sync) {
let self = this;
self._repo.sync = sync;
//-> Build repository path
if(_.isEmpty(appconfig.datadir.repo)) {