2016-09-01 02:45:28 +00:00
|
|
|
// ===========================================
|
|
|
|
// REQUARKS WIKI - Background Agent
|
|
|
|
// 1.0.0
|
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
|
|
|
global.ROOTPATH = __dirname;
|
2016-09-29 01:58:18 +00:00
|
|
|
global.PROCNAME = 'AGENT';
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-09-05 04:39:59 +00:00
|
|
|
// Load Winston
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-09-05 04:39:59 +00:00
|
|
|
var _isDebug = process.env.NODE_ENV === 'development';
|
2016-10-16 05:34:34 +00:00
|
|
|
global.winston = require('./libs/winston')(_isDebug);
|
2016-09-05 04:39:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-15 03:31:15 +00:00
|
|
|
// Load global modules
|
2016-09-05 04:39:59 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
winston.info('[AGENT] Background Agent is initializing...');
|
2016-09-01 02:45:28 +00:00
|
|
|
|
2016-10-16 05:34:34 +00:00
|
|
|
var appconfig = require('./libs/config')('./config.yml');
|
|
|
|
global.db = require('./libs/mongo').init(appconfig);
|
|
|
|
global.upl = require('./libs/uploads-agent').init(appconfig);
|
|
|
|
global.git = require('./libs/git').init(appconfig);
|
|
|
|
global.entries = require('./libs/entries').init(appconfig);
|
|
|
|
global.mark = require('./libs/markdown');
|
2016-10-15 03:31:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Load modules
|
|
|
|
// ----------------------------------------
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
var moment = require('moment');
|
|
|
|
var Promise = require('bluebird');
|
2016-09-04 05:12:42 +00:00
|
|
|
var fs = Promise.promisifyAll(require("fs-extra"));
|
|
|
|
var path = require('path');
|
2016-09-01 02:45:28 +00:00
|
|
|
var cron = require('cron').CronJob;
|
2016-09-25 04:08:49 +00:00
|
|
|
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Start Cron
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
var jobIsBusy = false;
|
2016-10-09 05:26:25 +00:00
|
|
|
var jobUplWatchStarted = false;
|
|
|
|
|
2016-09-01 02:45:28 +00:00
|
|
|
var job = new cron({
|
|
|
|
cronTime: '0 */5 * * * *',
|
|
|
|
onTick: () => {
|
|
|
|
|
|
|
|
// Make sure we don't start two concurrent jobs
|
|
|
|
|
|
|
|
if(jobIsBusy) {
|
|
|
|
winston.warn('[AGENT] Previous job has not completed gracefully or is still running! Skipping for now. (This is not normal, you should investigate)');
|
|
|
|
return;
|
|
|
|
}
|
2016-09-05 23:23:49 +00:00
|
|
|
winston.info('[AGENT] Running all jobs...');
|
2016-09-01 02:45:28 +00:00
|
|
|
jobIsBusy = true;
|
|
|
|
|
|
|
|
// Prepare async job collector
|
|
|
|
|
|
|
|
let jobs = [];
|
2016-10-15 03:31:15 +00:00
|
|
|
let repoPath = path.resolve(ROOTPATH, appconfig.paths.repo);
|
|
|
|
let dataPath = path.resolve(ROOTPATH, appconfig.paths.data);
|
2016-09-19 03:03:19 +00:00
|
|
|
let uploadsPath = path.join(repoPath, 'uploads');
|
2016-10-15 03:31:15 +00:00
|
|
|
let uploadsTempPath = path.join(dataPath, 'temp-upload');
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-15 03:31:15 +00:00
|
|
|
// REGULAR JOBS
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
//*****************************************
|
2016-10-15 03:31:15 +00:00
|
|
|
//-> Sync with Git remote
|
2016-09-19 03:03:19 +00:00
|
|
|
//*****************************************
|
2016-09-01 02:45:28 +00:00
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
jobs.push(git.onReady.then(() => {
|
|
|
|
return git.resync().then(() => {
|
2016-09-01 02:45:28 +00:00
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
//-> Stream all documents
|
2016-09-01 02:45:28 +00:00
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
let cacheJobs = [];
|
2016-09-05 23:23:49 +00:00
|
|
|
let jobCbStreamDocs_resolve = null,
|
|
|
|
jobCbStreamDocs = new Promise((resolve, reject) => {
|
|
|
|
jobCbStreamDocs_resolve = resolve;
|
|
|
|
});
|
2016-09-01 02:45:28 +00:00
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
fs.walk(repoPath).on('data', function (item) {
|
|
|
|
if(path.extname(item.path) === '.md') {
|
|
|
|
|
|
|
|
let entryPath = entries.parsePath(entries.getEntryPathFromFullPath(item.path));
|
|
|
|
let cachePath = entries.getCachePath(entryPath);
|
|
|
|
|
|
|
|
//-> Purge outdated cache
|
|
|
|
|
|
|
|
cacheJobs.push(
|
|
|
|
fs.statAsync(cachePath).then((st) => {
|
|
|
|
return moment(st.mtime).isBefore(item.stats.mtime) ? 'expired' : 'active';
|
|
|
|
}).catch((err) => {
|
|
|
|
return (err.code !== 'EEXIST') ? err : 'new';
|
|
|
|
}).then((fileStatus) => {
|
|
|
|
|
|
|
|
//-> Delete expired cache file
|
|
|
|
|
|
|
|
if(fileStatus === 'expired') {
|
|
|
|
return fs.unlinkAsync(cachePath).return(fileStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileStatus;
|
|
|
|
|
|
|
|
}).then((fileStatus) => {
|
|
|
|
|
2016-09-05 23:23:49 +00:00
|
|
|
//-> Update cache and search index
|
2016-09-04 05:12:42 +00:00
|
|
|
|
|
|
|
if(fileStatus !== 'active') {
|
2016-09-05 23:23:49 +00:00
|
|
|
return entries.updateCache(entryPath);
|
2016-09-04 05:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2016-09-05 23:23:49 +00:00
|
|
|
}).on('end', () => {
|
|
|
|
jobCbStreamDocs_resolve(Promise.all(cacheJobs));
|
2016-09-04 05:12:42 +00:00
|
|
|
});
|
|
|
|
|
2016-09-05 23:23:49 +00:00
|
|
|
return jobCbStreamDocs;
|
2016-09-04 05:12:42 +00:00
|
|
|
|
|
|
|
});
|
2016-09-01 02:45:28 +00:00
|
|
|
}));
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
//*****************************************
|
2016-10-15 03:31:15 +00:00
|
|
|
//-> Clear failed temporary upload files
|
2016-09-19 03:03:19 +00:00
|
|
|
//*****************************************
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
jobs.push(
|
|
|
|
fs.readdirAsync(uploadsTempPath).then((ls) => {
|
2016-09-19 03:03:19 +00:00
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
let fifteenAgo = moment().subtract(15, 'minutes');
|
2016-09-25 04:08:49 +00:00
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
return Promise.map(ls, (f) => {
|
|
|
|
return fs.statAsync(path.join(uploadsTempPath, f)).then((s) => { return { filename: f, stat: s }; });
|
|
|
|
}).filter((s) => { return s.stat.isFile(); }).then((arrFiles) => {
|
|
|
|
return Promise.map(arrFiles, (f) => {
|
2016-09-25 04:08:49 +00:00
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
if(moment(f.stat.ctime).isBefore(fifteenAgo, 'minute')) {
|
|
|
|
return fs.unlinkAsync(path.join(uploadsTempPath, f.filename));
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-25 04:08:49 +00:00
|
|
|
|
|
|
|
});
|
2016-09-19 03:03:19 +00:00
|
|
|
});
|
2016-09-25 04:08:49 +00:00
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
})
|
|
|
|
);
|
2016-09-19 03:03:19 +00:00
|
|
|
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Run
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
Promise.all(jobs).then(() => {
|
2016-09-05 23:23:49 +00:00
|
|
|
winston.info('[AGENT] All jobs completed successfully! Going to sleep for now.');
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
if(!jobUplWatchStarted) {
|
|
|
|
jobUplWatchStarted = true;
|
2016-10-30 22:41:10 +00:00
|
|
|
upl.initialScan().then(() => {
|
|
|
|
job.start();
|
|
|
|
});
|
2016-10-09 05:26:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
return true;
|
|
|
|
|
2016-09-01 02:45:28 +00:00
|
|
|
}).catch((err) => {
|
2016-09-05 04:39:59 +00:00
|
|
|
winston.error('[AGENT] One or more jobs have failed: ', err);
|
2016-09-01 02:45:28 +00:00
|
|
|
}).finally(() => {
|
|
|
|
jobIsBusy = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
2016-09-05 04:39:59 +00:00
|
|
|
start: false,
|
2016-09-04 05:12:42 +00:00
|
|
|
timeZone: 'UTC',
|
|
|
|
runOnInit: true
|
2016-09-01 02:45:28 +00:00
|
|
|
});
|
|
|
|
|
2016-09-05 04:39:59 +00:00
|
|
|
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Shutdown gracefully
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
process.on('disconnect', () => {
|
2016-09-05 04:39:59 +00:00
|
|
|
winston.warn('[AGENT] Lost connection to main server. Exiting...');
|
2016-09-01 02:45:28 +00:00
|
|
|
job.stop();
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
job.stop();
|
|
|
|
});
|