2016-09-01 02:45:28 +00:00
|
|
|
// ===========================================
|
|
|
|
// REQUARKS WIKI - Background Agent
|
|
|
|
// 1.0.0
|
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
|
|
|
global.ROOTPATH = __dirname;
|
|
|
|
|
|
|
|
// ----------------------------------------
|
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-09-01 02:45:28 +00:00
|
|
|
global.winston = require('winston');
|
2016-09-05 04:39:59 +00:00
|
|
|
winston.remove(winston.transports.Console)
|
|
|
|
winston.add(winston.transports.Console, {
|
|
|
|
level: (_isDebug) ? 'info' : 'warn',
|
|
|
|
prettyPrint: true,
|
|
|
|
colorize: true,
|
|
|
|
silent: false,
|
|
|
|
timestamp: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Fetch internal handshake key
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
if(!process.argv[2] || process.argv[2].length !== 40) {
|
|
|
|
winston.error('[WS] Illegal process start. Missing handshake key.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
global.WSInternalKey = process.argv[2];
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Load modules
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
winston.info('[AGENT] Background Agent is initializing...');
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
var appconfig = require('./models/config')('./config.yml');
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
global.git = require('./models/git').init(appconfig);
|
2016-09-01 02:45:28 +00:00
|
|
|
global.entries = require('./models/entries').init(appconfig);
|
|
|
|
global.mark = require('./models/markdown');
|
|
|
|
|
|
|
|
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-05 04:39:59 +00:00
|
|
|
var wsClient = require('socket.io-client');
|
|
|
|
global.ws = wsClient('http://localhost:' + appconfig.wsPort, { reconnectionAttempts: 10 });
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Start Cron
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
var jobIsBusy = false;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
jobIsBusy = true;
|
|
|
|
|
|
|
|
// Prepare async job collector
|
|
|
|
|
|
|
|
let jobs = [];
|
2016-09-04 05:12:42 +00:00
|
|
|
let repoPath = path.resolve(ROOTPATH, appconfig.datadir.repo);
|
2016-09-01 02:45:28 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Compile Jobs
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
//-> Resync with Git remote
|
|
|
|
|
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-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) => {
|
|
|
|
|
|
|
|
//-> Update search index
|
|
|
|
|
|
|
|
if(fileStatus !== 'active') {
|
2016-09-05 04:39:59 +00:00
|
|
|
return entries.fetchIndexableVersion(entryPath).then((content) => {
|
|
|
|
ws.emit('searchAdd', {
|
|
|
|
auth: WSInternalKey,
|
|
|
|
content
|
|
|
|
});
|
2016-09-04 05:12:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(cacheJobs);
|
|
|
|
|
|
|
|
});
|
2016-09-01 02:45:28 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Run
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
Promise.all(jobs).then(() => {
|
2016-09-05 04:39:59 +00:00
|
|
|
winston.info('[AGENT] All jobs completed successfully! Going to sleep for now...');
|
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
|
|
|
// ----------------------------------------
|
|
|
|
// Connect to local WebSocket server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
ws.on('connect', function () {
|
|
|
|
job.start();
|
|
|
|
winston.info('[AGENT] Background Agent started successfully! [RUNNING]');
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('connect_error', function () {
|
|
|
|
winston.warn('[AGENT] Unable to connect to WebSocket server! Retrying...');
|
|
|
|
});
|
|
|
|
ws.on('reconnect_failed', function () {
|
|
|
|
winston.error('[AGENT] Failed to reconnect to WebSocket server too many times! Stopping agent...');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
});
|