Skip to main content

ActivityScope

Struct ActivityScope 

Source
pub struct ActivityScope;
Expand description

Scope for executing an activity within a transaction.

When an activity is executed within this scope:

  1. A transaction is started from the storage backend
  2. All writes are buffered via buffer_write
  3. On success, buffered writes are applied to the transaction, then it commits
  4. 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

Source

pub async fn run<F, Fut, T>( storage: &Arc<dyn WorkflowStorage>, f: F, ) -> Result<T, ClusterError>
where F: FnOnce() -> Fut, Fut: Future<Output = 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)
Source

pub fn is_active() -> bool

Check if there’s an active transaction in the current task.

Source

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!

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more