-
Notifications
You must be signed in to change notification settings - Fork 8
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
Cleanup running containers on the Control-C signal #422
Open
ammernico
wants to merge
1
commit into
science-computing:master
Choose a base branch
from
ammernico:container-cleanup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 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 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 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 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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
use std::borrow::Borrow; | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
use std::process::ExitCode; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
|
||
|
@@ -32,8 +33,9 @@ use tokio::sync::mpsc::Receiver; | |
use tokio::sync::mpsc::Sender; | ||
use tokio::sync::RwLock; | ||
use tokio_stream::StreamExt; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::Instrument; | ||
use tracing::{debug, error, trace}; | ||
use tracing::{debug, error, info, trace}; | ||
use typed_builder::TypedBuilder; | ||
use uuid::Uuid; | ||
|
||
|
@@ -265,12 +267,26 @@ impl Borrow<ArtifactPath> for ProducedArtifact { | |
|
||
impl<'a> Orchestrator<'a> { | ||
pub async fn run(self, output: &mut Vec<ArtifactPath>) -> Result<HashMap<Uuid, Error>> { | ||
let (results, errors) = self.run_tree().await?; | ||
let cancellation_token = CancellationToken::new(); | ||
let controlc_cancellation_token = cancellation_token.clone(); | ||
|
||
tokio::spawn(async move { | ||
tokio::signal::ctrl_c().await.unwrap(); | ||
info!("Received the ctl-c signal, stopping..."); | ||
controlc_cancellation_token.cancel(); | ||
ExitCode::from(1) | ||
}); | ||
|
||
let (results, errors) = self.run_tree(cancellation_token).await?; | ||
|
||
output.extend(results); | ||
Ok(errors) | ||
} | ||
|
||
async fn run_tree(self) -> Result<(Vec<ArtifactPath>, HashMap<Uuid, Error>)> { | ||
async fn run_tree( | ||
self, | ||
token: CancellationToken, | ||
) -> Result<(Vec<ArtifactPath>, HashMap<Uuid, Error>)> { | ||
let prepare_span = tracing::debug_span!("run tree preparation"); | ||
|
||
// There is no async code until we drop this guard, so this is fine | ||
|
@@ -452,45 +468,55 @@ impl<'a> Orchestrator<'a> { | |
// The JobTask::run implementation handles the rest, we just have to wait for all futures | ||
// to succeed. | ||
let run_span = tracing::debug_span!("run"); | ||
let running_jobs = jobs | ||
.into_iter() | ||
.map(|prep| { | ||
trace!(parent: &run_span, job_uuid = %prep.1.jobdef.job.uuid(), "Creating JobTask"); | ||
// the sender is set or we need to use the root sender | ||
let sender = prep | ||
.3 | ||
.into_inner() | ||
.unwrap_or_else(|| vec![root_sender.clone()]); | ||
JobTask::new(prep.0, prep.1, sender) | ||
}) | ||
.inspect( | ||
|task| trace!(parent: &run_span, job_uuid = %task.jobdef.job.uuid(), "Running job"), | ||
) | ||
.map(|task| { | ||
task.run() | ||
.instrument(tracing::debug_span!(parent: &run_span, "JobTask::run")) | ||
}) | ||
.collect::<futures::stream::FuturesUnordered<_>>(); | ||
debug!("Built {} jobs", running_jobs.len()); | ||
|
||
running_jobs | ||
.collect::<Result<()>>() | ||
.instrument(run_span.clone()) | ||
.await?; | ||
trace!(parent: &run_span, "All jobs finished"); | ||
drop(run_span); | ||
|
||
match root_receiver.recv().await { | ||
None => Err(anyhow!("No result received...")), | ||
Some(Ok(results)) => { | ||
let results = results | ||
.into_iter() | ||
.flat_map(|tpl| tpl.1.into_iter()) | ||
.map(ProducedArtifact::unpack) | ||
.collect(); | ||
Ok((results, HashMap::with_capacity(0))) | ||
|
||
tokio::select! { | ||
_ = token.cancelled() => { | ||
anyhow::bail!("Received Control-C signal"); | ||
} | ||
r = async { | ||
let running_jobs = jobs | ||
.into_iter() | ||
.map(|prep| { | ||
trace!(parent: &run_span, job_uuid = %prep.1.jobdef.job.uuid(), "Creating JobTask"); | ||
// the sender is set or we need to use the root sender | ||
let sender = prep | ||
.3 | ||
.into_inner() | ||
.unwrap_or_else(|| vec![root_sender.clone()]); | ||
JobTask::new(prep.0, prep.1, sender) | ||
}) | ||
.inspect( | ||
|task| trace!(parent: &run_span, job_uuid = %task.jobdef.job.uuid(), "Running job"), | ||
) | ||
.map(|task| { | ||
task.run() | ||
.instrument(tracing::debug_span!(parent: &run_span, "JobTask::run")) | ||
}) | ||
.collect::<futures::stream::FuturesUnordered<_>>(); | ||
debug!("Built {} jobs", running_jobs.len()); | ||
|
||
running_jobs | ||
.collect::<Result<()>>() | ||
.instrument(run_span.clone()) | ||
.await?; | ||
trace!(parent: &run_span, "All jobs finished"); | ||
drop(run_span); | ||
|
||
match root_receiver.recv().await { | ||
None => Err(anyhow!("No result received...")), | ||
Some(Ok(results)) => { | ||
let results = results | ||
.into_iter() | ||
.flat_map(|tpl| tpl.1.into_iter()) | ||
.map(ProducedArtifact::unpack) | ||
.collect(); | ||
Ok((results, HashMap::with_capacity(0))) | ||
} | ||
Some(Err(errors)) => Ok((vec![], errors)), | ||
} | ||
} => { | ||
r | ||
} | ||
Some(Err(errors)) => Ok((vec![], errors)), | ||
Comment on lines
-455
to
-493
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: I still need to properly review this part. |
||
} | ||
} | ||
} | ||
|
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.
This approach seems fine but we never set
container_id
back toNone
for finished jobs - it would be more elegant if we could do so (after ensuring that the container has indeed exited but that might already be implemented to check if the job has finished).