Added Background Agent (git resync + cache purge)
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
};
|
@@ -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)) {
|
||||
|
Reference in New Issue
Block a user