2016-09-01 02:45:28 +00:00
|
|
|
// ===========================================
|
2016-12-09 03:44:31 +00:00
|
|
|
// Wiki.js - Background Agent
|
2016-09-01 02:45:28 +00:00
|
|
|
// 1.0.0
|
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
global.PROCNAME = 'AGENT';
|
2016-11-21 01:09:50 +00:00
|
|
|
global.ROOTPATH = __dirname;
|
|
|
|
global.IS_DEBUG = process.env.NODE_ENV === 'development';
|
2016-11-23 03:06:59 +00:00
|
|
|
if(IS_DEBUG) {
|
|
|
|
global.CORE_PATH = ROOTPATH + '/../core/';
|
|
|
|
} else {
|
2016-11-23 03:21:03 +00:00
|
|
|
global.CORE_PATH = ROOTPATH + '/node_modules/requarks-core/';
|
2016-11-23 03:06:59 +00:00
|
|
|
}
|
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-11-21 01:09:50 +00:00
|
|
|
global.winston = require(CORE_PATH + 'core-libs/winston')(IS_DEBUG);
|
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
|
|
|
|
2017-01-10 03:45:56 +00:00
|
|
|
let appconf = require(CORE_PATH + 'core-libs/config')();
|
|
|
|
global.appconfig = appconf.config;
|
|
|
|
global.appdata = appconf.data;
|
|
|
|
global.db = require(CORE_PATH + 'core-libs/mongodb').init();
|
|
|
|
global.upl = require('./libs/uploads-agent').init();
|
|
|
|
global.git = require('./libs/git').init();
|
|
|
|
global.entries = require('./libs/entries').init();
|
2016-10-16 05:34:34 +00:00
|
|
|
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) {
|
2016-12-06 02:42:41 +00:00
|
|
|
if(path.extname(item.path) === '.md' && path.basename(item.path) !== 'README.md') {
|
2016-09-04 05:12:42 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|