pub struct ActivityScope;Expand description
Scope for executing an activity within a transaction.
When an activity is executed within this scope:
- A transaction is started from the storage backend
- All writes are buffered via
buffer_write - On success, buffered writes are applied to the transaction, then it commits
- On failure (panic or error), the transaction rolls back
This ensures that persistence is SYNCHRONOUS with activity completion - the activity only returns success AFTER the transaction is committed.
Implementations§
Source§impl ActivityScope
impl ActivityScope
Sourcepub async fn run<F, Fut, T>(
storage: &Arc<dyn WorkflowStorage>,
f: F,
) -> Result<T, ClusterError>
pub async fn run<F, Fut, T>( storage: &Arc<dyn WorkflowStorage>, f: F, ) -> Result<T, ClusterError>
Run an activity within a transactional scope.
The provided async closure is executed with an active transaction.
If the closure returns Ok, the transaction is committed SYNCHRONOUSLY
before this function returns.
If the closure returns Err or panics, the transaction is rolled back.
During execution, the activity can:
- Buffer writes via
ActivityScope::buffer_write()(committed atomically) - Execute arbitrary SQL via
ActivityScope::sql_transaction()(if using SQL storage)
Sourcepub fn buffer_write(key: String, value: Vec<u8>)
pub fn buffer_write(key: String, value: Vec<u8>)
Buffer a write to be applied to the transaction on commit.
This is called by DurableContext to buffer journal writes
so that activity results are persisted atomically.
The write is buffered synchronously and applied to the transaction BEFORE the activity returns. No fire-and-forget!
Sourcepub async fn db() -> SqlTransactionHandle
pub async fn db() -> SqlTransactionHandle
Get the activity’s SQL transaction handle.
This is a convenience method that calls sql_transaction() and panics
if no SQL transaction is available. Use this in #[activity] methods
when you know the storage backend supports SQL transactions.
§Panics
Panics if called outside of an activity scope or if the storage backend does not support SQL transactions (e.g., memory storage).
§Note
Prefer using self.tx() in activity methods instead. ActivityScope::db()
is the legacy API retained for backward compatibility.
Sourcepub async fn sql_transaction() -> Option<SqlTransactionHandle>
pub async fn sql_transaction() -> Option<SqlTransactionHandle>
Get the underlying SQL transaction for executing arbitrary SQL.
Returns None if:
- Not currently within an activity scope
- The storage backend doesn’t support SQL transactions (e.g., memory storage)
Auto Trait Implementations§
impl Freeze for ActivityScope
impl RefUnwindSafe for ActivityScope
impl Send for ActivityScope
impl Sync for ActivityScope
impl Unpin for ActivityScope
impl UnsafeUnpin for ActivityScope
impl UnwindSafe for ActivityScope
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request