fix bug for eventListeners

This commit is contained in:
Roman Storm 2019-12-12 10:56:58 -08:00
parent 7be718d773
commit 08a55d6dd7

@ -10,16 +10,17 @@ const { redisClient, redisOpts } = require('./redis')
const { web3, fetcher } = require('./instances') const { web3, fetcher } = require('./instances')
const withdrawQueue = new Queue('withdraw', redisOpts) const withdrawQueue = new Queue('withdraw', redisOpts)
async function relayController(req, resp) { const reponseCbs = {}
let requestJob
let respLambda = (job, { msg, status }) => { let respLambda = (job, { msg, status }) => {
console.log('response', job.id, requestJob.id) const resp = reponseCbs[job.id]
if(requestJob.id === job.id) {
resp.status(status).json(msg) resp.status(status).json(msg)
withdrawQueue.removeListener('completed', respLambda) delete reponseCbs[job.id]
}
} }
withdrawQueue.on('completed', respLambda) withdrawQueue.on('completed', respLambda)
async function relayController(req, resp) {
let requestJob
const { proof, args, contract } = req.body const { proof, args, contract } = req.body
let { valid , reason } = isValidProof(proof) let { valid , reason } = isValidProof(proof)
if (!valid) { if (!valid) {
@ -60,15 +61,16 @@ async function relayController(req, resp) {
await redisClient.set('foo', 'bar') await redisClient.set('foo', 'bar')
requestJob = await withdrawQueue.add({ requestJob = await withdrawQueue.add({
contract, nullifierHash, root, proof, args, currency, amount, fee: fee.toString(), recipient contract, nullifierHash, root, proof, args, currency, amount, fee: fee.toString()
}, { removeOnComplete: true }) }, { removeOnComplete: true })
console.log('id', requestJob.id) reponseCbs[requestJob.id] = resp
} }
withdrawQueue.process(async function(job, done){ withdrawQueue.process(async function(job, done){
console.log(Date.now(), ' withdraw started', job.id) console.log(Date.now(), ' withdraw started', job.id)
const gasPrices = fetcher.gasPrices const gasPrices = fetcher.gasPrices
const { contract, nullifierHash, root, proof, args, refund, currency, amount, fee, recipient } = job.data const { contract, nullifierHash, root, proof, args, refund, currency, amount, fee } = job.data
// cb()
// job.data contains the custom data passed when the job was created // job.data contains the custom data passed when the job was created
// job.id contains id of this job. // job.id contains id of this job.
try { try {