All files / redis-smq-monitor/src/workers websocket-heartbeat-stream.worker.ts

94.11% Statements 16/17
66.66% Branches 4/6
100% Functions 7/7
100% Lines 16/16

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    57x 57x   57x       35x 35x     35x 30x     30x 30x   30x     30x 30x     30x       30x     30x                 57x  
import { TWebsocketHeartbeatOnlineIdsStreamPayload } from '../../types';
import { ICallback } from 'redis-smq-common/dist/types';
import { async, RedisClient, Worker } from 'redis-smq-common';
import { Consumer } from 'redis-smq';
 
export class WebsocketHeartbeatStreamWorker extends Worker {
  protected redisClient: RedisClient;
 
  constructor(redisClient: RedisClient, managed: boolean) {
    super(managed);
    this.redisClient = redisClient;
  }
 
  work = (cb: ICallback<void>): void => {
    const onlineIds: TWebsocketHeartbeatOnlineIdsStreamPayload = {
      consumers: [],
    };
    Consumer.getConsumerHeartbeats(this.redisClient, (err, reply) => {
      Iif (err) cb(err);
      else {
        async.each(
          reply ?? [],
          (item, _, done) => {
            onlineIds.consumers.push(item.consumerId);
            this.redisClient.publish(
              `streamConsumerHeartbeat:${item.consumerId}`,
              item.payload,
              () => done(),
            );
          },
          () => {
            this.redisClient.publish(
              `streamHeartbeatOnlineIds`,
              JSON.stringify(onlineIds),
              () => cb(),
            );
          },
        );
      }
    });
  };
}
 
export default WebsocketHeartbeatStreamWorker;