| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | 
							- // ===========================================
 
- // REQUARKS WIKI - Background Agent
 
- // 1.0.0
 
- // Licensed under AGPLv3
 
- // ===========================================
 
- global.ROOTPATH = __dirname;
 
- global.PROCNAME = 'AGENT';
 
- // ----------------------------------------
 
- // Load Winston
 
- // ----------------------------------------
 
- var _isDebug = process.env.NODE_ENV === 'development';
 
- global.winston = require('./libs/winston')(_isDebug);
 
- // ----------------------------------------
 
- // Load global modules
 
- // ----------------------------------------
 
- winston.info('[AGENT] Background Agent is initializing...');
 
- 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');
 
- // ----------------------------------------
 
- // Load modules
 
- // ----------------------------------------
 
- var _ = require('lodash');
 
- var moment = require('moment');
 
- var Promise = require('bluebird');
 
- var fs = Promise.promisifyAll(require("fs-extra"));
 
- var path = require('path');
 
- var cron = require('cron').CronJob;
 
- // ----------------------------------------
 
- // Start Cron
 
- // ----------------------------------------
 
- var jobIsBusy = false;
 
- var jobUplWatchStarted = 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;
 
- 		}
 
- 		winston.info('[AGENT] Running all jobs...');
 
- 		jobIsBusy = true;
 
- 		// Prepare async job collector
 
- 		let jobs = [];
 
- 		let repoPath = path.resolve(ROOTPATH, appconfig.paths.repo);
 
- 		let dataPath = path.resolve(ROOTPATH, appconfig.paths.data);
 
- 		let uploadsPath = path.join(repoPath, 'uploads');
 
- 		let uploadsTempPath = path.join(dataPath, 'temp-upload');
 
- 		// ----------------------------------------
 
- 		// REGULAR JOBS
 
- 		// ----------------------------------------
 
- 		//*****************************************
 
- 		//-> Sync with Git remote
 
- 		//*****************************************
 
- 		jobs.push(git.onReady.then(() => {
 
- 			return git.resync().then(() => {
 
- 				//-> Stream all documents
 
- 				let cacheJobs = [];
 
- 				let jobCbStreamDocs_resolve = null,
 
- 						jobCbStreamDocs = new Promise((resolve, reject) => {
 
- 							jobCbStreamDocs_resolve = resolve;
 
- 						});
 
- 				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 cache and search index
 
- 								if(fileStatus !== 'active') {
 
- 									return entries.updateCache(entryPath);
 
- 								}
 
- 								return true;
 
- 							})
 
- 						);
 
- 					}
 
- 				}).on('end', () => {
 
- 					jobCbStreamDocs_resolve(Promise.all(cacheJobs));
 
- 				});
 
- 				return jobCbStreamDocs;
 
- 			});
 
- 		}));
 
- 		//*****************************************
 
- 		//-> Clear failed temporary upload files
 
- 		//*****************************************
 
- 		jobs.push(
 
- 			fs.readdirAsync(uploadsTempPath).then((ls) => {
 
- 				let fifteenAgo = moment().subtract(15, 'minutes');
 
- 				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) => {
 
- 						if(moment(f.stat.ctime).isBefore(fifteenAgo, 'minute')) {
 
- 							return fs.unlinkAsync(path.join(uploadsTempPath, f.filename));
 
- 						} else {
 
- 							return true;
 
- 						}
 
- 					});
 
- 				});
 
- 			})
 
- 		);
 
- 		// ----------------------------------------
 
- 		// Run
 
- 		// ----------------------------------------
 
- 		Promise.all(jobs).then(() => {
 
- 			winston.info('[AGENT] All jobs completed successfully! Going to sleep for now.');
 
- 			if(!jobUplWatchStarted) {
 
- 				jobUplWatchStarted = true;
 
- 				upl.initialScan();
 
- 			}
 
- 			return true;
 
- 		}).catch((err) => {
 
- 			winston.error('[AGENT] One or more jobs have failed: ', err);
 
- 		}).finally(() => {
 
- 			jobIsBusy = false;
 
- 		});
 
- 	},
 
- 	start: false,
 
- 	timeZone: 'UTC',
 
- 	runOnInit: true
 
- });
 
- // ----------------------------------------
 
- // Shutdown gracefully
 
- // ----------------------------------------
 
- process.on('disconnect', () => {
 
- 	winston.warn('[AGENT] Lost connection to main server. Exiting...');
 
- 	job.stop();
 
- 	process.exit();
 
- });
 
- process.on('exit', () => {
 
- 	job.stop();
 
- });
 
 
  |