Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sequelize registration with timeout and retry #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ import sponsorshipPolicyRoutes from './routes/sponsorship-policy-routes.js';

let server: FastifyInstance;


async function registerWithRetry(server: FastifyInstance, plugin: any, retries: number, timeout: number) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const registerPromise = server.register(plugin);
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeout));
await Promise.race([registerPromise, timeoutPromise]);
console.log('Plugin registered successfully');
return;
} catch (error) {
if (attempt === retries) {
console.error('Failed to register plugin after maximum retries:', error);
throw error;
} else {
console.warn(`Attempt ${attempt} failed. Retrying...`);
}
}
}
}

const initializeServer = async (): Promise<void> => {

server = fastify({
Expand Down Expand Up @@ -69,7 +89,9 @@ const initializeServer = async (): Promise<void> => {
await server.register(sponsorshipPolicyRoutes);

// Register the sequelizePlugin
await server.register(sequelizePlugin);
//await server.register(sequelizePlugin);

await registerWithRetry(server, sequelizePlugin, 3, 5000); // 3 retries with a 5-second timeout

// Synchronize all models
await server.sequelize.sync();
Expand Down
Loading