Skip to main content

Resource

Trait Resource 

Source
pub trait Resource:
    Send
    + Sync
    + 'static {
    // Provided methods
    fn setup<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), CanoError>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn teardown<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), CanoError>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

A lifecycle-managed dependency that can be set up and torn down by the engine.

Implement this trait for any external resource your workflow depends on — database connection pools, HTTP clients, message-queue consumers, etc.

Both methods have default implementations that do nothing, so you only need to override the phases that matter for your resource.

§Examples

use cano::resource::Resource;
use cano::error::CanoError;

struct HttpClient {
    base_url: String,
}

#[cano::resource]
impl Resource for HttpClient {
    async fn setup(&self) -> Result<(), CanoError> {
        // verify connectivity, warm connection pool, etc.
        Ok(())
    }

    async fn teardown(&self) -> Result<(), CanoError> {
        // drain pending requests, close sockets, etc.
        Ok(())
    }
}

Provided Methods§

Source

fn setup<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), CanoError>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Called once before the workflow starts. Use it to open connections, validate configuration, or perform any other one-time initialization.

The default implementation does nothing and returns Ok(()).

Source

fn teardown<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), CanoError>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait,

Called once after the workflow finishes (or after a failed setup rollback). Use it to close connections, flush buffers, or release OS resources.

The default implementation does nothing and returns Ok(()).

Implementors§