camel_core/lifecycle/adapters/
runtime_execution.rs1use async_trait::async_trait;
2
3use camel_api::{CamelError, RuntimeQueryResult};
4
5use crate::lifecycle::adapters::controller_actor::RouteControllerHandle;
6use crate::lifecycle::application::RouteDefinition;
7use crate::lifecycle::domain::DomainError;
8use crate::lifecycle::ports::RuntimeExecutionPort;
9
10#[derive(Clone)]
12pub struct RuntimeExecutionAdapter {
13 controller: RouteControllerHandle,
14}
15
16impl RuntimeExecutionAdapter {
17 pub fn new(controller: RouteControllerHandle) -> Self {
18 Self { controller }
19 }
20}
21
22fn to_domain(e: CamelError) -> DomainError {
23 DomainError::InvalidState(e.to_string())
24}
25
26#[async_trait]
27impl RuntimeExecutionPort for RuntimeExecutionAdapter {
28 async fn register_route(&self, definition: RouteDefinition) -> Result<(), DomainError> {
29 self.controller
30 .add_route(definition)
31 .await
32 .map_err(to_domain)
33 }
34
35 async fn start_route(&self, route_id: &str) -> Result<(), DomainError> {
36 self.controller
37 .start_route(route_id)
38 .await
39 .map_err(to_domain)
40 }
41
42 async fn stop_route(&self, route_id: &str) -> Result<(), DomainError> {
43 self.controller
44 .stop_route(route_id)
45 .await
46 .map_err(to_domain)
47 }
48
49 async fn suspend_route(&self, route_id: &str) -> Result<(), DomainError> {
50 self.controller
51 .suspend_route(route_id)
52 .await
53 .map_err(to_domain)
54 }
55
56 async fn resume_route(&self, route_id: &str) -> Result<(), DomainError> {
57 self.controller
58 .resume_route(route_id)
59 .await
60 .map_err(to_domain)
61 }
62
63 async fn reload_route(&self, route_id: &str) -> Result<(), DomainError> {
64 self.controller
65 .restart_route(route_id)
66 .await
67 .map_err(to_domain)
68 }
69
70 async fn remove_route(&self, route_id: &str) -> Result<(), DomainError> {
71 self.controller
72 .remove_route(route_id)
73 .await
74 .map_err(to_domain)
75 }
76
77 async fn in_flight_count(&self, route_id: &str) -> Result<RuntimeQueryResult, DomainError> {
78 Ok(
79 match self
80 .controller
81 .in_flight_count(route_id)
82 .await
83 .map_err(to_domain)?
84 {
85 Some(count) => RuntimeQueryResult::InFlightCount {
86 route_id: route_id.to_string(),
87 count,
88 },
89 None => RuntimeQueryResult::RouteNotFound {
90 route_id: route_id.to_string(),
91 },
92 },
93 )
94 }
95}