geo bufer

This commit is contained in:
Alex Holliday committed 2026-02-24 18:44:47 +00:00
1 parent 705ecf41c2
commit 5a74411ea2
3 files changed
+44 -15

No files matched your search

-2
View File
@@ -106,9 +106,7 @@ const GeoCheckSchema = new Schema<GeoCheckDocument>(
GeoCheckSchema.index({ "metadata.monitorId": 1, createdAt: -1 });
GeoCheckSchema.index({ "metadata.monitorId": 1, createdAt: 1 });
GeoCheckSchema.index({ "metadata.monitorId": 1, "results.location.continent": 1, createdAt: -1 });
GeoCheckSchema.index({ "metadata.teamId": 1, createdAt: -1 });
GeoCheckSchema.index({ "metadata.teamId": 1, "results.status": 1, createdAt: -1 });
GeoCheckSchema.index({ createdAt: 1 });
const GeoCheckModel = model<GeoCheckDocument>("GeoCheck", GeoCheckSchema);
@@ -1,5 +1,6 @@
import type { Check } from "@/types/index.js";
import type { GeoCheck } from "@/types/index.js";
import type Logger from "@/utils/logger.js";
const SERVICE_NAME = "BufferService";
@@ -9,12 +10,13 @@ export interface IBufferService {
removeCheckFromBuffer(check: Check): boolean;
scheduleNextFlush(): void;
flushBuffer(): Promise<void>;
flushGeoBuffer(): Promise<void>;
}
class BufferService implements IBufferService {
static SERVICE_NAME = SERVICE_NAME;
private BUFFER_TIMEOUT: number;
private logger: any;
private logger: Logger;
private SERVICE_NAME: string;
private buffer: any[];
private geoBuffer: any[];
@@ -28,7 +30,7 @@ class BufferService implements IBufferService {
geoChecksService,
settingsService,
}: {
logger: any;
logger: Logger;
checkService: any;
geoChecksService?: any;
settingsService: any;
@@ -123,6 +125,7 @@ class BufferService implements IBufferService {
this.bufferTimer = setTimeout(async () => {
try {
await this.flushBuffer();
await this.flushGeoBuffer();
} catch (error: any) {
this.logger.error({
message: `Error in flush cycle: ${error.message}`,
@@ -139,23 +142,47 @@ class BufferService implements IBufferService {
async flushBuffer() {
try {
if (this.buffer.length > 0) {
this.logger.debug({
message: `Flushing ${this.buffer.length} checks to database`,
service: this.SERVICE_NAME,
method: "flushBuffer",
});
await this.checksService.createChecks(this.buffer);
}
if (this.geoBuffer.length > 0 && this.geoChecksService) {
await this.geoChecksService.createGeoChecks(this.geoBuffer);
this.buffer = [];
}
} catch (error: any) {
this.logger.error({
message: error.message,
message: `Error flushing checks buffer: ${error.message}`,
service: this.SERVICE_NAME,
method: "flushBuffer",
stack: error.stack,
});
// Clear buffer even on error to prevent infinite retry loops
this.buffer = [];
}
}
this.buffer = [];
this.geoBuffer = [];
async flushGeoBuffer() {
try {
if (this.geoBuffer.length > 0) {
this.logger.debug({
message: `Flushing ${this.geoBuffer.length} geo checks to database`,
service: this.SERVICE_NAME,
method: "flushGeoBuffer",
});
await this.geoChecksService.createGeoChecks(this.geoBuffer);
this.geoBuffer = [];
}
} catch (error: any) {
this.logger.error({
message: `Error flushing geo checks buffer: ${error.message}`,
service: this.SERVICE_NAME,
method: "flushGeoBuffer",
stack: error.stack,
});
// Clear buffer even on error to prevent infinite retry loops
this.geoBuffer = [];
}
}
}
@@ -1,4 +1,5 @@
import type { GeoContinent, GeoCheckResult, GeoCheckTimings, GeoCheckLocation } from "@/types/geoCheck.js";
import type Logger from "@/utils/logger.js";
import got from "got";
const SERVICE_NAME = "GlobalPingService";
@@ -56,9 +57,9 @@ export interface IGlobalPingService {
class GlobalPingService implements IGlobalPingService {
static SERVICE_NAME = SERVICE_NAME;
private logger: any;
private logger: Logger;
constructor({ logger }: { logger: any }) {
constructor({ logger }: { logger: Logger }) {
this.logger = logger;
}
@@ -68,9 +69,12 @@ class GlobalPingService implements IGlobalPingService {
async createMeasurement(url: string, locations: GeoContinent[]): Promise<string | null> {
try {
// GlobalPing API expects target without protocol (http:// or https://)
const cleanTarget = url.replace(/^https?:\/\//, "");
const requestBody: GlobalPingMeasurementRequest = {
type: "http",
target: url,
target: cleanTarget,
locations: locations.map((continent) => ({ continent })),
limit: locations.length,
};
@@ -96,7 +100,7 @@ class GlobalPingService implements IGlobalPingService {
message: "GlobalPing API unavailable, skipping geo check",
service: SERVICE_NAME,
method: "createMeasurement",
details: error.message,
details: error,
});
return null;
}