Skip to content

Instantly share code, notes, and snippets.

@miladr0
Created October 17, 2020 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miladr0/4f0cceebe98be87468e03c271c250233 to your computer and use it in GitHub Desktop.
Save miladr0/4f0cceebe98be87468e03c271c250233 to your computer and use it in GitHub Desktop.
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