Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-03-24 - Optimize Gateway API Fetch Concurrency in Astro API routes
**Learning:** Initiating slow external network requests sequentially after local database queries and filesystem checks creates a bottleneck in Astro API route response times.
**Action:** When an API handler requires both external fetches and local data, start the external fetch as a Promise at the very beginning of the handler (with a dummy catch to prevent unhandled rejection crashes), execute the local operations, and then `await` the Promise at the end to parallelize the workloads.
6 changes: 5 additions & 1 deletion src/pages/api/dashboard-projects-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function mapSessionToRunState(raw: Record<string, unknown>): { running: boolean
/** GET — état synthétique pour la carte Projets du dashboard (poll léger). */
export const GET: APIRoute = async ({ locals }) => {
const email = locals.user?.email as string | undefined;
const zimaosSessionPromise = fetchZimaOSSessionsPayload(email);
// Attach a dummy catch handler immediately to prevent UnhandledPromiseRejection
// if the fetch fails before we await it later in the final try/catch block.
zimaosSessionPromise.catch(() => {});
Comment on lines +33 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of attaching a dummy .catch(() => {}) handler that does nothing, it is cleaner and more robust to catch the rejection and return a fallback ZimaOSSessionsPayloadResult object. This prevents await zimaosSessionPromise from throwing an exception later in the handler, allowing the error to be handled gracefully through the existing oc.ok and oc.error checks rather than relying on the outer try/catch block.

  const zimaosSessionPromise = fetchZimaOSSessionsPayload(email).catch((err) => ({
    ok: false,
    status: 500,
    data: null,
    error: err instanceof Error ? err.message : String(err),
    attempts: [],
  }));


const payload: {
projects: Array<{
Expand Down Expand Up @@ -150,7 +154,7 @@ export const GET: APIRoute = async ({ locals }) => {
}

try {
const oc = await fetchZimaOSSessionsPayload(email);
const oc = await zimaosSessionPromise;
payload.swarm.zimaosOk = oc.ok;
const sessions = oc.ok
? (normalizeZimaOSSessions(oc.data) as Record<string, unknown>[])
Expand Down