Skip to content

Commit 22e8b2f

Browse files
Merge pull request #217 from code0-tech/#213-heartbeat-status-update
heartbeat status update
2 parents dbadeca + b3deebf commit 22e8b2f

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

crates/base/src/client/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl DracoRuntimeStatusService {
101101
&self,
102102
status: tucana::shared::adapter_runtime_status::Status,
103103
) {
104-
log::info!("Updating the current Runtime Status!");
104+
log::info!("Updating the current runtime status!");
105105
let mut client = RuntimeStatusServiceClient::new(self.channel.clone());
106106

107107
let now = SystemTime::now();
@@ -128,12 +128,10 @@ impl DracoRuntimeStatusService {
128128
);
129129

130130
match client.update(request).await {
131-
Ok(response) => {
132-
log::info!(
133-
"Was the update of the RuntimeStatus accepted by Sagittarius? {}",
134-
response.into_inner().success
135-
);
136-
}
131+
Ok(response) => match response.into_inner().success {
132+
true => log::info!("Successful update of the runtime status."),
133+
false => log::warn!("Failed to update runtime status."),
134+
},
137135
Err(err) => {
138136
log::error!("Failed to update RuntimeStatus: {:?}", err);
139137
}

crates/base/src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ pub struct AdapterConfig {
6161
///
6262
/// The Variant of Draco. E.g. Http, Cron...
6363
pub draco_variant: String,
64+
65+
/// Adapter Status Update Interval Seconds
66+
///
67+
/// Interval for runtime status heartbeat updates while the adapter is running.
68+
/// Set to 0 to disable periodic heartbeat updates.
69+
pub adapter_status_update_interval_seconds: u64,
6470
}
6571

6672
impl AdapterConfig {
@@ -93,6 +99,10 @@ impl AdapterConfig {
9399

94100
let draco_variant =
95101
code0_flow::flow_config::env_with_default("DRACO_VARIANT", String::from("None"));
102+
let adapter_status_update_interval_seconds = code0_flow::flow_config::env_with_default(
103+
"ADAPTER_STATUS_UPDATE_INTERVAL_SECONDS",
104+
30_u64,
105+
);
96106
Self {
97107
environment,
98108
nats_bucket,
@@ -105,6 +115,7 @@ impl AdapterConfig {
105115
definition_path,
106116
with_health_service,
107117
draco_variant,
118+
adapter_status_update_interval_seconds,
108119
}
109120
}
110121

crates/base/src/runner.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
};
77
use code0_flow::flow_service::FlowUpdateService;
88
use std::{sync::Arc, time::Duration};
9-
use tokio::{signal, time::sleep};
9+
use tokio::{signal, task::JoinHandle, time::sleep};
1010
use tonic::transport::Server;
1111
use tonic_health::pb::health_server::HealthServer;
1212
use tucana::shared::{AdapterConfiguration, RuntimeFeature};
@@ -62,11 +62,12 @@ impl<C: LoadConfig> ServerRunner<C> {
6262
runtime_config: Vec<AdapterConfiguration>,
6363
) -> anyhow::Result<()> {
6464
let config = self.context.adapter_config.clone();
65-
let mut runtime_status_service: Option<DracoRuntimeStatusService> = None;
65+
let mut runtime_status_service: Option<Arc<DracoRuntimeStatusService>> = None;
66+
let mut runtime_status_heartbeat_task: Option<JoinHandle<()>> = None;
6667
log::info!("Starting Draco Variant: {}", config.draco_variant);
6768

6869
if !config.is_static() {
69-
runtime_status_service = Some(
70+
runtime_status_service = Some(Arc::new(
7071
DracoRuntimeStatusService::from_url(
7172
config.aquila_url.clone(),
7273
config.aquila_token.clone(),
@@ -75,7 +76,7 @@ impl<C: LoadConfig> ServerRunner<C> {
7576
runtime_config,
7677
)
7778
.await,
78-
);
79+
));
7980

8081
if let Some(ser) = &runtime_status_service {
8182
ser.update_runtime_status_by_status(
@@ -148,6 +149,35 @@ impl<C: LoadConfig> ServerRunner<C> {
148149
tucana::shared::adapter_runtime_status::Status::Running,
149150
)
150151
.await;
152+
153+
if config.adapter_status_update_interval_seconds > 0 {
154+
let status_service = Arc::clone(ser);
155+
let update_interval_seconds = config.adapter_status_update_interval_seconds;
156+
runtime_status_heartbeat_task = Some(tokio::spawn(async move {
157+
let mut interval =
158+
tokio::time::interval(Duration::from_secs(update_interval_seconds));
159+
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
160+
161+
// First tick is immediate; consume it so heartbeats start after the interval.
162+
interval.tick().await;
163+
164+
loop {
165+
interval.tick().await;
166+
status_service
167+
.update_runtime_status_by_status(
168+
tucana::shared::adapter_runtime_status::Status::Running,
169+
)
170+
.await;
171+
}
172+
}));
173+
174+
log::info!(
175+
"Runtime status heartbeat started (interval={}s)",
176+
update_interval_seconds
177+
);
178+
} else {
179+
log::info!("Runtime status heartbeat is disabled");
180+
}
151181
};
152182
log::info!("Draco successfully initialized.");
153183

@@ -212,6 +242,15 @@ impl<C: LoadConfig> ServerRunner<C> {
212242
}
213243
}
214244
}
245+
if let Some(handle) = runtime_status_heartbeat_task.take() {
246+
handle.abort();
247+
if let Err(err) = handle.await {
248+
if !err.is_cancelled() {
249+
log::warn!("Runtime status heartbeat task ended unexpectedly: {}", err);
250+
}
251+
}
252+
}
253+
215254
if let Some(ser) = &runtime_status_service {
216255
ser.update_runtime_status_by_status(
217256
tucana::shared::adapter_runtime_status::Status::Stopped,

0 commit comments

Comments
 (0)