forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 118
improvement(core): handle more OS signals and broadcast them #2667
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16c26ac
handle more OS signals
onur-ozkan b5a8e29
drop `signal` feature from tokio
onur-ozkan 266d048
disable `spawn_os_signal_handler` for Windows
onur-ozkan 4c2b8a3
broadcast shutdown signals through streaming manager
onur-ozkan a35426f
apply nit
onur-ozkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
mm2src/mm2_main/src/rpc/streaming_activations/shutdown_signal.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //! RPC activation and deactivation for the shutdown signals. | ||
| use super::{EnableStreamingRequest, EnableStreamingResponse}; | ||
|
|
||
| use crate::shutdown_signal_event::ShutdownSignalEvent; | ||
| use common::HttpStatusCode; | ||
| use derive_more::Display; | ||
| use http::StatusCode; | ||
| use mm2_core::mm_ctx::MmArc; | ||
| use mm2_err_handle::{map_to_mm::MapToMmResult, mm_error::MmResult}; | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct EnableShutdownSignalRequest; | ||
|
|
||
| #[derive(Display, Serialize, SerializeErrorType)] | ||
| #[serde(tag = "error_type", content = "error_data")] | ||
| pub enum ShutdownSignalRequestError { | ||
| EnableError(String), | ||
| } | ||
|
|
||
| impl HttpStatusCode for ShutdownSignalRequestError { | ||
| fn status_code(&self) -> StatusCode { | ||
| StatusCode::BAD_REQUEST | ||
| } | ||
| } | ||
|
|
||
| pub async fn enable_shutdown_signal( | ||
| ctx: MmArc, | ||
| req: EnableStreamingRequest<EnableShutdownSignalRequest>, | ||
| ) -> MmResult<EnableStreamingResponse, ShutdownSignalRequestError> { | ||
| ctx.event_stream_manager | ||
| .add(req.client_id, ShutdownSignalEvent, ctx.spawner()) | ||
| .await | ||
| .map(EnableStreamingResponse::new) | ||
| .map_to_mm(|e| ShutdownSignalRequestError::EnableError(format!("{e:?}"))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use async_trait::async_trait; | ||
| use futures::channel::oneshot; | ||
| use futures::StreamExt; | ||
| use mm2_event_stream::{Broadcaster, Event, EventStreamer, StreamHandlerInput, StreamerId}; | ||
|
|
||
| pub struct ShutdownSignalEvent; | ||
|
|
||
| #[async_trait] | ||
| impl EventStreamer for ShutdownSignalEvent { | ||
| type DataInType = String; | ||
|
|
||
| fn streamer_id(&self) -> StreamerId { | ||
| StreamerId::ShutdownSignal | ||
| } | ||
|
|
||
| async fn handle( | ||
| self, | ||
| broadcaster: Broadcaster, | ||
| ready_tx: oneshot::Sender<Result<(), String>>, | ||
| mut data_rx: impl StreamHandlerInput<Self::DataInType>, | ||
| ) { | ||
| ready_tx | ||
| .send(Ok(())) | ||
| .expect("Receiver is dropped, which should never happen."); | ||
|
|
||
| while let Some(signal) = data_rx.next().await { | ||
| broadcaster.broadcast(Event::new(self.streamer_id(), json!(signal))); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
since we intend to handle any signal, shouldn't we init our signal hook with all signals (if there is a catch all thing).
not really a problem since we have the important signals covered, but looking at this, the
unknownsignal line would never be triggered (though i would prefer it like that instead ofunreachable!so i don't have a problem with that either).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.
No, not any signal. We can't handle every signal (e.g., we can't handle SIGKILL). I wanted to handle ones (the obvious ones) that can be handled.