wikijs-fork/server/jobs/sync-storage.js

36 lines
1010 B
JavaScript
Raw Normal View History

2019-02-18 02:48:48 +00:00
const _ = require('lodash')
/* global WIKI */
2019-02-18 02:48:48 +00:00
module.exports = async (targetKey) => {
WIKI.logger.info(`Syncing with storage target ${targetKey}...`)
2019-02-18 02:48:48 +00:00
try {
const target = _.find(WIKI.models.storage.targets, ['key', targetKey])
if (target) {
await target.fn.sync()
WIKI.logger.info(`Syncing with storage target ${targetKey}: [ COMPLETED ]`)
2019-02-25 04:48:28 +00:00
await WIKI.models.storage.query().patch({
state: {
status: 'operational',
message: '',
lastAttempt: new Date().toISOString()
}
}).where('key', targetKey)
2019-02-18 02:48:48 +00:00
} else {
throw new Error('Invalid storage target. Unable to perform sync.')
}
} catch (err) {
WIKI.logger.error(`Syncing with storage target ${targetKey}: [ FAILED ]`)
WIKI.logger.error(err.message)
2019-02-25 04:48:28 +00:00
await WIKI.models.storage.query().patch({
state: {
status: 'error',
message: err.message,
lastAttempt: new Date().toISOString()
}
}).where('key', targetKey)
2019-02-18 02:48:48 +00:00
}
}