Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 909585a

Browse files
committed
wip(backend): add endpoint /selfServiceSectionsRuns/:slug/logs
1 parent 20ef9b1 commit 909585a

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

backend/src/database.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ pub struct SelfServiceRunJson {
9393
pub tasks: serde_json::Value,
9494
}
9595

96+
#[derive(Serialize, Deserialize, Debug)]
97+
pub struct SelfServiceRunLogJson {
98+
pub id: String,
99+
pub created_at: String,
100+
pub is_stderr: bool,
101+
pub message: String,
102+
}
103+
96104
/// Initialize the database by creating the tables
97105
pub async fn init_database(pg_pool: &Pool<Postgres>) -> Result<(), QError> {
98106
// execute SQL schema

backend/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tracing_subscriber::util::SubscriberInitExt;
1515

1616
use crate::cli::CLI;
1717
use crate::database::init_database;
18-
use crate::self_service::controllers::{exec_self_service_section_action_post_validate_scripts, exec_self_service_section_action_validate_scripts, list_self_service_section_actions, list_self_service_section_runs, list_self_service_section_runs_by_section_and_action_slugs, list_self_service_section_runs_by_section_slug, list_self_service_sections};
18+
use crate::self_service::controllers::{exec_self_service_section_action_post_validate_scripts, exec_self_service_section_action_validate_scripts, list_self_service_section_actions, list_self_service_section_run_logs, list_self_service_section_runs, list_self_service_section_runs_by_section_and_action_slugs, list_self_service_section_runs_by_section_slug, list_self_service_sections};
1919
use crate::self_service::services::BackgroundWorkerTask;
2020
use crate::yaml_config::YamlConfig;
2121

@@ -110,6 +110,7 @@ async fn main() {
110110
.route("/healthz", get(|| async { "OK" }))
111111
.route("/selfServiceSections", get(list_self_service_sections))
112112
.route("/selfServiceSections/runs", get(list_self_service_section_runs))
113+
.route("/selfServiceSectionsRuns/:slug/logs", get(list_self_service_section_run_logs))
113114
.route("/selfServiceSections/:slug/actions", get(list_self_service_section_actions))
114115
.route("/selfServiceSections/:slug/runs", get(list_self_service_section_runs_by_section_slug))
115116
.route("/selfServiceSections/:slug/actions/:slug/validate", post(exec_self_service_section_action_validate_scripts))

backend/src/self_service/controllers.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use std::sync::Arc;
33
use axum::{debug_handler, Extension, Json};
44
use axum::extract::Path;
55
use axum::http::StatusCode;
6+
use chrono::{NaiveDateTime, Utc};
67
use tokio::sync::mpsc::Sender;
78
use tracing::error;
89

910
use crate::database;
10-
use crate::database::{insert_self_service_run, SelfServiceRunJson, Status};
11+
use crate::database::{insert_self_service_run, SelfServiceRunJson, SelfServiceRunLogJson, Status};
1112
use crate::self_service::{check_json_payload_against_yaml_config_fields, execute_command, ExecValidateScriptRequest, find_self_service_section_by_slug, get_self_service_section_and_action, JobResponse, ResultsResponse};
1213
use crate::self_service::services::BackgroundWorkerTask;
1314
use crate::yaml_config::{SelfServiceSectionActionYamlConfig, SelfServiceSectionYamlConfig, YamlConfig};
@@ -82,6 +83,30 @@ pub async fn list_self_service_section_runs(
8283
}
8384
}
8485

86+
#[debug_handler]
87+
pub async fn list_self_service_section_run_logs(
88+
Extension(pg_pool): Extension<Arc<sqlx::PgPool>>,
89+
Path(logs_slug): Path<String>,
90+
) -> (StatusCode, Json<ResultsResponse<SelfServiceRunLogJson>>) {
91+
92+
// mock data for now
93+
let mut data = vec![];
94+
95+
for i in 1..1000 {
96+
data.push(SelfServiceRunLogJson {
97+
id: i.to_string(),
98+
created_at: "2021-08-01T00:00:00Z".to_string(),
99+
is_stderr: i % 2 == 0,
100+
message: if i % 2 == 0 { format!("this is an error message {}", i) } else { format!("this is a log message {}", i) },
101+
});
102+
}
103+
104+
(StatusCode::OK, Json(ResultsResponse {
105+
message: None,
106+
results: data,
107+
}))
108+
}
109+
85110
#[debug_handler]
86111
pub async fn exec_self_service_section_action_validate_scripts(
87112
Extension(yaml_config): Extension<Arc<YamlConfig>>,

backend/src/self_service/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async fn execute_command<T>(
109109

110110
if external_command.get_command().len() == 1 {
111111
return Err(format!("Validate script '{}' is invalid. \
112-
Be explicit on the command to execute, e.g. 'python3 examples/validation_script.py'",
112+
Be explicit on the command to execute, e.g. 'python examples/validation_script.py'",
113113
external_command.get_command()[0]));
114114
}
115115

@@ -129,6 +129,8 @@ async fn execute_command<T>(
129129
Err(err) => return Err(format!("Validate script '{}' failed: {}", &cmd_one_line, err))
130130
};
131131

132+
// get stdout and stderr from child and forward it in real time to the upper function
133+
132134
let exit_status = match timeout(Duration::from_secs(external_command.get_timeout()), child.wait()).await {
133135
Ok(exit_status) => exit_status,
134136
Err(_) => return Err(match child.kill().await {
@@ -148,8 +150,6 @@ async fn execute_command<T>(
148150
return Err(format!("Validate script '{}' failed: {:?}", &cmd_one_line, exit_status));
149151
}
150152

151-
// TODO parse output.stdout and output.stderr and forward to the frontend
152-
153153
Ok(consume_job_output_result_from_json_output_env(cmd_one_line.as_str(), start.elapsed().as_millis()))
154154
}
155155

0 commit comments

Comments
 (0)