All files / redis-smq-monitor-app/src/services queues.service.ts

100% Statements 26/26
100% Branches 1/1
100% Functions 10/10
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8457x                   57x     57x 57x           5x 5x 5x       2x           2x 2x       1x 1x       2x       1x 1x             3x 3x             1x 1x             3x 3x       15x 5x 5x 5x   15x      
import { promisifyAll } from 'bluebird';
import { TQueueParams } from 'redis-smq/dist/types';
import { DeleteQueueRequestDTO } from '../controllers/api/namespaces/queue/delete-queue/delete-queue.request.DTO';
import { GetNamespaceQueuesRequestDTO } from '../controllers/api/namespaces/get-namespace-queues/get-namespace-queues.request.DTO';
import { DeleteNamespaceRequestDTO } from '../controllers/api/namespaces/delete-namespace/delete-namespace.request.DTO';
import { SetRateLimitRequestDTO } from '../controllers/api/namespaces/queue/rate-limiting/set-rate-limit/set-rate-limit.request.DTO';
import { ClearRateLimitRequestDTO } from '../controllers/api/namespaces/queue/rate-limiting/clear-rate-limit/clear-rate-limit.request.DTO';
import { GetRateLimitRequestDTO } from '../controllers/api/namespaces/queue/rate-limiting/get-rate-limit/get-rate-limit.request.DTO';
import { QueueManager } from 'redis-smq';
import { TConfig } from '../../types';
import { getQueueManagerInstance } from '../lib/queue-manager';
import { TRegistry } from '../registry';
 
export class QueuesService {
  protected static instance: QueuesService | null = null;
  protected queue;
  protected namespace;
  protected queueRateLimit;
 
  protected constructor(queueManager: QueueManager) {
    this.queue = promisifyAll(queueManager.queue);
    this.namespace = promisifyAll(queueManager.namespace);
    this.queueRateLimit = promisifyAll(queueManager.queueRateLimit);
  }
 
  async getNamespaces(): Promise<string[]> {
    return this.namespace.listAsync();
  }
 
  async getNamespaceQueues(
    args: GetNamespaceQueuesRequestDTO,
  ): Promise<TQueueParams[]> {
    const { ns } = args;
    return this.namespace.getQueuesAsync(ns);
  }
 
  async deleteNamespace(args: DeleteNamespaceRequestDTO): Promise<void> {
    const { ns } = args;
    return this.namespace.deleteAsync(ns);
  }
 
  async getQueues(): Promise<TQueueParams[]> {
    return this.queue.listAsync();
  }
 
  async deleteQueue(args: DeleteQueueRequestDTO): Promise<void> {
    const { ns, queueName } = args;
    return this.queue.deleteAsync({
      name: queueName,
      ns,
    });
  }
 
  async setQueueRateLimit(args: SetRateLimitRequestDTO) {
    const { ns, queueName, interval, limit } = args;
    return this.queueRateLimit.setAsync(
      { name: queueName, ns },
      { interval, limit },
    );
  }
 
  async clearQueueRateLimit(args: ClearRateLimitRequestDTO) {
    const { ns, queueName } = args;
    return this.queueRateLimit.clearAsync({
      name: queueName,
      ns,
    });
  }
 
  async getQueueRateLimit(args: GetRateLimitRequestDTO) {
    const { ns, queueName } = args;
    return this.queueRateLimit.getAsync({ name: queueName, ns });
  }
 
  static async getInstance(registry: TRegistry) {
    if (!QueuesService.instance) {
      const config = registry.getItem('config');
      const queueManager = await getQueueManagerInstance(config);
      QueuesService.instance = new QueuesService(queueManager);
    }
    return QueuesService.instance;
  }
}