fix: localization master callback + various fixes

pull/760/head
Nicolas Giard 6 years ago
parent 3abc254685
commit 27adad8dad

@ -1,4 +1,5 @@
const _ = require('lodash') const _ = require('lodash')
const EventEmitter = require('events')
/* global WIKI */ /* global WIKI */
@ -23,6 +24,8 @@ module.exports = {
await WIKI.models.onReady await WIKI.models.onReady
await WIKI.configSvc.loadFromDb() await WIKI.configSvc.loadFromDb()
await WIKI.queue.clean() await WIKI.queue.clean()
WIKI.events = new EventEmitter()
WIKI.redisSub = require('./redis').subscribe()
} catch (err) { } catch (err) {
WIKI.logger.error(err) WIKI.logger.error(err)
process.exit(1) process.exit(1)

@ -33,6 +33,15 @@ module.exports = {
// Load current language + namespaces // Load current language + namespaces
this.refreshNamespaces(true) this.refreshNamespaces(true)
// Listen for localization events
WIKI.events.on('localization', (action) => {
switch (action) {
case 'reload':
this.refreshNamespaces()
break
}
})
return this return this
}, },
attachMiddleware (app) { attachMiddleware (app) {

@ -36,6 +36,11 @@ module.exports = {
} }
}) })
}, },
async quit() {
for (const queueName in this.job) {
await this.job[queueName].close()
}
},
async clean() { async clean() {
return Promise.each(_.keys(WIKI.data.jobs), queueName => { return Promise.each(_.keys(WIKI.data.jobs), queueName => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

@ -19,5 +19,19 @@ module.exports = {
WIKI.logger.error('Invalid Redis configuration!') WIKI.logger.error('Invalid Redis configuration!')
process.exit(1) process.exit(1)
} }
},
subscribe() {
let red = this.init()
red.on('message', (channel, msg) => {
WIKI.events.emit(channel, msg)
})
red.subscribe('localization', (err, count) => {
if (err) {
WIKI.logger.error(err)
process.exit(1)
}
WIKI.logger.info('Redis Subscriber connection: [ OK ]')
})
return red
} }
} }

@ -4,8 +4,8 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const autoload = require('auto-load') const autoload = require('auto-load')
const PubSub = require('graphql-subscriptions').PubSub const PubSub = require('graphql-subscriptions').PubSub
const util = require('util') const { LEVEL, MESSAGE } = require('triple-beam')
const winston = require('winston') const Transport = require('winston-transport')
/* global WIKI */ /* global WIKI */
@ -37,20 +37,24 @@ let schemaDirectives = autoload(path.join(WIKI.SERVERPATH, 'graph/directives'))
// Live Trail Logger (admin) // Live Trail Logger (admin)
let LiveTrailLogger = winston.transports.LiveTrailLogger = function (options) { class LiveTrailLogger extends Transport {
this.name = 'livetrailLogger' constructor(opts) {
this.level = 'debug' super(opts)
}
util.inherits(LiveTrailLogger, winston.Transport) this.name = 'liveTrailLogger'
LiveTrailLogger.prototype.log = function (level, msg, meta, callback) { this.level = 'debug'
WIKI.GQLEmitter.publish('livetrail', { }
loggingLiveTrail: {
timestamp: new Date(), log (info, callback = () => {}) {
level, WIKI.GQLEmitter.publish('livetrail', {
output: msg loggingLiveTrail: {
} timestamp: new Date(),
}) level: info[LEVEL],
callback(null, true) output: info[MESSAGE]
}
})
callback(null, true)
}
} }
WIKI.logger.add(new LiveTrailLogger({})) WIKI.logger.add(new LiveTrailLogger({}))

@ -51,6 +51,8 @@ module.exports = async (job) => {
throw new Error('Failed to fetch cached locales list! Restart server to resolve this issue.') throw new Error('Failed to fetch cached locales list! Restart server to resolve this issue.')
} }
await WIKI.redis.publish('localization', 'reload')
WIKI.logger.info(`Fetching locale ${job.data.locale} from Graph endpoint: [ COMPLETED ]`) WIKI.logger.info(`Fetching locale ${job.data.locale} from Graph endpoint: [ COMPLETED ]`)
} catch (err) { } catch (err) {
WIKI.logger.error(`Fetching locale ${job.data.locale} from Graph endpoint: [ FAILED ]`) WIKI.logger.error(`Fetching locale ${job.data.locale} from Graph endpoint: [ FAILED ]`)

@ -29,16 +29,25 @@ module.exports = async (job) => {
$('h1,h2,h3,h4,h5,h6').each((idx, el) => { $('h1,h2,h3,h4,h5,h6').each((idx, el) => {
const depth = _.toSafeInteger(el.name.substring(1)) - (isStrict ? 1 : 2) const depth = _.toSafeInteger(el.name.substring(1)) - (isStrict ? 1 : 2)
let leafPathError = false
const leafPath = _.reduce(_.times(depth), (curPath, curIdx) => { const leafPath = _.reduce(_.times(depth), (curPath, curIdx) => {
if (_.has(toc, curPath)) { if (_.has(toc, curPath)) {
const lastLeafIdx = _.get(toc, curPath).length - 1 const lastLeafIdx = _.get(toc, curPath).length - 1
curPath = `${curPath}[${lastLeafIdx}].children` if (lastLeafIdx >= 0) {
curPath = `${curPath}[${lastLeafIdx}].children`
} else {
leafPathError = true
}
} }
return curPath return curPath
}, 'root') }, 'root')
if (leafPathError) { return }
const leafSlug = $('.toc-anchor', el).first().attr('href') const leafSlug = $('.toc-anchor', el).first().attr('href')
$('.toc-anchor', el).remove() $('.toc-anchor', el).remove()
_.get(toc, leafPath).push({ _.get(toc, leafPath).push({
title: _.trim($(el).text()), title: _.trim($(el).text()),
anchor: leafSlug, anchor: leafSlug,

@ -68,6 +68,8 @@ module.exports = async (job) => {
}).where('code', WIKI.config.lang.code) }).where('code', WIKI.config.lang.code)
} }
await WIKI.redis.publish('localization', 'reload')
WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]') WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')
} catch (err) { } catch (err) {
WIKI.logger.error('Syncing locales with Graph endpoint: [ FAILED ]') WIKI.logger.error('Syncing locales with Graph endpoint: [ FAILED ]')

@ -133,7 +133,7 @@ module.exports = () => {
nativeName: 'English' nativeName: 'English'
}) })
// Create default locale // Create default groups
WIKI.logger.info('Creating default groups...') WIKI.logger.info('Creating default groups...')
const adminGroup = await WIKI.models.groups.query().insert({ const adminGroup = await WIKI.models.groups.query().insert({

@ -121,6 +121,9 @@ const init = {
await global.WIKI.models.knex.destroy() await global.WIKI.models.knex.destroy()
console.warn(chalk.yellow('--- Closing Redis connections...')) console.warn(chalk.yellow('--- Closing Redis connections...'))
await global.WIKI.redis.quit() await global.WIKI.redis.quit()
await global.WIKI.redisSub.quit()
console.warn(chalk.yellow('--- Closing Queue connections...'))
await global.WIKI.queue.quit()
console.warn(chalk.yellow('--- Closing Server connections...')) console.warn(chalk.yellow('--- Closing Server connections...'))
global.WIKI.server.destroy(() => { global.WIKI.server.destroy(() => {
global.WIKI = {} global.WIKI = {}

Loading…
Cancel
Save