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