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

reorganize #7

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions src/bin/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ async fn main() {
let token = CancellationToken::new();
tokio::spawn(listen_for_shutdown(token.clone()));

run(command, interval, timeout, &token, working_directory).await;
schedule(command, interval, timeout, &token, working_directory).await;
}

async fn run(
async fn schedule(
mut command: Command,
interval: u64,
timeout: u64,
Expand All @@ -55,20 +55,14 @@ async fn run(
let instant = tokio::select! {
instant = clock.tick() => { instant }
_ = token.cancelled() => {
println!("Stopping");
log("Stopping");
return
}
};
let name = instant.duration_since(start).as_secs();
let run_directory = working_directory.join(format!("{name}"));
let run_directory = name_run_directory(start, instant, &working_directory);
command = setup(command, &run_directory);
let mut status = create_file(&run_directory.join("status")).unwrap();
println!("Starting");
let mut child = command.spawn().expect("Failed to execute command");
let outcome = wait_with_output(&mut child, timeout, token).await;
status
.write_all(format!("{:?}", outcome).as_bytes())
.unwrap();
let outcome = wait_with_output(&mut command, timeout, token).await;
persist(&outcome, &run_directory);
}
}

Expand All @@ -80,6 +74,22 @@ struct WithPath {
path: PathBuf,
}

fn log(message: &str) {
println!("{}", message);
}

fn name_run_directory(start: Instant, instant: Instant, working_directory: &Path) -> PathBuf {
let name = instant.duration_since(start).as_secs();
working_directory.join(format!("{name}"))
}

fn persist(outcome: &Outcome, run_directory: &Path) {
let mut status = create_file(&run_directory.join("status")).unwrap();
status
.write_all(format!("{:?}", outcome).as_bytes())
.unwrap();
}

fn create_file(path: &Path) -> Result<File, WithPath> {
File::create(path).map_err(|error| WithPath {
error,
Expand Down Expand Up @@ -117,22 +127,28 @@ enum Outcome {
Complete(Status),
}

async fn wait_with_output(child: &mut Child, timeout: u64, token: &CancellationToken) -> Outcome {
async fn wait_with_output(
command: &mut Command,
timeout: u64,
token: &CancellationToken,
) -> Outcome {
log("Starting");
let mut child = command.spawn().expect("Failed to execute command");
tokio::select! {
output = child.wait() => {
Outcome::Complete(output)
}
_ = time::sleep(Duration::from_secs(timeout)) => {
Outcome::TimedOut(kill(child).await)
Outcome::TimedOut(kill(&mut child).await)
}
_ = token.cancelled() => {
Outcome::Cancelled(kill(child).await)
Outcome::Cancelled(kill(&mut child).await)
}
}
}

async fn kill(child: &mut Child) -> Status {
println!("KILLING");
log("KILLING");

#[cfg(target_os = "linux")]
sys::linux::interrupt(child);
Expand Down