Module proc

Module proc 

Source
Expand description

A processor in ProSA is an element that process transactions and can contact external component. It’s similar to a micro service. It can answer to a service request or ask something to a service.

Define ProSA processor to do a processing

To create a ProSA processor:

use std::error::Error;
use serde::{Deserialize, Serialize};
use prosa::core::proc::{proc_settings, proc, Proc, ProcBusParam};
use prosa::core::adaptor::Adaptor;
use prosa::core::msg::{Msg, InternalMsg, Tvf};
use prosa::core::error::ProcError;

pub trait MyAdaptorTrait<M>
where
    M: 'static
    + std::marker::Send
    + std::marker::Sync
    + std::marker::Sized
    + std::clone::Clone
    + std::fmt::Debug
    + Tvf
    + std::default::Default,
{
    /// Method called when the processor spawns
    /// This method is called only once so the processing will be thread safe
    fn new(proc: &MyProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>> where Self: Sized;
}

#[derive(Adaptor)]
pub struct MyAdaptor {
    // your adaptor vars here
}

impl<M> MyAdaptorTrait<M> for MyAdaptor
where
    M: 'static
    + std::marker::Send
    + std::marker::Sync
    + std::marker::Sized
    + std::clone::Clone
    + std::fmt::Debug
    + Tvf
    + std::default::Default,
{
    fn new(proc: &MyProc<M>) -> Result<Self, Box<dyn ProcError + Send + Sync>> {
        // Init your adaptor from processor parameters
        Ok(Self {})
    }
}

#[proc_settings]
#[derive(Default, Debug, Deserialize, Serialize)]
pub struct MyProcSettings {
    param: String,
    // ...
}

#[proc(settings = MyProcSettings)]
pub struct MyProc { /* Nothing in here */ }

#[proc]
impl MyProc
{
    fn internal_func() {
        // You can declare function
    }
}
// or explicitly
//#[proc]
//impl<M> MyProc<M>
//where
//    M: 'static
//    + std::marker::Send
//    + std::marker::Sync
//    + std::marker::Sized
//    + std::clone::Clone
//    + std::fmt::Debug
//    + Tvf
//    + std::default::Default,
//{
//    fn internal_func() {
//        // You can declare function
//    }
//}

// You must implement the trait Proc to define your processing
#[proc]
impl<A> Proc<A> for MyProc
where
    A: Adaptor + MyAdaptorTrait<M> + std::marker::Send + std::marker::Sync,
{
    async fn internal_run(&mut self) -> Result<(), Box<dyn ProcError + Send + Sync>> {
        // Initiate an adaptor
        let mut adaptor = A::new(self)?;

        // Declare the processor
        self.proc.add_proc().await?;

        // Retrieve param from the processor settings `MyProcSettings`
        let _param = &self.settings.param;

        loop {
            if let Some(msg) = self.internal_rx_queue.recv().await {
                match msg {
                    InternalMsg::Request(msg) => {
                        // TODO process the request
                    }
                    InternalMsg::Response(msg) => {
                       // TODO process the response
                    },
                    InternalMsg::Error(err) => {
                       // TODO process the error
                    },
                    InternalMsg::Command(_) => todo!(),
                    InternalMsg::Config => todo!(),
                    InternalMsg::Service(table) => self.service = table,
                    InternalMsg::Shutdown => {
                        adaptor.terminate();
                        self.proc.remove_proc(None).await?;
                        return Ok(());
                    }
                }
            }
        }
    }
}

Structs§

ProcParam
Parameters embeded in a ProSA processor

Traits§

Proc
Generic trait to define ProSA processor
ProcBusParam
Global parameter for a processor (main or specific)
ProcConfig
Trait to define ProSA processor configuration
ProcEpilogue
Trait to define all processor handle functions
ProcSettings
Trait to define ProSA processor settings

Attribute Macros§

proc
Procedural macro to help building an ProSA Processor
proc_settings
Implement the trait ProcSettings. Procedural macro to help building an ProSA Processor Settings