@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 77 lines 2.07 KB
Raw
1 import {
2 TelemetryMetricPayloadBody,
3 TelemetryPayload,
4 } from '../services/telemetry/TelemetryService.types';
5
6 export class BatchProcessor {
7 private batch: TelemetryMetricPayloadBody = [];
8 private time: number;
9 private batchSize: number;
10 private processBatchCallback: (data: TelemetryMetricPayloadBody) => Promise<void>;
11 private timer: ReturnType<typeof setTimeout> | null = null;
12 private pendingBatches: Set<Promise<void>> = new Set();
13
14 constructor(
15 time: number = 2000,
16 batchSize: number = 100,
17 processBatchCallback: (data: TelemetryMetricPayloadBody) => Promise<void>
18 ) {
19 this.batch = [];
20 this.time = time;
21 this.batchSize = batchSize;
22 this.processBatchCallback = processBatchCallback;
23 }
24
25 pushItem(item: TelemetryPayload) {
26 this.batch.push(item);
27 if (this.batch.length >= this.batchSize) {
28 this.processBatch();
29 } else if (!this.timer) {
30 this.timer = setTimeout(() => this.processBatch(), this.time);
31 }
32 }
33
34 processBatch() {
35 if (this.batch.length > 0) {
36 const batchToProcess = this.batch;
37 this.batch = [];
38
39 const pending = new Promise<void>(resolve => {
40 const run = () => {
41 Promise.resolve()
42 .then(() => this.processBatchCallback(batchToProcess))
43 .catch(() => {
44 // Silently ignore errors - they should be handled by the callback
45 })
46 .finally(resolve);
47 };
48
49 if (typeof queueMicrotask === 'function') {
50 queueMicrotask(run);
51 } else {
52 setTimeout(run, 0);
53 }
54 });
55
56 this.pendingBatches.add(pending);
57 pending.finally(() => {
58 this.pendingBatches.delete(pending);
59 });
60 }
61 if (this.timer) {
62 clearTimeout(this.timer);
63 this.timer = null;
64 }
65 }
66
67 /**
68 * Flush any pending batches and wait for all of them to complete.
69 * Useful for ensuring telemetry is sent before process exit.
70 */
71 async flush(): Promise<void> {
72 this.processBatch();
73 if (this.pendingBatches.size > 0) {
74 await Promise.all(this.pendingBatches);
75 }
76 }
77 }