Skip to content

Commit c4695ba

Browse files
committed
fixes
1 parent 53af516 commit c4695ba

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

js/botasaurus-server-js/src/api-config.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,46 @@ function isWorker() {
6969
}
7070

7171
/**
72-
* Check master endpoint health
72+
* Check master endpoint health, retrying up to 3 times.
7373
*/
74-
async function checkMasterHealth(masterEndpoint: string){
75-
try {
76-
const response = await fetch(`${masterEndpoint}/health`, {
77-
method: 'GET',
78-
signal: AbortSignal.timeout(10000) // 10 second timeout
79-
});
80-
81-
if (!response.ok) {
82-
throw new Error(`Health check returned status ${response.status}`);
74+
async function checkMasterHealth(masterEndpoint: string) {
75+
const MAX_RETRIES = 3;
76+
let retryCount = 0;
77+
while (retryCount < MAX_RETRIES) {
78+
retryCount++;
79+
try {
80+
const response = await fetch(`${masterEndpoint}/health`, {
81+
method: 'GET',
82+
signal: AbortSignal.timeout(10000) // 10 second timeout
83+
});
84+
85+
if (!response.ok) {
86+
throw new Error(`Health check returned status ${response.status}`);
87+
}
88+
if (retryCount > 1) {
89+
console.debug(`[K8s] Successfully reached master after retry (attempt ${retryCount}/${MAX_RETRIES}): ${masterEndpoint}`);
90+
}
91+
return true;
92+
} catch (error: any) {
93+
console.error(error);
94+
console.debug(`[K8s] Health check error (attempt ${retryCount}/${MAX_RETRIES}): ${error instanceof Error ? error.message : error} - ${masterEndpoint}`);
95+
if (retryCount < MAX_RETRIES) {
96+
// Wait briefly before retrying
97+
await sleep(3);
98+
continue;
99+
}
83100
}
84-
return true;
85-
} catch (error: any) {
86-
console.error(`[K8s] Cannot reach master at ${masterEndpoint}`);
87-
console.error(`[K8s] Error: ${error.message}`);
101+
}
102+
103+
console.error(`[K8s] Failed to reach master after ${MAX_RETRIES} attempts: ${masterEndpoint}`);
104+
if (!ApiConfig.isMasterNode()) {
105+
// If not master node, exit the process
88106
process.exit(1);
89-
return false;
90107
}
108+
return false;
91109
}
92110

111+
93112
function addCorsHeaders(reply: any) {
94113
reply.header("Access-Control-Allow-Origin", "*");
95114
reply.header(
@@ -413,7 +432,11 @@ export function buildApp(
413432
routeAliases: any,
414433
enable_cache: boolean
415434
): FastifyInstance {
416-
const app = fastify({ logger: true });
435+
const app = fastify({
436+
logger: true,
437+
// TODO: change appropriately as needed
438+
bodyLimit: 500 * 1024 * 1024 // 500MB
439+
});
417440

418441
// Add CORS handling
419442
app.addHook("onRequest", (request, reply, done) => {

js/botasaurus-server-js/src/ndjson.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,13 @@ export class NDJSONWriteStream {
2929

3030
performWrite(content: any): void | PromiseLike<void> {
3131
return new Promise((resolve) => {
32-
const attemptWrite = (content: string) => {
33-
if (!this.writeStream.write(content)) {
34-
35-
// Wait for the 'drain' event if write returns false
36-
this.writeStream.once('drain', () => {
37-
// console.log('Buffer drained. Resuming writing.');
38-
// attemptWrite(); // Retry writing the data
39-
resolve() // Resolve the promise if write is successful
40-
})
41-
} else {
32+
if (!this.writeStream.write(content)) {
33+
this.writeStream.once('drain', () => {
4234
resolve() // Resolve the promise if write is successful
43-
}
35+
})
36+
} else {
37+
resolve() // Resolve the promise if write is successful
4438
}
45-
attemptWrite(content)
4639
})
4740
}
4841

0 commit comments

Comments
 (0)