feat: modular auth + queue tasks
This commit is contained in:
65
server/queues/git-sync.js
Normal file
65
server/queues/git-sync.js
Normal file
@@ -0,0 +1,65 @@
|
||||
'use strict'
|
||||
|
||||
/* global wiki */
|
||||
|
||||
const Promise = require('bluebird')
|
||||
const fs = Promise.promisifyAll(require('fs-extra'))
|
||||
const klaw = require('klaw')
|
||||
const moment = require('moment')
|
||||
const path = require('path')
|
||||
const entryHelper = require('../helpers/entry')
|
||||
|
||||
module.exports = (job, done) => {
|
||||
return wiki.git.resync().then(() => {
|
||||
// -> Stream all documents
|
||||
|
||||
let cacheJobs = []
|
||||
let jobCbStreamDocsResolve = null
|
||||
let jobCbStreamDocs = new Promise((resolve, reject) => {
|
||||
jobCbStreamDocsResolve = resolve
|
||||
})
|
||||
|
||||
klaw(wiki.REPOPATH).on('data', function (item) {
|
||||
if (path.extname(item.path) === '.md' && path.basename(item.path) !== 'README.md') {
|
||||
let entryPath = entryHelper.parsePath(entryHelper.getEntryPathFromFullPath(item.path))
|
||||
let cachePath = entryHelper.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 global.entries.updateCache(entryPath).then(entry => {
|
||||
process.send({
|
||||
action: 'searchAdd',
|
||||
content: entry
|
||||
})
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
)
|
||||
}
|
||||
}).on('end', () => {
|
||||
jobCbStreamDocsResolve(Promise.all(cacheJobs))
|
||||
})
|
||||
|
||||
return jobCbStreamDocs
|
||||
})
|
||||
}
|
26
server/queues/upl-clear-temp.js
Normal file
26
server/queues/upl-clear-temp.js
Normal file
@@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
/* global wiki */
|
||||
|
||||
const Promise = require('bluebird')
|
||||
const fs = Promise.promisifyAll(require('fs-extra'))
|
||||
const moment = require('moment')
|
||||
const path = require('path')
|
||||
|
||||
module.exports = (job, done) => {
|
||||
return fs.readdirAsync(wiki.UPLTEMPPATH).then((ls) => {
|
||||
let fifteenAgo = moment().subtract(15, 'minutes')
|
||||
|
||||
return Promise.map(ls, (f) => {
|
||||
return fs.statAsync(path.join(wiki.UPLTEMPPATH, 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(wiki.UPLTEMPPATH, f.filename))
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user