2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-17 07:42:13 +02:00
|
|
|
import Xev from 'xev';
|
2023-02-16 15:09:41 +01:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2024-08-09 09:04:41 +02:00
|
|
|
import { isJsonObject } from '@/misc/json-value.js';
|
2024-07-18 13:04:23 +02:00
|
|
|
import type { JsonObject, JsonValue } from '@/misc/json-value.js';
|
2023-12-27 07:08:59 +01:00
|
|
|
import Channel, { type MiChannelService } 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;
|
2023-12-27 07:08:59 +01:00
|
|
|
public static requireCredential = false as const;
|
2019-03-10 11:16:33 +01:00
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
constructor(id: string, connection: Channel['connection']) {
|
|
|
|
|
super(id, connection);
|
2022-12-04 07:03:09 +01:00
|
|
|
//this.onStats = this.onStats.bind(this);
|
|
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2022-02-27 03:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-07-18 13:04:23 +02:00
|
|
|
public async init(params: JsonObject) {
|
2019-03-10 11:16:33 +01:00
|
|
|
ev.addListener('queueStats', this.onStats);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-07-18 13:04:23 +02:00
|
|
|
private onStats(stats: JsonObject) {
|
2019-03-10 11:16:33 +01:00
|
|
|
this.send('stats', stats);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-07-18 13:04:23 +02:00
|
|
|
public onMessage(type: string, body: JsonValue) {
|
2019-03-10 11:16:33 +01:00
|
|
|
switch (type) {
|
|
|
|
|
case 'requestLog':
|
2024-08-09 09:04:41 +02:00
|
|
|
if (!isJsonObject(body)) return;
|
2024-07-18 13:04:23 +02:00
|
|
|
if (typeof body.id !== 'string') return;
|
|
|
|
|
if (typeof body.length !== 'number') return;
|
2019-03-10 11:16:33 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@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()
|
2023-12-27 07:08:59 +01:00
|
|
|
export class QueueStatsChannelService implements MiChannelService<false> {
|
2022-09-17 20:27:08 +02:00
|
|
|
public readonly shouldShare = QueueStatsChannel.shouldShare;
|
|
|
|
|
public readonly requireCredential = QueueStatsChannel.requireCredential;
|
2023-12-27 07:08:59 +01:00
|
|
|
public readonly kind = QueueStatsChannel.kind;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public create(id: string, connection: Channel['connection']): QueueStatsChannel {
|
|
|
|
|
return new QueueStatsChannel(
|
|
|
|
|
id,
|
|
|
|
|
connection,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|