Skip to content

Conversation

dotansimha
Copy link
Member

@dotansimha dotansimha commented Sep 25, 2025

Can be easily used like that to create background tasks that are also linked to the lifetime of the server:

pub struct TestTask;

#[async_trait]
impl BackgroundTask for TestTask {
    fn id(&self) -> &str {
        "TestTask"
    }

    async fn run(&self, token: CancellationToken) {
        let mut interval = tokio::time::interval(Duration::from_secs(3));
        loop {
            tokio::select! {
                _ = interval.tick() => { println!("running"); }
                _ = token.cancelled() => { println!("Shutting down."); return; }
            }
        }
    }
}

// then later
manager.register_task(TestTask);

Copy link
Contributor

Summary of Changes

Hello @dotansimha, 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 introduces a new framework for managing background tasks within the router application. It provides a BackgroundTask trait and a BackgroundTasksManager to register and oversee long-running asynchronous operations, ensuring they are properly initialized and gracefully terminated in sync with the server's lifecycle. This enhancement improves the robustness and maintainability of background processes.

Highlights

  • New Background Task System: Introduced a new background_tasks module that provides a structured way to define and manage asynchronous background operations within the router.
  • Graceful Shutdown: Implemented a BackgroundTasksManager that uses tokio-util's CancellationToken to ensure all registered background tasks are gracefully shut down when the server stops.
  • Dependency Update: Added tokio-util as a new dependency to facilitate the implementation of cancellation tokens for background tasks.
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
Contributor

@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 simple and effective manager for background tasks, which is a great addition for handling long-running processes tied to the server's lifecycle. The use of CancellationToken for graceful shutdown is well-implemented. My review includes a few suggestions to enhance robustness by handling task panics, improve logging consistency, and correct a minor typo.

Comment on lines +47 to +49
join_all(self.task_handles).await;

println!("all background tasks have been shut down gracefully.");
Copy link
Contributor

Choose a reason for hiding this comment

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

high

There are a couple of improvements that can be made here:

  1. Handle Task Panics: The result of join_all is currently ignored. If a background task panics, it will result in a JoinError which will be silently swallowed. It's important to handle these results and log any panics to aid in debugging.
  2. Consistent Logging: The println! macro is used, but the rest of the project uses the tracing crate. For consistency and to benefit from structured logging, it's better to use info!.

This suggestion addresses both points.

        let join_results = join_all(self.task_handles).await;
        for result in join_results {
            if let Err(err) = result {
                error!("A background task panicked during shutdown: {:?}", err);
            }
        }

        info!("all background tasks have been shut down gracefully.");

.map_err(|err| err.into())
.map_err(|err| err.into());

info!("server stopped, clearning background tasks");
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There's a typo in the log message. "clearning" should be "cleaning".

Suggested change
info!("server stopped, clearning background tasks");
info!("server stopped, cleaning background tasks");

@kamilkisiela
Copy link
Contributor

Let's wait with merging this PR until we actually use it for something

@dotansimha dotansimha marked this pull request as draft September 25, 2025 16:10
@dotansimha dotansimha mentioned this pull request Sep 29, 2025
10 tasks
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.

2 participants