2020-10-29 01:36:05 -04:00
|
|
|
import Bull from 'bull';
|
|
|
|
import { Client } from '.';
|
|
|
|
import { ScoreInterface } from '../models';
|
2020-10-28 19:44:31 -04:00
|
|
|
|
2020-10-29 01:36:05 -04:00
|
|
|
export default class Queue {
|
2020-10-28 19:44:31 -04:00
|
|
|
public client: Client;
|
|
|
|
|
2020-10-29 01:36:05 -04:00
|
|
|
public queues: { score: Bull.Queue };
|
2020-10-28 19:44:31 -04:00
|
|
|
|
2020-10-29 01:36:05 -04:00
|
|
|
constructor(client: Client) {
|
2020-10-28 19:44:31 -04:00
|
|
|
this.client = client;
|
2020-10-29 01:36:05 -04:00
|
|
|
this.queues = {
|
|
|
|
score: new Bull('score', { limiter: { duration: 5500, max: 5 } }),
|
|
|
|
};
|
|
|
|
this.setProcessors();
|
2020-10-28 19:44:31 -04:00
|
|
|
}
|
|
|
|
|
2020-10-29 01:36:05 -04:00
|
|
|
protected listeners() {
|
|
|
|
this.queues.score.on('active', (job) => {
|
|
|
|
this.client.util.signale.pending(`${job.id} has become active.`);
|
2020-10-28 19:44:31 -04:00
|
|
|
});
|
2020-10-29 01:36:05 -04:00
|
|
|
this.queues.score.on('completed', (job) => {
|
|
|
|
this.client.util.signale.success(`Job with id ${job.id} has been completed`);
|
2020-10-28 19:44:31 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-29 01:36:05 -04:00
|
|
|
protected setProcessors() {
|
|
|
|
this.queues.score.process('score::update', async (job: Bull.Job<{ score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number }>) => {
|
|
|
|
await this.client.db.Score.updateOne({ userID: job.data.score.userID }, { $set: { total: job.data.total, activity: job.data.activity, roles: job.data.roles, moderation: job.data.moderation, cloudServices: job.data.cloudServices, other: job.data.other, staff: job.data.staff, lastUpdate: new Date() } });
|
|
|
|
if (!job.data.score.pin || job.data.score.pin?.length < 1) {
|
|
|
|
await this.client.db.Score.updateOne({ userID: job.data.score.userID }, { $set: { pin: [this.client.util.randomNumber(100, 999), this.client.util.randomNumber(10, 99), this.client.util.randomNumber(1000, 9999)] } });
|
|
|
|
}
|
|
|
|
});
|
2020-10-28 19:44:31 -04:00
|
|
|
}
|
|
|
|
|
2020-10-29 01:36:05 -04:00
|
|
|
public updateScore(score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number) {
|
|
|
|
this.queues.score.add('score::update', { score, total, activity, roles, moderation, cloudServices, other, staff });
|
2020-10-28 19:44:31 -04:00
|
|
|
}
|
|
|
|
}
|