-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(workflows): fix listening traits
- Loading branch information
1 parent
cdba2f3
commit 7d5d59d
Showing
14 changed files
with
202 additions
and
79 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::{ | ||
ctx::WorkflowCtx, | ||
db::SignalRow, | ||
error::{WorkflowError, WorkflowResult}, | ||
}; | ||
|
||
/// Indirection struct to prevent invalid implementations of listen traits. | ||
pub struct ListenCtx<'a> { | ||
ctx: &'a mut WorkflowCtx, | ||
} | ||
|
||
impl<'a> ListenCtx<'a> { | ||
pub(crate) fn new(ctx: &'a mut WorkflowCtx) -> Self { | ||
ListenCtx { ctx } | ||
} | ||
|
||
/// Checks for a signal to this workflow with any of the given signal names. | ||
pub async fn listen_any(&self, signal_names: &[&'static str]) -> WorkflowResult<SignalRow> { | ||
// Fetch new pending signal | ||
let signal = self | ||
.ctx | ||
.db | ||
.pull_next_signal( | ||
self.ctx.workflow_id(), | ||
signal_names, | ||
self.ctx.full_location().as_ref(), | ||
) | ||
.await?; | ||
|
||
let Some(signal) = signal else { | ||
return Err(WorkflowError::NoSignalFound(Box::from(signal_names))); | ||
}; | ||
|
||
tracing::info!( | ||
workflow_name=%self.ctx.name(), | ||
workflow_id=%self.ctx.workflow_id(), | ||
signal_id=%signal.signal_id, | ||
signal_name=%signal.signal_name, | ||
"signal received", | ||
); | ||
|
||
Ok(signal) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::{ctx::ListenCtx, error::WorkflowResult}; | ||
|
||
/// A trait which allows listening for signals from the workflows database. This is used by | ||
/// `WorkflowCtx::listen` and `WorkflowCtx::query_signal`. If you need a listener with state, use | ||
/// `CustomListener`. | ||
#[async_trait] | ||
pub trait Listen: Sized { | ||
async fn listen(ctx: &ListenCtx) -> WorkflowResult<Self>; | ||
fn parse(name: &str, body: serde_json::Value) -> WorkflowResult<Self>; | ||
} | ||
|
||
/// A trait which allows listening for signals with a custom state. This is used by | ||
/// `WorkflowCtx::custom_listener`. | ||
#[async_trait] | ||
pub trait CustomListener: Sized { | ||
type Output; | ||
|
||
async fn listen(&self, ctx: &ListenCtx) -> WorkflowResult<Self::Output>; | ||
fn parse(name: &str, body: serde_json::Value) -> WorkflowResult<Self::Output>; | ||
} |
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
Oops, something went wrong.