Commit 7b02c46e authored by Anton's avatar Anton

Update conversion responder

parent 382ac47f
const
config = require('config'),
Queue = require('bull'),
{ DateTime } = require('luxon'),
{ ObjectUtil } = require('mbjs-utils'),
cote = require('cote')
const init = async function () {
const
queue = new Queue('conversions', config.conversions.redisURL),
conversionService = new cote.Responder({ name: 'Conversion Service' })
class ConversionService {
constructor (config = {}) {
this._queue = new Queue('conversions', config.redisURL)
this._queue.process(parseInt(config.concurrency), require('../workers/convert'))
queue.process(parseInt(config.conversions.concurrency), require('../workers/convert'))
this._responder = new cote.Responder({ name: 'Conversion Service', key: 'service.conversion' })
conversionService.on('start', (req, cb) => {
const jobId = ObjectUtil.uuid4()
queue.add(req, { jobId })
cb({ jobId })
})
this._responder.on('service.conversion:post', (req, cb) => {
const jobId = ObjectUtil.uuid4()
this._queue.add(req, { jobId })
conversionService.on('status', async (req, cb) => {
const job = await queue.getJob(req.id)
if (!job) return cb(null)
cb({ jobId })
})
const jobInfo = {
uuid: job.id,
source: job.data.source,
result: job.returnvalue,
failed: typeof job.failedReason !== 'undefined',
attempts: job.attemptsMade,
progress: job.progress,
delay: job.delay,
created: DateTime.fromMillis(job.timestamp).toISO(),
processed: job.processedOn ? DateTime.fromMillis(job.processedOn).toISO() : undefined,
finished: job.finishedOn ? DateTime.fromMillis(job.finishedOn).toISO() : undefined
}
this._responder.on('service.conversion:get', async (req, cb) => {
const job = await this._queue.getJob(req.id)
cb(jobInfo)
})
if (job) cb({
uuid: job.id,
source: job.data.source,
result: job.returnvalue,
failed: typeof job.failedReason !== 'undefined',
attempts: job.attemptsMade,
progress: job.progress,
delay: job.delay,
created: DateTime.fromMillis(job.timestamp).toISO(),
processed: job.processedOn ? DateTime.fromMillis(job.processedOn).toISO() : undefined,
finished: job.finishedOn ? DateTime.fromMillis(job.finishedOn).toISO() : undefined
})
else cb(null)
})
}
}
init()
module.exports = ConversionService
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment