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: gracefully shutdown command when shutdown signal is received #493

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

Robert-maker1994
Copy link
Contributor

@Robert-maker1994 Robert-maker1994 commented Feb 3, 2025

Checklist

Related issue

Fixes: #469
Currently, the dev CLI panics when a user hits CTRL+C.

Overview

Implemented an receiver shutdown to handle async jobs, plus wait till all the process have stopped.
I have created a mpsc::channel to send a shutdown signal to the main watch function, this singal triggers a graceful shutdown of both the rust server and the SSR bundle build.

I have used tokio::spawn ensureing a shutdown process that is executed asynchronously, with explicitly stopping the running server and the ssr bundle.

@github-actions github-actions bot added the rust Requires rust knowledge label Feb 3, 2025
@Robert-maker1994 Robert-maker1994 changed the title Feat(crates\tuono\src\watch): Shutdown gracefully. feat: Shutdown gracefully. Feb 3, 2025
@Robert-maker1994 Robert-maker1994 marked this pull request as draft February 4, 2025 11:33
@Robert-maker1994 Robert-maker1994 marked this pull request as ready for review February 4, 2025 12:14
crates/tuono/src/watch.rs Outdated Show resolved Hide resolved
build_ssr_bundle.stop().await;
run_server.stop().await;
});
action.quit_gracefully(Signal::Interrupt, std::time::Duration::from_secs(9999));
Copy link
Member

Choose a reason for hiding this comment

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

crates/tuono/src/watch.rs Outdated Show resolved Hide resolved
crates/tuono/src/watch.rs Outdated Show resolved Hide resolved
@@ -116,6 +114,7 @@ pub async fn watch() -> Result<()> {
// watch the current directory
wx.config.pathset(["./src"]);

let _ = wx.main().await.into_diagnostic()?;
let _ = wx.main().await.into_diagnostic();
Copy link
Member

Choose a reason for hiding this comment

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

Why we remove here the error propagation?

@marcalexiei marcalexiei changed the title feat: Shutdown gracefully. feat: gracefully shutdown command when interrupt signal is received Feb 5, 2025
@marcalexiei marcalexiei changed the title feat: gracefully shutdown command when interrupt signal is received feat: gracefully shutdown command when shutdown signal is received Feb 5, 2025
rust_server.delete();
build_ssr_bundle.delete();
action.quit_gracefully(Signal::Interrupt, std::time::Duration::from_secs(9999));
eprintln!("Tuono gracefully shutting down...");
Copy link
Member

Choose a reason for hiding this comment

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

There is no need for the users to be informed about this process

Suggested change
eprintln!("Tuono gracefully shutting down...");

@Valerioageno
Copy link
Member

@jacobhq are you interested in taking a look?

@jacobhq
Copy link
Contributor

jacobhq commented Feb 16, 2025

@jacobhq are you interested in taking a look?

Yeah if needed. Is there any specific thing that we're stuck on?

@Valerioageno
Copy link
Member

Nono the PR is ready. I was asking if you are interested in reviewing it

action.quit();
rust_server.delete();
build_ssr_bundle.delete();
action.quit_gracefully(Signal::Interrupt, std::time::Duration::from_secs(9999));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should have a shorter delay maybe like ~30s.

Copy link
Member

@marcalexiei marcalexiei left a comment

Choose a reason for hiding this comment

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

I tested the patch on this branch on my MacBook Air (M3) and the issue is still present.
I always get tokio-runtime-worker panic error when pressing CTRL + C

Attached screenshot

Screenshot 2025-02-17 at 18 26 10

I'm using examples/tuono-tutorial folder as test case.
Am I missing something 😅?

@Robert-maker1994
Copy link
Contributor Author

Robert-maker1994 commented Feb 17, 2025

@marcalexiei Interesting, it should work out of the box. We may be getting different results, as I'm running Windows 11. However, if the problem persists, could someone investigate it further?

I would handle this use case as I did previously by adding a select! block to catch the Ctrl+C signal.

@jacobhq
Copy link
Contributor

jacobhq commented Feb 17, 2025

I will take a look, as I am also on Win11 :)

@marcalexiei
Copy link
Member

Maybe this can be related: https://docs.rs/tokio/latest/tokio/signal/fn.ctrl_c.html#caveats

@jacobhq
Copy link
Contributor

jacobhq commented Feb 17, 2025

I still get the panic too:

tuono\examples\tuono-app on  feat/gracefull-shutdown is 📦 v0.0.1 via ⬢ v22.12.0 via 🦀 v1.83.0 
❯ ../../target/debug/tuono.exe dev

  ⚡ Tuono v0.17.7
  Ready at: http://localhost:3000

thread 'tokio-runtime-worker' panicked at C:\Users\jacob\.cargo\registry\src\index.crates.io-6f17d22bba15001f\watchexec-supervisor-3.0.0\src\job\task.rs:54:17:
all branches are disabled and there is no else branch
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

There are some open tasks from code review. Please address those (they may resolve this issue, since we may be stopping stuff twice at the moment), and then see if you can fix this.

Happy to help if needed, but please finish code review first :)

@github-actions github-actions bot added typescript Requires typescript knowledge CI/CD repo maintenance linting, fs organization, that sort of stuff labels Feb 17, 2025
@Robert-maker1994

This comment was marked as duplicate.

@Robert-maker1994
Copy link
Contributor Author

Robert-maker1994 commented Feb 17, 2025

I merged with main and now all of the changed files on main are now on this PR now :( Just an FYI - not to sure how to get rid of them all.

@jacobhq I did add the resolve the comments about the duration to 30 seconds on it killed the remaining job that I missed. However, I'm still receiving the panic from the open jobs.

Copy link
Contributor

@jacobhq jacobhq left a comment

Choose a reason for hiding this comment

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

You're in a tricky place.

I would suggest reverting cc78c84 with git reset HEAD~1 --hard (undoes last commit and deletes changes). Then use update your fork by pulling tuono-labs/tuono main (or Github UI). Let me know if you need help with that.

git merge main when on this branch to bring in changes. Please don't rebase, as you'll rewrite history, and there's a lot of code review that would be messed up. Resolve the conflicts and commit.

After you have merged main, address code review in a separate commit.

I did add the resolve the comments about the duration to 30 seconds on it killed the remaining job that I missed. However, I'm still receiving the panic from the open jobs.

I will take a look tomorrow. Probably priority is to merge main at the moment.

@github-actions github-actions bot removed typescript Requires typescript knowledge CI/CD repo maintenance linting, fs organization, that sort of stuff labels Feb 18, 2025
@Robert-maker1994
Copy link
Contributor Author

@jacobhq Thanks for you're help, that was a right PITA! Everything is in order now, if you view all the commits and not individually, you shall see my changes.

Copy link
Contributor

@jacobhq jacobhq left a comment

Choose a reason for hiding this comment

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

Thanks for this. Can confirm this works on my machine.

action.quit();
rust_server.delete();
build_ssr_bundle.delete();
build_rust_src.delete();
Copy link
Contributor

Choose a reason for hiding this comment

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

Using .delete() does work here although I don't think it gracefully shuts down the jobs. @Valerioageno WDYT?

@@ -21,7 +20,6 @@ const DEV_SSR_BIN_SRC: &str = "node_modules\\.bin\\tuono-dev-ssr.cmd";
const DEV_WATCH_BIN_SRC: &str = "node_modules/.bin/tuono-dev-watch";
#[cfg(not(target_os = "windows"))]
const DEV_SSR_BIN_SRC: &str = "node_modules/.bin/tuono-dev-ssr";

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add this new line back in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust Requires rust knowledge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request]: gracefully shutdown CLI watch script
4 participants