2020-09-28 05:28:34 +03:00
|
|
|
const { v4: uuid } = require('uuid')
|
|
|
|
const Queue = require('bull')
|
|
|
|
const Redis = require('ioredis')
|
2020-10-06 14:20:26 +03:00
|
|
|
const { redisUrl } = require('./config')
|
2020-11-27 17:21:56 +01:00
|
|
|
const { status } = require('./constants')
|
2020-09-28 05:28:34 +03:00
|
|
|
const redis = new Redis(redisUrl)
|
|
|
|
|
|
|
|
const queue = new Queue('proofs', redisUrl)
|
|
|
|
|
2020-10-02 15:09:33 +03:00
|
|
|
async function postJob({ type, request }) {
|
2020-09-28 05:28:34 +03:00
|
|
|
const id = uuid()
|
|
|
|
|
|
|
|
const job = await queue.add(
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
type,
|
2020-11-27 17:21:56 +01:00
|
|
|
status: status.QUEUED,
|
2020-10-02 15:09:33 +03:00
|
|
|
...request, // proof, args, ?contract
|
2020-09-28 05:28:34 +03:00
|
|
|
},
|
|
|
|
// { removeOnComplete: true },
|
|
|
|
)
|
|
|
|
await redis.set(`job:${id}`, job.id)
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getJob(uuid) {
|
|
|
|
const id = await redis.get(`job:${uuid}`)
|
|
|
|
return queue.getJobFromId(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getJobStatus(uuid) {
|
|
|
|
const job = await getJob(uuid)
|
2020-10-02 15:09:33 +03:00
|
|
|
return {
|
|
|
|
...job.data,
|
|
|
|
failedReason: job.failedReason,
|
|
|
|
}
|
2020-09-28 05:28:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
postJob,
|
|
|
|
getJob,
|
|
|
|
getJobStatus,
|
|
|
|
queue,
|
|
|
|
}
|