Skip to content

Commit

Permalink
fix(clusters): fix linode cleanup logic (#1034)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Aug 9, 2024
1 parent 6e79bc7 commit f7d021c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
14 changes: 14 additions & 0 deletions lib/chirp-workflow/core/src/ctx/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,20 @@ impl WorkflowCtx {
tokio::task::spawn(async move { closure(f).execute(&mut ctx).await })
}

/// Tests if the given error is unrecoverable. If it is, allows the user to run recovery code safely.
/// Should always be used when trying to handle activity errors manually.
pub fn catch_unrecoverable<T>(&mut self, res: GlobalResult<T>) -> GlobalResult<GlobalResult<T>> {
match res {
Err(err) if !err.is_workflow_recoverable() => {
self.location_idx += 1;

Ok(Err(err))
}
Err(err) => Err(err),
Ok(x) => Ok(Ok(x)),
}
}

/// Sends a signal.
pub async fn signal<T: Signal + Serialize>(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion lib/chirp-workflow/core/src/db/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ impl Database for DatabasePostgres {
sqlx::query(indoc!(
"
INSERT INTO db_workflow.workflow_message_send_events(
workflow_id, location, tags, message_name, body
workflow_id, location, tags, message_name, body, loop_location
)
VALUES($1, $2, $3, $4, $5, $6)
RETURNING 1
Expand Down
10 changes: 6 additions & 4 deletions svc/pkg/linode/src/workflows/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ pub struct Input {
#[workflow]
pub async fn linode_server(ctx: &mut WorkflowCtx, input: &Input) -> GlobalResult<()> {
let mut cleanup_ctx = CleanupCtx::default();
let provision_res = match provision(ctx, input, &mut cleanup_ctx).await {

let res = provision(ctx, input, &mut cleanup_ctx).await;
let provision_res = match ctx.catch_unrecoverable(res)? {
Ok(x) => x,
// If we cannot recover a provisioning error, send a failed signal and clean up resources
Err(err) if !err.is_workflow_recoverable() => {
tracing::warn!(?err);
Err(err) => {
tracing::warn!(?err, "unrecoverable provision, cleaning up");

ctx.dispatch_workflow(cleanup::Input {
api_token: input.api_token.clone(),
Expand All @@ -51,7 +54,6 @@ pub async fn linode_server(ctx: &mut WorkflowCtx, input: &Input) -> GlobalResult
// Throw the original error from the provisioning activities
return Err(err);
}
x => x?,
};

ctx.tagged_signal(
Expand Down

0 comments on commit f7d021c

Please sign in to comment.