-
Notifications
You must be signed in to change notification settings - Fork 11
refactor: optimize and schedule international price updates #45
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
base: main
Are you sure you want to change the base?
Conversation
Refactored updateInternationalPrices to fetch rates and offers in parallel and added error handling. Introduced a 12-hour interval to automatically update prices and ensured cleanup on SIGTERM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the international price update logic to improve performance and reliability through concurrent database queries, error handling, and automated scheduling. The changes optimize the existing update function and add process lifecycle management.
Key Changes:
- Concurrent fetching of currency rates and USD coin offers using
Promise.allinstead of sequential queries - Addition of try-catch error handling for the update operation
- Implementation of 12-hour automated updates with proper cleanup on SIGTERM
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/hooks.server.ts
Outdated
| for (const offer of templateOffers) { | ||
| await prisma.coinOffers.upsert({ |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sequential await inside nested loops creates N×M database queries. Consider batching all upsert operations and executing them with Promise.all outside the loops to significantly reduce database round-trips.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Addresses performance feedback from #45 where nested loops with sequential `await` create N×M database round-trips during international price updates. **Changes:** - Collect all upsert operations in an array during loop iteration - Execute batched operations with `Promise.all` after loop completion - Reduces database queries from N×M sequential to 2 initial + 1 parallel batch **Before:** ```typescript for (const { currency, rate } of rates) { for (const offer of templateOffers) { await prisma.coinOffers.upsert({ ... }); // N×M sequential queries } } ``` **After:** ```typescript const upsertOperations = []; for (const { currency, rate } of rates) { for (const offer of templateOffers) { upsertOperations.push(prisma.coinOffers.upsert({ ... })); } } await Promise.all(upsertOperations); // Single parallel batch ``` <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dudantas <8551443+dudantas@users.noreply.github.com>
This improves the reliability and performance of the international price update logic in
src/hooks.server.ts. The main changes include optimizing database queries, adding error handling, and automating periodic updates with proper cleanup on process termination.International price update improvements:
updateInternationalPricesfunction to fetch currency exchange rates and USD coin offers concurrently usingPromise.all, reducing redundant database queries and improving performance.Automation and process management:
updateInternationalPricesevery 12 hours, ensuring prices stay up-to-date automatically.SIGTERMsignal to clear the interval on process termination, preventing resource leaks.