Skip to main content

TypedWorker

Trait TypedWorker 

Source
pub trait TypedWorker: Send + Sync {
    type Args: DeserializeOwned + Send + Sync;

    // Required methods
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        args: Self::Args,
        context: &'life1 WorkerContext,
    ) -> Pin<Box<dyn Future<Output = Result<WorkerResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn method_name(&self) -> &str;
}
Expand description

Typed worker trait — receives a deserialized Args value instead of the raw JSON payload.

use async_trait::async_trait;
use qml_rs::{TypedWorker, WorkerContext, WorkerResult, Result};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct SendEmailArgs { to: String, subject: String }

struct SendEmailWorker;

#[async_trait]
impl TypedWorker for SendEmailWorker {
    type Args = SendEmailArgs;

    async fn execute(&self, args: Self::Args, _ctx: &WorkerContext) -> Result<WorkerResult> {
        println!("sending {} to {}", args.subject, args.to);
        Ok(WorkerResult::success(None, 0))
    }

    fn method_name(&self) -> &str { "send_email" }
}

Register a TypedWorker with WorkerRegistry::register_typed, which wraps it in a TypedWorkerAdapter and stores it as a regular Worker.

Required Associated Types§

Source

type Args: DeserializeOwned + Send + Sync

Strongly-typed argument payload. Deserialized from job.payload before TypedWorker::execute is invoked.

Required Methods§

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, args: Self::Args, context: &'life1 WorkerContext, ) -> Pin<Box<dyn Future<Output = Result<WorkerResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute a job with its deserialized arguments.

Source

fn method_name(&self) -> &str

Get the method name this worker handles.

Implementors§