Skip to content

Commit e14f62d

Browse files
fix: memory leak ? (#2172)
1 parent 1e9cc03 commit e14f62d

4 files changed

Lines changed: 19 additions & 26 deletions

File tree

apps/workflows/src/cron/checker.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
import { regionDict } from "@openstatus/regions";
2929
import { db } from "../lib/db";
3030

31-
import { getSentry } from "@hono/sentry";
3231
import { getLogger } from "@logtape/logtape";
3332
import type { monitorPeriodicitySchema } from "@openstatus/db/src/schema/constants";
3433
import {
@@ -37,7 +36,6 @@ import {
3736
type tpcPayloadSchema,
3837
transformHeaders,
3938
} from "@openstatus/utils";
40-
import type { Context } from "hono";
4139
import { env } from "../env";
4240

4341
type TaskInput = {
@@ -64,8 +62,7 @@ const client = new CloudTasksClient({
6462

6563
export async function sendCheckerTasks(
6664
periodicity: z.infer<typeof monitorPeriodicitySchema>,
67-
c: Context,
68-
) {
65+
): Promise<{ success: number; failed: number }> {
6966
const parent = client.queuePath(
7067
env().GCP_PROJECT_ID,
7168
env().GCP_LOCATION,
@@ -121,7 +118,7 @@ export async function sendCheckerTasks(
121118

122119
if (monitors.data.length === 0) {
123120
logger.info("No monitors to check", { periodicity });
124-
return;
121+
return { success: 0, failed: 0 };
125122
}
126123

127124
// Batch fetch all monitor statuses in a single query (N+1 fix)
@@ -235,11 +232,9 @@ export async function sendCheckerTasks(
235232
failed_count: failed,
236233
success_count: success,
237234
});
238-
getSentry(c).captureMessage(
239-
`sendCheckerTasks for ${periodicity} ended with ${failed} failed tasks`,
240-
"error",
241-
);
242235
}
236+
237+
return { success, failed };
243238
}
244239
// timestamp needs to be in ms
245240
const createCronTask = async (

apps/workflows/src/cron/external-status.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { db } from "../lib/db";
1313

1414
const logger = getLogger(["workflow", "external-status"]);
1515

16+
const tb = new OSTinybird(env().TINY_BIRD_API_KEY);
17+
1618
function toStatusPageEntry(row: ExternalServiceRow): StatusPageEntry {
1719
return {
1820
id: row.slug,
@@ -58,7 +60,6 @@ export async function runExternalStatusTick(): Promise<{
5860
failureCount: number;
5961
total: number;
6062
}> {
61-
const tb = new OSTinybird(env().TINY_BIRD_API_KEY);
6263
const services = await listExternalServices({ ctx: { db } });
6364

6465
const entries = services.map(toStatusPageEntry);

apps/workflows/src/cron/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,27 @@ app.get("/checker/:period", async (c) => {
4040

4141
void Effect.runPromise(
4242
Effect.tryPromise({
43-
try: () => sendCheckerTasks(schema.data, c),
43+
try: () => sendCheckerTasks(schema.data),
4444
catch: (e) => new Error(`Error in /checker/${period} cron: ${e}`),
4545
}).pipe(
4646
Effect.retry({
4747
times: 3,
4848
schedule: Schedule.exponential("1000 millis"),
4949
}),
50-
Effect.tap(() =>
51-
Effect.sync(() =>
50+
Effect.tap((result) =>
51+
Effect.sync(() => {
52+
if (result.failed > 0) {
53+
sentry.captureMessage(
54+
`sendCheckerTasks for ${period} ended with ${result.failed} failed tasks`,
55+
"error",
56+
);
57+
}
5258
sentry.captureCheckIn({
5359
checkInId,
5460
monitorSlug: period,
5561
status: "ok",
56-
}),
57-
),
62+
});
63+
}),
5864
),
5965
Effect.catchAll((e) =>
6066
Effect.sync(() => {

apps/workflows/src/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function shouldSample(event: Record<string, unknown>): boolean {
4747
if (statusCode && statusCode >= 500) return true;
4848

4949
// Always capture: explicit errors
50-
if (event.error) return true;
50+
if (event.outcome === "error") return true;
5151

5252
// Always capture: slow requests (above p99 - 2s threshold)
5353
if (durationMs && durationMs > 2000) return true;
@@ -138,16 +138,7 @@ app.use("*", async (c, next) => {
138138
const duration = Date.now() - startTime;
139139

140140
event.status_code = c.res.status;
141-
if (c.error) {
142-
event.outcome = "error";
143-
event.error = {
144-
type: c.error.name,
145-
message: c.error.message,
146-
stack: c.error.stack,
147-
};
148-
} else {
149-
event.outcome = "success";
150-
}
141+
event.outcome = c.error ? "error" : "success";
151142
event.duration_ms = duration;
152143
// Emit canonical log line with all context (wide event pattern)
153144
if (shouldSample(event)) {

0 commit comments

Comments
 (0)