Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feature/eager init #74

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

Closed
fulleni wants to merge 16 commits into main from feature/eager-init
Closed

Feature/eager init #74

fulleni wants to merge 16 commits into main from feature/eager-init

Conversation

@fulleni
Copy link
Member

@fulleni fulleni commented Oct 19, 2025
edited
Loading

Status

READY

Description

This pull request significantly enhances the server's startup reliability and predictability by implementing an eager loading and fail-fast initialization strategy. By ensuring all essential services and connections are fully operational before the server processes any requests, the system gains improved stability and robustness. This change centralizes critical startup logic, preventing the server from ever running in a partially configured or compromised state.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

fulleni added 11 commits October 19, 2025 07:03
Introduces a custom `bin/main.dart` entrypoint to shift the application from a lazy-loading to an eager-loading model.
This new entrypoint ensures that all application dependencies are initialized upfront before the Dart Frog server starts. If any initialization step fails, the server process will exit immediately, providing a robust "fail-fast" startup sequence and eliminating race conditions.
Removes all lazy-loading and race-condition handling logic from the `AppDependencies` class. This includes the `_isInitialized` flag, error caching fields, and the `try-catch` block within the `init` method.
With the new eager-loading entrypoint, initialization is guaranteed to happen once at startup, making this complex state management logic redundant. The class is now a simpler, more direct dependency initializer.
Updates the root middleware to remove the `AppDependencies.instance.init()` call.
Since initialization is now handled eagerly at application startup by the new custom entrypoint, the middleware's responsibility is simplified to only providing the already-initialized dependencies into the request context.
Adds the `executables` section to `pubspec.yaml` to define the new `bin/main.dart` script as a named executable.
This allows the server to be started with `dart run :main`, which is the new standard for running the application with the eager-loading strategy.
Updates the README.md to include a new section under "Architecture & Infrastructure" that highlights the "Eager Loading & Fail-Fast Startup" pattern.
This change accurately reflects the recent architectural overhaul, explaining the benefits of initializing all dependencies before the server starts, which enhances robustness and stability.
Updates the changelog to include the recent architectural shift to an eager-loading startup model. This is marked as a breaking change (`!`) because it alters the command used to run the server.
Relocates the global logger configuration from the root middleware (`routes/_middleware.dart`) to the new eager-loading entrypoint (`bin/main.dart`).
This is an architectural improvement that ensures the logger is configured exactly once when the application process starts, rather than within a middleware that could be re-instantiated. It makes the startup sequence more robust and simplifies the root middleware's responsibilities.
Removes the `await` from the `server.main()` call in the custom entrypoint, as the function returns `void`. Also removes redundant `// ignore: avoid_print` comments that were duplicated by a file-level ignore, resolving linter warnings.
- Integrate shelf_hotreload package for hot reload capability
- Wrap server startup logic with withHotreload function
- Preserve eager initialization of dependencies
- Maintain "fail-fast" approach for production readiness
- Update server startup to use createServer function from Dart Frog
...patibility
- Update CHANGELOG.md to reflect the new hot reload compatibility of eager loading
- Enhance the description to include Dart Frog's hot reload feature
Removes the `reset()` static method and makes the `_instance` field `final` again. This logic was introduced to support hot reloading, which has since been removed from the custom entrypoint.
This change cleans up the `AppDependencies` class, removing dead code and preventing confusion about the server's startup behavior.
Copy link

Summary of Changes

Hello @fulleni, I'm Gemini Code Assist1 ! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the server's startup reliability and predictability by implementing an eager loading and fail-fast initialization strategy. By ensuring all essential services and connections are fully operational before the server processes any requests, the system gains improved stability and robustness. This change centralizes critical startup logic, preventing the server from ever running in a partially configured or compromised state.

Highlights

  • Eager Initialization: The application now employs an eager loading strategy, ensuring all critical dependencies, such as database connections, migrations, and seeding, are fully initialized before the server starts accepting requests.
  • Fail-Fast Startup: A robust 'fail-fast' mechanism has been implemented. If any part of the dependency initialization process encounters an error, the server will immediately terminate with a clear error message, preventing it from running in a broken or unstable state.
  • Refactored Dependency Management: The AppDependencies class and the root middleware have been streamlined. Idempotent initialization logic and error tracking within AppDependencies have been removed, as the init() method is now called once at application startup.
  • Custom Application Entrypoint: A new bin/main.dart file has been introduced as the custom entrypoint for the application. This file orchestrates the eager initialization process and the subsequent startup of the Dart Frog server.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully refactors the application to an eager-loading, fail-fast startup strategy, which is a significant improvement for robustness. The introduction of a custom entrypoint and the simplification of the dependency initialization logic are well-executed. My feedback includes two suggestions for the new bin/main.dart file to further improve its resilience by adding a graceful shutdown mechanism and making the logging more robust.

Implements two key production-readiness features based on code review:
1. **Graceful Shutdown:** The `main.dart` entrypoint now listens for `SIGINT` and `SIGTERM` signals. Upon receiving a signal, it closes the HTTP server to stop accepting new connections, disposes of all application dependencies (like the database connection), and then exits cleanly. This prevents resource leaks.
2. **Atomic Logging:** The logger configuration in `main.dart` has been refactored to use a `StringBuffer` and a single `stdout.write()` call. This ensures that multi-line log entries are written to the console atomically, preventing interleaved output and improving log readability.
A defensive check has also been added to `AppDependencies.dispose` to ensure it can be called safely even if initialization failed.
Copy link
Member Author

fulleni commented Oct 19, 2025

/gemini review

- Add a platform check before listening for SIGTERM signal
- Prevent errors on Windows platforms that don't support SIGTERM
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant and valuable architectural change by shifting to an eager, fail-fast server initialization strategy. The new custom entrypoint in bin/main.dart ensures all dependencies are ready before the server starts, improving reliability. The related refactoring in AppDependencies and _middleware.dart correctly simplifies the logic by removing the now-redundant lazy initialization checks. My review includes a couple of suggestions for the new entrypoint to enhance production logging and prevent a race condition that could lead to lost error messages on fatal startup failures.

Fixes a type error where `buildRootHandler()` was being inferred as `dynamic` instead of `Handler`. The fix explicitly casts the result to `Handler` before passing it to the `serve` function, resolving the static analysis error and ensuring type safety.
Replaces the asynchronous `log.severe` call in the main catch block with a direct, synchronous write to `stderr`, followed by `stderr.flush()`.
This fixes a race condition where the process could exit before the logger had a chance to write the fatal error message, ensuring that critical startup failure information is never lost.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer

@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /