Created
October 17, 2020 17:30
-
-
Save miladr0/4f0cceebe98be87468e03c271c250233 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Queue from 'bull'; | |
import models from '../mongoose'; | |
import opts from '../lib/redisConnection'; | |
const hitApiQueue = new Queue('last-login', opts); | |
hitApiQueue.process(async (job) => { | |
try { | |
const { apiUrl } = job.data; | |
const result = await models.HitApi.findOneAndUpdate({ api: apiUrl }, { | |
$inc: { count: 1 } | |
}); | |
return Promise.resolve({ result }); | |
} catch (error) { | |
Promise.reject(error); | |
} | |
}); | |
const hitApi = async (req, res, next) => { | |
hitApiQueue.add({ apiUrl: req.originalUrl }); | |
next(); | |
}; | |
export default hitApi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment