Sharkey/packages/backend/src/server/api/stream/channels/queue-stats.ts

67 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-17 07:42:13 +02:00
import Xev from 'xev';
2022-09-17 20:27:08 +02:00
import { Inject, Injectable } from '@nestjs/common';
import { bindThis } from '@/decorators.js';
import Channel from '../channel.js';
2019-03-10 11:16:33 +01:00
2022-04-17 07:42:13 +02:00
const ev = new Xev();
2019-03-10 11:16:33 +01:00
2022-09-17 20:27:08 +02:00
class QueueStatsChannel extends Channel {
2019-03-10 11:16:33 +01:00
public readonly chName = 'queueStats';
public static shouldShare = true;
public static requireCredential = false;
constructor(id: string, connection: Channel['connection']) {
super(id, connection);
//this.onStats = this.onStats.bind(this);
//this.onMessage = this.onMessage.bind(this);
}
@bindThis
2019-03-10 11:16:33 +01:00
public async init(params: any) {
ev.addListener('queueStats', this.onStats);
}
@bindThis
2019-03-10 11:16:33 +01:00
private onStats(stats: any) {
this.send('stats', stats);
}
@bindThis
2019-03-10 11:16:33 +01:00
public onMessage(type: string, body: any) {
switch (type) {
case 'requestLog':
ev.once(`queueStatsLog:${body.id}`, statsLog => {
this.send('statsLog', statsLog);
});
ev.emit('requestQueueStatsLog', {
id: body.id,
2021-12-09 15:58:30 +01:00
length: body.length,
2019-03-10 11:16:33 +01:00
});
break;
}
}
@bindThis
2019-03-10 11:16:33 +01:00
public dispose() {
ev.removeListener('queueStats', this.onStats);
}
}
2022-09-17 20:27:08 +02:00
@Injectable()
export class QueueStatsChannelService {
public readonly shouldShare = QueueStatsChannel.shouldShare;
public readonly requireCredential = QueueStatsChannel.requireCredential;
constructor(
) {
}
@bindThis
2022-09-17 20:27:08 +02:00
public create(id: string, connection: Channel['connection']): QueueStatsChannel {
return new QueueStatsChannel(
id,
connection,
);
}
}