Skip to content

Commit 266a4fb

Browse files
committed
performance improvements
1 parent 7f7a579 commit 266a4fb

File tree

22 files changed

+2921
-119
lines changed

22 files changed

+2921
-119
lines changed

apps/webapp/app/entry.server.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ import { logger } from "./services/logger.server";
240240
import { Prisma } from "./db.server";
241241
import { registerRunEngineEventBusHandlers } from "./v3/runEngineHandlers.server";
242242
import { remoteBuildsEnabled } from "./v3/remoteImageBuilder.server";
243+
import { resourceMonitor } from "./services/resourceMonitor.server";
243244

244245
if (env.EVENT_LOOP_MONITOR_ENABLED === "1") {
245246
eventLoopMonitor.enable();
@@ -250,3 +251,7 @@ if (remoteBuildsEnabled()) {
250251
} else {
251252
console.log("🏗️ Local builds enabled");
252253
}
254+
255+
if (env.RESOURCE_MONITOR_ENABLED === "1") {
256+
resourceMonitor.startMonitoring(1000);
257+
}

apps/webapp/app/env.server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ const EnvironmentSchema = z
492492
CENTS_PER_RUN: z.coerce.number().default(0),
493493

494494
EVENT_LOOP_MONITOR_ENABLED: z.string().default("1"),
495+
RESOURCE_MONITOR_ENABLED: z.string().default("0"),
495496
MAXIMUM_LIVE_RELOADING_EVENTS: z.coerce.number().int().default(1000),
496497
MAXIMUM_TRACE_SUMMARY_VIEW_COUNT: z.coerce.number().int().default(25_000),
497498
MAXIMUM_TRACE_DETAILED_SUMMARY_VIEW_COUNT: z.coerce.number().int().default(10_000),
@@ -1122,7 +1123,8 @@ const EnvironmentSchema = z
11221123
EVENTS_CLICKHOUSE_FLUSH_INTERVAL_MS: z.coerce.number().int().default(1000),
11231124
EVENT_REPOSITORY_CLICKHOUSE_ROLLOUT_PERCENT: z.coerce.number().optional(),
11241125
EVENT_REPOSITORY_DEFAULT_STORE: z.enum(["postgres", "clickhouse"]).default("postgres"),
1125-
EVENTS_CLICKHOUSE_MAX_TRACE_SUMMARY_VIEW_COUNT: z.coerce.number().int().default(250_000),
1126+
EVENTS_CLICKHOUSE_MAX_TRACE_SUMMARY_VIEW_COUNT: z.coerce.number().int().default(25_000),
1127+
EVENTS_CLICKHOUSE_MAX_TRACE_DETAILED_SUMMARY_VIEW_COUNT: z.coerce.number().int().default(5_000),
11261128

11271129
// Bootstrap
11281130
TRIGGER_BOOTSTRAP_ENABLED: z.string().default("0"),

apps/webapp/app/eventLoopMonitor.server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ function after(asyncId: number) {
6565
cached.parentCtx
6666
);
6767

68+
console.log("🥸 Event loop blocked", { asyncType: cached.type, time });
69+
6870
newSpan.end();
6971
}
7072
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ResourceMonitor } from "@trigger.dev/core/v3/serverOnly";
2+
import { singleton } from "~/utils/singleton";
3+
4+
export const resourceMonitor = singleton("resourceMonitor", initializeResourceMonitor);
5+
6+
function initializeResourceMonitor() {
7+
return new ResourceMonitor({
8+
ctx: {},
9+
verbose: false,
10+
});
11+
}

apps/webapp/app/v3/dynamicFlushScheduler.server.ts

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Logger } from "@trigger.dev/core/logger";
2+
import { tryCatch } from "@trigger.dev/core/utils";
23
import { nanoid } from "nanoid";
34
import pLimit from "p-limit";
45
import { signalsEmitter } from "~/services/signals.server";
@@ -195,47 +196,62 @@ export class DynamicFlushScheduler<T> {
195196
// Schedule all batches for concurrent processing
196197
const flushPromises = batchesToFlush.map((batch) =>
197198
this.limiter(async () => {
198-
const flushId = nanoid();
199199
const itemCount = batch.length;
200200

201-
try {
202-
const startTime = Date.now();
203-
await this.callback(flushId, batch);
204-
205-
const duration = Date.now() - startTime;
206-
this.totalQueuedItems -= itemCount;
207-
this.consecutiveFlushFailures = 0;
208-
this.lastFlushTime = Date.now();
209-
this.metrics.flushedBatches++;
210-
this.metrics.totalItemsFlushed += itemCount;
211-
212-
this.logger.debug("Batch flushed successfully", {
213-
flushId,
214-
itemCount,
215-
duration,
216-
remainingQueueDepth: this.totalQueuedItems,
217-
activeConcurrency: this.limiter.activeCount,
218-
pendingConcurrency: this.limiter.pendingCount,
219-
});
220-
} catch (error) {
221-
this.consecutiveFlushFailures++;
222-
this.metrics.failedBatches++;
201+
const self = this;
202+
203+
async function tryFlush(flushId: string, batchToFlush: T[], attempt: number = 1) {
204+
try {
205+
const startTime = Date.now();
206+
await self.callback(flushId, batchToFlush);
207+
208+
const duration = Date.now() - startTime;
209+
self.totalQueuedItems -= itemCount;
210+
self.consecutiveFlushFailures = 0;
211+
self.lastFlushTime = Date.now();
212+
self.metrics.flushedBatches++;
213+
self.metrics.totalItemsFlushed += itemCount;
214+
215+
self.logger.debug("Batch flushed successfully", {
216+
flushId,
217+
itemCount,
218+
duration,
219+
remainingQueueDepth: self.totalQueuedItems,
220+
activeConcurrency: self.limiter.activeCount,
221+
pendingConcurrency: self.limiter.pendingCount,
222+
});
223+
} catch (error) {
224+
self.consecutiveFlushFailures++;
225+
self.metrics.failedBatches++;
226+
227+
self.logger.error("Error attempting to flush batch", {
228+
flushId,
229+
itemCount,
230+
error,
231+
consecutiveFailures: self.consecutiveFlushFailures,
232+
attempt,
233+
});
234+
235+
// Back off on failures
236+
if (self.consecutiveFlushFailures > 5) {
237+
self.adjustConcurrency(true);
238+
}
239+
240+
if (attempt <= 3) {
241+
await new Promise((resolve) => setTimeout(resolve, 500));
242+
return await tryFlush(flushId, batchToFlush, attempt + 1);
243+
} else {
244+
throw error;
245+
}
246+
}
247+
}
248+
249+
const [flushError] = await tryCatch(tryFlush(nanoid(), batch));
223250

251+
if (flushError) {
224252
this.logger.error("Error flushing batch", {
225-
flushId,
226-
itemCount,
227-
error,
228-
consecutiveFailures: this.consecutiveFlushFailures,
253+
error: flushError,
229254
});
230-
231-
// Re-queue the batch at the front if it fails
232-
this.batchQueue.unshift(batch);
233-
this.totalQueuedItems += itemCount;
234-
235-
// Back off on failures
236-
if (this.consecutiveFlushFailures > 3) {
237-
this.adjustConcurrency(true);
238-
}
239255
}
240256
})
241257
);

0 commit comments

Comments
 (0)