Skip to content

Commit

Permalink
fix: concurrent compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Oct 27, 2024
1 parent 14c5296 commit f79dfb9
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions packages/app/src/workers/circuit-generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Entry } from "@prisma/client";
import { getLogs } from "@/lib/models/logs";

const processing: string[] = [];
let forceStop = false;

async function generateCiruitService() {
while (true) {
Expand All @@ -23,44 +22,41 @@ async function generateCiruitService() {
}

const circuitName = (entry.parameters as any)['name'];
try {
processing.push(entry.slug);
await updateState(entry.id, "PROCESSING");
processing.push(entry.id);
(async () => {
// Use modal to compile circuit
await generateCodeLibrary(entry.parameters, entry.slug, entry.status)
await compileCircuitModal(entry.slug, circuitName, true);
await downloadZkey(entry.slug, circuitName);
await downloadBuiltCircuit(entry.slug, circuitName);
await updateState(entry.id, "COMPLETED", true);
} catch (e) {
if (await shouldRetryCompilation(entry.slug)) {
console.error("Retrying circuit compilation", e)
await updateState(entry.id, "PENDING");
} else {
console.error("Failed to compile circuit using modal", e)
await updateState(entry.id, "ERROR");
try {
await generateCodeLibrary(entry.parameters, entry.slug, entry.status)
await compileCircuitModal(entry.slug, circuitName, true);
await downloadZkey(entry.slug, circuitName);
await downloadBuiltCircuit(entry.slug, circuitName);
await updateState(entry.id, "COMPLETED", true);
} catch (e) {
if (await shouldRetryCompilation(entry.slug)) {
console.error("Retrying circuit compilation", e)
await updateState(entry.id, "PENDING");
} else {
console.error("Failed to compile circuit using modal", e)
await updateState(entry.id, "ERROR");
}
}
}
processing.splice(processing.indexOf(entry.slug), 1);
processing.splice(processing.indexOf(entry.id), 1);
})();
}
}

// Function to handle graceful exit
function handleGracefulExit() {
console.log('Received exit signal. Gracefully shutting down...');
if (forceStop) {
process.exit(0);
}
// Perform any cleanup tasks here if needed
if (processing.length > 0) {
console.log("Waiting for circuits to finish processing", processing);
console.log("Cancel again to force exit");
setInterval(() => {
console.log("Waiting for circuits to finish processing", processing);
console.log("Cancel again to force exit");
}, 10000);
forceStop = true;
} else {
process.exit(0);
const promises = [];
for (const id of processing) {
promises.push(updateState(id, "PENDING"))
}
Promise.all(promises).finally(() => {
process.exit(0);
});
}
}

Expand Down

0 comments on commit f79dfb9

Please sign in to comment.