tornado-relayer/src/queue.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-09-28 04:28:34 +02:00
const { v4: uuid } = require('uuid')
const Queue = require('bull')
2022-04-19 09:09:45 +02:00
const { netId } = require('./config')
2020-11-27 17:21:56 +01:00
const { status } = require('./constants')
const { redis, redisUrl } = require('./modules/redis')
2020-09-28 04:28:34 +02:00
2022-04-19 09:09:45 +02:00
const queue = new Queue(`proofs_${netId}`, redisUrl, {
2020-12-18 23:43:38 +01:00
lockDuration: 300000, // Key expiration time for job locks.
lockRenewTime: 30000, // Interval on which to acquire the job lock
stalledInterval: 30000, // How often check for stalled jobs (use 0 for never checking).
maxStalledCount: 3, // Max amount of times a stalled job will be re-processed.
guardInterval: 5000, // Poll interval for delayed jobs and added jobs.
retryProcessDelay: 5000, // delay before processing next job in case of internal error.
drainDelay: 5, // A timeout for when the queue is in drained state (empty waiting for jobs).
})
2020-09-28 04:28:34 +02:00
2020-10-02 14:09:33 +02:00
async function postJob({ type, request }) {
2020-09-28 04:28:34 +02: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 14:09:33 +02:00
...request, // proof, args, ?contract
2020-09-28 04:28:34 +02:00
},
2020-12-18 23:43:38 +01:00
{
//removeOnComplete: true
},
2020-09-28 04:28:34 +02:00
)
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-12-18 23:43:38 +01:00
if (!job) {
return null
}
2020-10-02 14:09:33 +02:00
return {
...job.data,
failedReason: job.failedReason,
}
2020-09-28 04:28:34 +02:00
}
module.exports = {
postJob,
getJob,
getJobStatus,
queue,
}