Skip to content

Commit

Permalink
feat: deliver workflow outcomes to cloud (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Jul 10, 2024
1 parent e1bcf7e commit f80c503
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
5 changes: 2 additions & 3 deletions crates/cli/src/commands/apply_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ pub(crate) async fn run_apply_migration(
arg: ApplyMigrationArgs,
flags: &GlobalFormatFlags,
) -> Result<()> {
use crate::workflows::display_workflow_outcome;

let input = arg.get_payload()?;

let format = OutputFormat::from(flags);
Expand Down Expand Up @@ -94,8 +92,9 @@ pub(crate) async fn run_apply_migration(
)
.await?;

// Note the workflow may have already emitted its own conclusion - this is a fallback
emitter.finish_workflow(&outcome)?;
emitter.flush().await?;

display_workflow_outcome(outcome)
Ok(())
}
46 changes: 46 additions & 0 deletions crates/cli/src/result_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ pub fn print_file_header(
Ok(())
}

fn get_pretty_workflow_message(
outcome: &marzano_messenger::workflows::PackagedWorkflowOutcome,
) -> String {
match outcome.success {
true => {
format!(
"✅ {}",
outcome
.message
.as_deref()
.unwrap_or("Workflow completed successfully")
)
}
false => {
format!(
"❌ {}",
outcome.message.as_deref().unwrap_or("Workflow failed")
)
}
}
}

impl fmt::Display for FormattedResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -290,6 +312,8 @@ pub struct FormattedMessager<'a> {
total_rejected: usize,
total_supressed: usize,
input_pattern: String,

workflow_done: bool,
}

impl<'a> FormattedMessager<'_> {
Expand All @@ -307,6 +331,7 @@ impl<'a> FormattedMessager<'_> {
total_rejected: 0,
total_supressed: 0,
input_pattern,
workflow_done: false,
}
}
}
Expand Down Expand Up @@ -361,6 +386,27 @@ impl Messager for FormattedMessager<'_> {
Ok(())
}

fn finish_workflow(
&mut self,
outcome: &marzano_messenger::workflows::PackagedWorkflowOutcome,
) -> anyhow::Result<()> {
if self.workflow_done {
// If we already finished once, short-circuit it
return Ok(());
}

if let Some(writer) = &mut self.writer {
let mut writer = writer.lock().map_err(|_| anyhow!("Output lock poisoned"))?;
writeln!(writer, "{}", get_pretty_workflow_message(outcome))?;
} else {
log::info!("{}", get_pretty_workflow_message(outcome));
}

self.workflow_done = true;

Ok(())
}

fn emit_log(&mut self, log: &marzano_messenger::SimpleLogMessage) -> anyhow::Result<()> {
if let Some(writer) = &mut self.writer {
let mut writer = writer.lock().map_err(|_| anyhow!("Output lock poisoned"))?;
Expand Down
15 changes: 0 additions & 15 deletions crates/cli/src/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,6 @@ where
}
}

pub fn display_workflow_outcome(outcome: PackagedWorkflowOutcome) -> Result<()> {
match outcome.success {
true => {
log::info!(
"{}",
outcome
.message
.unwrap_or("Workflow completed successfully".to_string())
);
Ok(())
}
false => anyhow::bail!(outcome.message.unwrap_or("Workflow failed".to_string())),
}
}

#[cfg(feature = "remote_workflows")]
pub async fn run_remote_workflow(
workflow_name: String,
Expand Down
3 changes: 2 additions & 1 deletion crates/marzano_messenger/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ pub trait Messager: Send + Sync {
Ok(())
}

// Finish workflow, with a message
// Called when a workflow finishes processing, with the outcome
// Note that this *may* be called multiple times. The *first* time it is called should be considered the "true" outcome.
fn finish_workflow(&mut self, _outcome: &PackagedWorkflowOutcome) -> anyhow::Result<()> {
// do nothing
Ok(())
Expand Down

0 comments on commit f80c503

Please sign in to comment.