1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
//! # MCAI Worker SDK //! //! This library is an SDK to communicate via message broker with [StepFlow](https://hexdocs.pm/step_flow/readme.html). //! It's used for every worker as an abstraction. //! It manage itself requirements, message parsing, direct messaging. //! //! ## Worker implementation //! //! 1. Create a Rust project //! 2. Add MCAI Worker SDK as a dependency in Cargo.toml: `mcai_worker_sdk = "^1.0"` //! 1. Update the main file with the example provided here to implement [MessageEvent](trait.MessageEvent.html) trait, //! and call the [`start_worker`](fn.start_worker.html) to start the worker itself. //! //! ```rust //! use mcai_worker_sdk::prelude::*; //! use serde_derive::Deserialize; //! use schemars::JsonSchema; //! //! #[derive(Debug)] //! struct WorkerNameEvent {} //! //! #[derive(Debug, Deserialize, JsonSchema)] //! struct WorkerParameters {} //! //! impl MessageEvent<WorkerParameters> for WorkerNameEvent { //! fn get_name(&self) -> String {"sample_worker".to_string()} //! fn get_short_description(&self) -> String {"Short description".to_string()} //! fn get_description(&self) -> String {"Long description".to_string()} //! fn get_version(&self) -> Version { Version::new(0, 0, 1) } //! } //! static WORKER_NAME_EVENT: WorkerNameEvent = WorkerNameEvent {}; //! //! // uncomment it to start the worker //! // fn main() { //! // mcai_worker_sdk::start_worker(&WORKER_NAME_EVENT); //! // } //! ``` //! //! ## Runtime configuration //! //! ### AMQP connection //! //! | Variable | Description | //! |-----------------|-------------| //! | `AMQP_HOSTNAME` | IP or host of AMQP server (default: `localhost`) | //! | `AMQP_PORT` | AMQP server port (default: `5672`) | //! | `AMQP_TLS` | enable secure connection using AMQPS (default: `false`, enable with `true` or `1` or `TRUE` or `True`) | //! | `AMQP_USERNAME` | Username used to connect to AMQP server (default: `guest`) | //! | `AMQP_PASSWORD` | Password used to connect to AMQP server (default: `guest`) | //! | `AMQP_VHOST` | AMQP virtual host (default: `/`) | //! | `AMQP_QUEUE` | AMQP queue name used to receive job orders (default: `job_undefined`) | //! //! ### Vault connection //! //! | Variable | Description | //! |--------------------|-------------| //! | `BACKEND_HOSTNAME` | URL used to connect to backend server (default: `http://127.0.0.1:4000/api`) | //! | `BACKEND_USERNAME` | Username used to connect to backend server | //! | `BACKEND_PASSWORD` | Password used to connect to backend server | //! //! ## Start worker locally //! //! MCAI Worker SDK can be launched locally - without RabbitMQ. //! It can process some message for different purpose (functional tests, message order examples, etc.). //! //! To start worker in this mode, setup the environment variable `SOURCE_ORDERS` with path(s) to json orders. //! It can take multiple orders, joined with `:` on unix platform, `;` on windows os. //! //! ### Examples: //! //! ```bash //! RUST_LOG=info SOURCE_ORDERS=./examples/success_order.json:./examples/error_order.json cargo run --example worker //! ``` #[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_json; #[cfg(feature = "media")] #[macro_use] extern crate yaserde_derive; pub mod config; mod error; pub mod job; pub mod message; pub mod message_event; pub mod parameter; pub mod prelude; #[cfg(feature = "media")] mod process_frame; #[cfg(feature = "media")] mod process_result; mod start_worker; pub mod worker; pub mod message_exchange; pub mod processor; use crate::message_exchange::WorkerResponseSender; pub use error::{MessageError, Result}; pub use message::publish_job_progression; pub use message_event::MessageEvent; pub use parameter::container::ParametersContainer; use processor::Processor; use std::sync::{Arc, Mutex}; /// Exposed Channel type pub type McaiChannel = Arc<Mutex<dyn WorkerResponseSender + Send>>; #[test] fn empty_message_event_impl() { use crate::prelude::*; #[derive(Debug)] struct CustomEvent {} #[derive(JsonSchema, Deserialize)] struct CustomParameters {} impl MessageEvent<CustomParameters> for CustomEvent { fn get_name(&self) -> String { "custom".to_string() } fn get_short_description(&self) -> String { "short description".to_string() } fn get_description(&self) -> String { "long description".to_string() } fn get_version(&self) -> semver::Version { semver::Version::new(1, 2, 3) } } let custom_event = CustomEvent {}; let parameters = CustomParameters {}; let job = job::Job { job_id: 1234, parameters: vec![], }; let job_result = job::JobResult::new(job.job_id); let result = custom_event.process(None, parameters, job_result); assert!(result == Err(MessageError::NotImplemented())); }