AssetWorker

Trait AssetWorker 

Source
pub trait AssetWorker:
    Send
    + Sync
    + Sized {
    type Opts;

    // Required methods
    fn name(&self) -> &'static str;
    fn build<'life0, 'async_trait, S>(
        opts: Self::Opts,
        store: &'life0 S,
        ids: Vec<String>,
    ) -> Pin<Box<dyn Future<Output = Result<Self, AssetWorkerError>> + Send + 'async_trait>>
       where S: 'async_trait + Store + 'static,
             Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

The universal trait for all workers that collect and update asset info.

This trait defines the common interface that all asset workers must implement, allowing the system to interact with different types of workers in a uniform way. Each worker is responsible for monitoring specific assets and updating their information in the store.

Worker implementations should:

  • Collect data from sources using appropriate methods
  • Parse and validate the received data
  • Update asset information using the WorkerStore
  • Handle errors and implement retry mechanisms
  • Support graceful shutdown

§Required Methods

Implementors must define:

  • name() - Returns a unique identifier for the worker type
  • build() - Creates a new worker instance with the specified options and query IDs

§Worker Store Usage

Workers should use the WorkerStore to interact with the main store. This provides namespace isolation and ensures that different workers can operate without conflicts. Typically, a worker implementation will:

  1. Receive a reference to the main store in its build method
  2. Create a WorkerStore instance with the worker’s name as prefix
  3. Use the WorkerStore to update asset information

§Examples

Implementing a simple HTTP polling worker:

use std::time::Duration;
use async_trait::async_trait;
use bothan_lib::worker::{AssetWorker, error::AssetWorkerError};
use bothan_lib::store::{Store, WorkerStore};
use bothan_lib::types::AssetInfo;
use rust_decimal::Decimal;

// Worker configuration
#[derive(Clone)]
struct HttpPollerOpts {
    poll_interval: Duration,
    endpoint: String,
}

// Worker implementation
struct HttpPoller {
    opts: HttpPollerOpts,
    ids: Vec<String>,
}

#[async_trait]
impl AssetWorker for HttpPoller {
    type Opts = HttpPollerOpts;

    fn name(&self) -> &'static str {
        "http_poller"
    }

    async fn build<S: Store + 'static>(
        opts: Self::Opts,
        store: &S,
        ids: Vec<String>,
    ) -> Result<Self, AssetWorkerError> {
        // Create a worker-specific store with the worker's name as prefix
        let worker_store = WorkerStore::new(store, "http_poller");

        // In a real implementation, you would also have methods for:
        // - Starting the worker (data collection loop)
        // - Handling errors and retries
        // - Shutting down gracefully
         
        Ok(HttpPoller {
            opts,
            ids,
        })
    }
}

Required Associated Types§

Source

type Opts

Configuration options for the worker.

This associated type defines the configuration parameters needed to construct an instance of the worker.

Required Methods§

Source

fn name(&self) -> &'static str

Returns the name of the worker.

The name serves as a unique identifier for the worker. When used in conjunction with Bothan’s registry, the worker’s name should match the source name specified in the registry.

Source

fn build<'life0, 'async_trait, S>( opts: Self::Opts, store: &'life0 S, ids: Vec<String>, ) -> Pin<Box<dyn Future<Output = Result<Self, AssetWorkerError>> + Send + 'async_trait>>
where S: 'async_trait + Store + 'static, Self: 'async_trait, 'life0: 'async_trait,

Builds a new worker instance with the specified options and query IDs.

This method is responsible for creating and initializing a new worker instance with the provided configuration and query IDs to monitor.

§Errors

Returns an AssetWorkerError if the worker cannot be built with the given parameters, such as when the configuration is invalid or required resources are unavailable.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§