All files / redis-smq-api/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 46 47  57x 57x 57x           57x 35x 30x     30x       30x   30x     30x 30x 30x     30x       30x     30x                   57x  
import { ICallback } from 'redis-smq/dist/types';
import { ConsumerHeartbeat } from 'redis-smq/dist/src/lib/consumer/consumer-heartbeat';
import { Worker } from 'redis-smq/dist/src/common/worker/worker';
import { each } from 'redis-smq/dist/src/util/async';
import {
  IMonitorWorkerParameters,
  TWebsocketHeartbeatOnlineIdsStreamPayload,
} from '../../types';
 
export class WebsocketHeartbeatStreamWorker extends Worker<IMonitorWorkerParameters> {
  work = (cb: ICallback<void>): void => {
    const onlineIds: TWebsocketHeartbeatOnlineIdsStreamPayload = {
      consumers: [],
    };
    ConsumerHeartbeat.getValidHeartbeats(
      this.redisClient,
      false,
      (err, reply) => {
        Iif (err) cb(err);
        else {
          each(
            reply ?? [],
            (item, _, done) => {
              const payload = String(item.payload);
              onlineIds.consumers.push(item.consumerId);
              this.redisClient.publish(
                `streamConsumerHeartbeat:${item.consumerId}`,
                payload,
                () => done(),
              );
            },
            () => {
              this.redisClient.publish(
                `streamHeartbeatOnlineIds`,
                JSON.stringify(onlineIds),
                () => cb(),
              );
            },
          );
        }
      },
    );
  };
}
 
export default WebsocketHeartbeatStreamWorker;