feat(orchestrator): add recent events API and monitor script
This commit is contained in:
@@ -4,11 +4,13 @@ import { ValkeyService } from "../../valkey/valkey.service";
|
||||
import type { EventHandler, OrchestratorEvent } from "../../valkey/types";
|
||||
|
||||
type UnsubscribeFn = () => void;
|
||||
const MAX_RECENT_EVENTS = 500;
|
||||
|
||||
@Injectable()
|
||||
export class AgentEventsService implements OnModuleInit {
|
||||
private readonly logger = new Logger(AgentEventsService.name);
|
||||
private readonly subscribers = new Map<string, EventHandler>();
|
||||
private readonly recentEvents: OrchestratorEvent[] = [];
|
||||
private connected = false;
|
||||
|
||||
constructor(private readonly valkeyService: ValkeyService) {}
|
||||
@@ -18,6 +20,7 @@ export class AgentEventsService implements OnModuleInit {
|
||||
|
||||
await this.valkeyService.subscribeToEvents(
|
||||
(event) => {
|
||||
this.appendRecentEvent(event);
|
||||
this.subscribers.forEach((handler) => {
|
||||
void handler(event);
|
||||
});
|
||||
@@ -67,4 +70,20 @@ export class AgentEventsService implements OnModuleInit {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
getRecentEvents(limit = 100): OrchestratorEvent[] {
|
||||
const safeLimit = Math.min(Math.max(Math.floor(limit), 1), MAX_RECENT_EVENTS);
|
||||
if (safeLimit >= this.recentEvents.length) {
|
||||
return [...this.recentEvents];
|
||||
}
|
||||
|
||||
return this.recentEvents.slice(-safeLimit);
|
||||
}
|
||||
|
||||
private appendRecentEvent(event: OrchestratorEvent): void {
|
||||
this.recentEvents.push(event);
|
||||
if (this.recentEvents.length > MAX_RECENT_EVENTS) {
|
||||
this.recentEvents.shift();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user