Skip to content

Conversation

@dudantas
Copy link
Collaborator

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:

  • Optimized the updateInternationalPrices function to fetch currency exchange rates and USD coin offers concurrently using Promise.all, reducing redundant database queries and improving performance.
  • Added a try/catch block with error logging to handle failures during international price updates, increasing robustness.

Automation and process management:

  • Set up a periodic interval to call updateInternationalPrices every 12 hours, ensuring prices stay up-to-date automatically.
  • Added a listener for the SIGTERM signal to clear the interval on process termination, preventing resource leaks.

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.
Copilot AI review requested due to automatic review settings October 20, 2025 17:05
Copy link

Copilot AI left a 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.all instead 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.

Comment on lines 29 to 30
for (const offer of templateOffers) {
await prisma.coinOffers.upsert({
Copy link

Copilot AI Oct 20, 2025

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.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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>
Copy link

Copilot AI commented Nov 14, 2025

@dudantas I've opened a new pull request, #46, to work on those changes. Once the pull request is ready, I'll request review from you.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants