Skip to content

Commit bc8a32b

Browse files
author
Andrew
committed
stage 2
1 parent 2fc96c5 commit bc8a32b

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
use jsonschema::JSONSchema;
55
use serde_json::Value;
6-
use std::{collections::HashMap, env, io::{Read, Write}};
6+
use std::{collections::HashMap, env};
77
use crate::{configure::{config_doc::ExecutionKind, {config_result::ResourceGetResult, parameters, Configurator}}, util::parse_input_to_json};
88
use crate::dscerror::DscError;
99
use super::{dscresource::get_diff, invoke_result::{ExportResult, GetResult, ResolveResult, SetResult, TestResult, ValidateResult, ResourceGetResponse, ResourceSetResponse, ResourceTestResponse, get_in_desired_state}, resource_manifest::{ArgKind, InputKind, Kind, ResourceManifest, ReturnKind, SchemaKind}};
1010
use tracing::{error, warn, info, debug, trace};
1111
use tokio::process::Command;
1212
use std::process::Stdio;
13-
use std::process::ExitStatus;
1413
use tokio::io::{BufReader, AsyncBufReadExt, AsyncWriteExt};
15-
use tokio::task::JoinError;
1614

1715
pub const EXIT_PROCESS_TERMINATED: i32 = 0x102;
1816

@@ -593,37 +591,43 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
593591
stdin.write(input.as_bytes()).await.expect("could not write to stdin");
594592
drop(stdin);
595593
}
596-
594+
597595
let child_id: u32 = match child.id() {
598596
Some(id) => id,
599597
None => {
600598
return Err(DscError::CommandOperation("Can't get child process id".to_string(), executable.to_string()));
601599
}
602600
};
603601

604-
// Ensure the child process is spawned in the runtime so it can
605-
// make progress on its own while we await for any output.
606-
let child_result:Result<ExitStatus, JoinError> = tokio::spawn(async {
607-
let status = child.wait_with_output().await;
608-
return status.unwrap().status
609-
}).await;
610-
611-
let mut stdout_result = String::with_capacity(1024*1024);
612-
while let Some(line) = stdout_reader.next_line().await? {
613-
stdout_result.push_str(&line);
614-
stdout_result.push('\n');
615-
}
602+
let child_task = tokio::spawn(async move {
603+
child.wait().await
604+
});
616605

617-
let mut filtered_stderr = String::with_capacity(1024*1024);
618-
while let Some(stderr_line) = stderr_reader.next_line().await? {
619-
let filtered_stderr_line = log_stderr_line(executable, &child_id, &stderr_line);
620-
if !filtered_stderr_line.is_empty() {
621-
filtered_stderr.push_str(filtered_stderr_line);
622-
filtered_stderr.push('\n');
606+
let stdout_task = tokio::spawn(async move {
607+
let mut stdout_result = String::with_capacity(1024*1024);
608+
while let Ok(Some(line)) = stdout_reader.next_line().await {
609+
stdout_result.push_str(&line);
610+
stdout_result.push('\n');
623611
}
624-
}
612+
return stdout_result
613+
});
614+
615+
let stderr_task = tokio::spawn(async move {
616+
let mut filtered_stderr = String::with_capacity(1024*1024);
617+
while let Ok(Some(stderr_line)) = stderr_reader.next_line().await {
618+
let filtered_stderr_line = log_stderr_line("pn", &child_id, &stderr_line);
619+
if !filtered_stderr_line.is_empty() {
620+
filtered_stderr.push_str(filtered_stderr_line);
621+
filtered_stderr.push('\n');
622+
}
623+
}
624+
return filtered_stderr
625+
});
626+
627+
let exit_code = child_task.await.unwrap()?.code();
628+
let stdout_result = stdout_task.await.unwrap();
629+
let stderr_result = stderr_task.await.unwrap();
625630

626-
let exit_code = child_result.unwrap().code();
627631
match exit_code {
628632
Some(code) => {
629633
debug!("Process '{executable}' id {child_id} exited with code {code}");
@@ -634,10 +638,10 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
634638
return Err(DscError::CommandExitFromManifest(executable.to_string(), code, error_message.to_string()));
635639
}
636640
}
637-
return Err(DscError::Command(executable.to_string(), code, filtered_stderr));
641+
return Err(DscError::Command(executable.to_string(), code, stderr_result));
638642
}
639643

640-
Ok((code, stdout_result, filtered_stderr)) },
644+
Ok((code, stdout_result, stderr_result)) },
641645
None => {
642646
debug!("Process '{executable}' id {child_id} terminated by signal");
643647
return Err(DscError::CommandOperation("Process terminated by signal".to_string(), executable.to_string()));

0 commit comments

Comments
 (0)