Skip to content

Commit 31c660d

Browse files
stop pinging when stop is called
1 parent 2a23803 commit 31c660d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/lib/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify';
44
import { readFileSync } from 'fs'
55
import { Context } from '../types.js'
66
import { validateSnapshot } from './schemaValidation.js'
7+
import { pingIntervalId } from './utils.js';
78

89
export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {
910

@@ -71,6 +72,11 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
7172
}
7273
let resp = await ctx.client.getS3PreSignedURL(ctx);
7374
await ctx.client.uploadLogs(ctx, resp.data.url);
75+
76+
if (pingIntervalId !== null) {
77+
clearInterval(pingIntervalId);
78+
ctx.log.debug('Ping polling stopped immediately.');
79+
}
7480
replyCode = 200;
7581
replyBody = { data: { message: "success", type: "DELETE" } };
7682
} catch (error: any) {

src/lib/utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ export async function startPolling(ctx: Context): Promise<void> {
305305
}, 5000);
306306
}
307307

308+
export let pingIntervalId: NodeJS.Timeout | null = null;
309+
308310
export async function startPingPolling(ctx: Context): Promise<void> {
309311
try {
310312
ctx.log.debug('Sending initial ping to server...');
@@ -314,14 +316,17 @@ export async function startPingPolling(ctx: Context): Promise<void> {
314316
ctx.log.error(`Error during initial ping: ${error.message}`);
315317
}
316318

317-
setInterval(async () => {
319+
// Start the polling interval
320+
pingIntervalId = setInterval(async () => {
318321
try {
319322
ctx.log.debug('Sending ping to server...');
320323
await ctx.client.ping(ctx.build.id, ctx.log);
321324
ctx.log.debug('Ping sent successfully.');
322325
} catch (error: any) {
323326
ctx.log.error(`Error during ping polling: ${error.message}`);
324327
}
325-
}, 10 * 60 * 1000);
328+
}, 10 * 60 * 1000); // 10 minutes interval
326329
}
327330

331+
332+

0 commit comments

Comments
 (0)