2026-04-07 00:13:54 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use tracing::{debug, warn};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::connect::ConnectClient;
|
2026-04-07 00:44:02 +02:00
|
|
|
use crate::proto::runner::v1::{
|
|
|
|
|
self, LogRow, StepState, TaskState, UpdateLogRequest, UpdateTaskRequest,
|
|
|
|
|
};
|
2026-04-07 00:41:26 +02:00
|
|
|
use crate::state::{RunnerState, StepInfo};
|
2026-04-07 00:13:54 +02:00
|
|
|
|
|
|
|
|
const POLL_INTERVAL: Duration = Duration::from_secs(3);
|
|
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
/// Categories that belong to the "Set up job" phase — must match reporter.rs.
|
|
|
|
|
const SETUP_CATEGORIES: &[&str] = &["boot", "default", "env", "env_setup", "tool_check"];
|
|
|
|
|
|
2026-04-07 00:23:00 +02:00
|
|
|
/// Log category summary from logs-service.
|
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
|
struct LogCategorySummary {
|
|
|
|
|
category: String,
|
|
|
|
|
count: i64,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 00:13:54 +02:00
|
|
|
/// Streams logs from logs-service to Forgejo while a job is in-flight.
|
2026-04-07 00:44:02 +02:00
|
|
|
/// Also sends incremental `UpdateTask` with step states so Forgejo assigns
|
|
|
|
|
/// log lines to the correct steps in real time.
|
2026-04-07 00:13:54 +02:00
|
|
|
pub async fn stream_logs(
|
|
|
|
|
client: Arc<ConnectClient>,
|
|
|
|
|
state: Arc<RunnerState>,
|
|
|
|
|
request_id: Uuid,
|
|
|
|
|
task_id: i64,
|
|
|
|
|
logs_base: String,
|
2026-04-07 00:41:26 +02:00
|
|
|
steps: Vec<StepInfo>,
|
2026-04-07 00:13:54 +02:00
|
|
|
mut stop: tokio::sync::watch::Receiver<bool>,
|
|
|
|
|
) -> i64 {
|
|
|
|
|
let http = reqwest::Client::new();
|
|
|
|
|
let mut log_index: i64 = 0;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
if *stop.borrow() {
|
|
|
|
|
log_index = poll_and_send(
|
2026-04-07 00:41:26 +02:00
|
|
|
&client, &state, &http, &logs_base, request_id, task_id, log_index, &steps,
|
2026-04-07 00:13:54 +02:00
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_index = poll_and_send(
|
2026-04-07 00:41:26 +02:00
|
|
|
&client, &state, &http, &logs_base, request_id, task_id, log_index, &steps,
|
2026-04-07 00:13:54 +02:00
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
|
_ = tokio::time::sleep(POLL_INTERVAL) => {}
|
|
|
|
|
_ = stop.changed() => {
|
|
|
|
|
log_index = poll_and_send(
|
|
|
|
|
&client, &state, &http, &logs_base,
|
2026-04-07 00:41:26 +02:00
|
|
|
request_id, task_id, log_index, &steps,
|
2026-04-07 00:13:54 +02:00
|
|
|
).await;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug!(task_id, log_index, "log streamer stopped");
|
|
|
|
|
log_index
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
/// Sort categories: setup first, then step categories in KDL order, then remaining.
|
2026-04-07 00:41:26 +02:00
|
|
|
fn sort_categories(categories: &mut [LogCategorySummary], steps: &[StepInfo]) {
|
|
|
|
|
categories.sort_by_key(|c| {
|
|
|
|
|
if SETUP_CATEGORIES.contains(&c.category.as_str()) {
|
|
|
|
|
(0, 0, c.category.clone())
|
|
|
|
|
} else if let Some(pos) = steps.iter().position(|s| s.log_category == c.category) {
|
|
|
|
|
(1, pos, c.category.clone())
|
|
|
|
|
} else {
|
|
|
|
|
(2, 0, c.category.clone())
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 00:13:54 +02:00
|
|
|
async fn poll_and_send(
|
|
|
|
|
client: &ConnectClient,
|
|
|
|
|
state: &RunnerState,
|
|
|
|
|
http: &reqwest::Client,
|
|
|
|
|
logs_base: &str,
|
|
|
|
|
request_id: Uuid,
|
|
|
|
|
task_id: i64,
|
|
|
|
|
current_index: i64,
|
2026-04-07 00:41:26 +02:00
|
|
|
steps: &[StepInfo],
|
2026-04-07 00:13:54 +02:00
|
|
|
) -> i64 {
|
|
|
|
|
let categories_url = format!(
|
|
|
|
|
"{}/jobs/{}/logs",
|
|
|
|
|
logs_base.trim_end_matches('/'),
|
|
|
|
|
request_id
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
let mut categories = match http.get(&categories_url).send().await {
|
2026-04-07 00:13:54 +02:00
|
|
|
Ok(resp) if resp.status().is_success() => resp
|
|
|
|
|
.json::<Vec<LogCategorySummary>>()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
_ => return current_index,
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
if categories.is_empty() {
|
2026-04-07 00:23:00 +02:00
|
|
|
return current_index;
|
2026-04-07 00:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-07 00:41:26 +02:00
|
|
|
sort_categories(&mut categories, steps);
|
2026-04-07 00:23:00 +02:00
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
// Build the full ordered log and track per-category line counts
|
2026-04-07 00:32:54 +02:00
|
|
|
let mut all_lines: Vec<String> = Vec::new();
|
2026-04-07 00:44:02 +02:00
|
|
|
let mut category_counts: Vec<(String, i64)> = Vec::new();
|
|
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
for cat in &categories {
|
2026-04-07 00:13:54 +02:00
|
|
|
let url = format!(
|
|
|
|
|
"{}/jobs/{}/logs/{}",
|
|
|
|
|
logs_base.trim_end_matches('/'),
|
|
|
|
|
request_id,
|
|
|
|
|
cat.category
|
|
|
|
|
);
|
2026-04-07 00:23:00 +02:00
|
|
|
|
|
|
|
|
let text = match http.get(&url).send().await {
|
|
|
|
|
Ok(resp) if resp.status().is_success() => match resp.text().await {
|
|
|
|
|
Ok(t) => t,
|
|
|
|
|
Err(_) => continue,
|
|
|
|
|
},
|
|
|
|
|
_ => continue,
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
let lines: Vec<&str> = text.lines().collect();
|
|
|
|
|
let count = lines.len() as i64;
|
|
|
|
|
category_counts.push((cat.category.clone(), count));
|
|
|
|
|
for line in lines {
|
2026-04-07 00:32:54 +02:00
|
|
|
all_lines.push(line.to_string());
|
2026-04-07 00:13:54 +02:00
|
|
|
}
|
2026-04-07 00:32:54 +02:00
|
|
|
}
|
2026-04-07 00:13:54 +02:00
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
let total = all_lines.len() as i64;
|
|
|
|
|
if total <= current_index {
|
|
|
|
|
return current_index;
|
|
|
|
|
}
|
2026-04-07 00:13:54 +02:00
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
// Send new log lines
|
2026-04-07 00:32:54 +02:00
|
|
|
let new_lines = &all_lines[current_index as usize..];
|
2026-04-07 00:13:54 +02:00
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
let now = prost_types::Timestamp {
|
|
|
|
|
seconds: time::OffsetDateTime::now_utc().unix_timestamp(),
|
|
|
|
|
nanos: 0,
|
|
|
|
|
};
|
2026-04-07 00:23:00 +02:00
|
|
|
|
2026-04-07 00:32:54 +02:00
|
|
|
let rows: Vec<LogRow> = new_lines
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|line| LogRow {
|
|
|
|
|
time: Some(now.clone()),
|
|
|
|
|
content: line.clone(),
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
let new_count = rows.len();
|
2026-04-07 00:32:54 +02:00
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
let log_req = UpdateLogRequest {
|
2026-04-07 00:32:54 +02:00
|
|
|
task_id,
|
|
|
|
|
index: current_index,
|
|
|
|
|
rows,
|
|
|
|
|
no_more: false,
|
|
|
|
|
};
|
2026-04-07 00:23:00 +02:00
|
|
|
|
2026-04-07 00:44:02 +02:00
|
|
|
let new_index = match client
|
|
|
|
|
.update_log(&log_req, &state.identity.uuid, &state.identity.token)
|
2026-04-07 00:32:54 +02:00
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(resp) => {
|
|
|
|
|
debug!(
|
|
|
|
|
task_id,
|
2026-04-07 00:44:02 +02:00
|
|
|
new_lines = new_count,
|
2026-04-07 00:32:54 +02:00
|
|
|
ack_index = resp.ack_index,
|
|
|
|
|
"streamed logs"
|
|
|
|
|
);
|
|
|
|
|
resp.ack_index
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!(error = %e, task_id, "failed to stream logs");
|
2026-04-07 00:44:02 +02:00
|
|
|
return current_index;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Build and send step states so Forgejo maps lines to steps in real time
|
|
|
|
|
let setup_lines: i64 = category_counts
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|(cat, _)| SETUP_CATEGORIES.contains(&cat.as_str()))
|
|
|
|
|
.map(|(_, count)| count)
|
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
|
|
let mut step_states: Vec<StepState> = Vec::new();
|
|
|
|
|
let mut cursor = setup_lines;
|
|
|
|
|
|
|
|
|
|
for (step_idx, step_info) in steps.iter().enumerate() {
|
|
|
|
|
let step_lines = category_counts
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|(cat, _)| *cat == step_info.log_category)
|
|
|
|
|
.map(|(_, count)| *count)
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
if step_lines > 0 {
|
|
|
|
|
step_states.push(StepState {
|
|
|
|
|
id: step_idx as i64,
|
|
|
|
|
result: v1::Result::Unspecified as i32, // still running
|
|
|
|
|
started_at: Some(now.clone()),
|
|
|
|
|
stopped_at: None,
|
|
|
|
|
log_index: cursor,
|
|
|
|
|
log_length: step_lines,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
cursor += step_lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send UpdateTask with current step states (incremental update)
|
|
|
|
|
if !step_states.is_empty() {
|
|
|
|
|
let task_req = UpdateTaskRequest {
|
|
|
|
|
state: Some(TaskState {
|
|
|
|
|
id: task_id,
|
|
|
|
|
result: v1::Result::Unspecified as i32, // still running
|
|
|
|
|
started_at: None,
|
|
|
|
|
stopped_at: None,
|
|
|
|
|
steps: step_states,
|
|
|
|
|
}),
|
|
|
|
|
outputs: Default::default(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Err(e) = client
|
|
|
|
|
.update_task(&task_req, &state.identity.uuid, &state.identity.token)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
warn!(error = %e, task_id, "failed to update step states");
|
2026-04-07 00:32:54 +02:00
|
|
|
}
|
2026-04-07 00:13:54 +02:00
|
|
|
}
|
2026-04-07 00:44:02 +02:00
|
|
|
|
|
|
|
|
new_index
|
2026-04-07 00:13:54 +02:00
|
|
|
}
|