Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions src/cli/cmd/job/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,15 @@ pub enum Error {
impl Input {
pub async fn exec(&self, ctx: Context) -> Result<(), Error> {
if self.follow {
self.follow_logs(ctx).await
self.follow_logs(ctx).await.context(HttpClientSnafu)
} else {
self.show_logs(&ctx, 0).await?;
self.show_logs(&ctx, 0).await.context(HttpClientSnafu)?;
Ok(())
}
}

async fn show_logs(&self, ctx: &Context, seen: usize) -> Result<usize, Error> {
let result = ctx
.client
.session_logs(&self.job_id)
.await
.context(HttpClientSnafu)?;
async fn show_logs(&self, ctx: &Context, seen: usize) -> Result<usize, httpclient::Error> {
let result = ctx.client.session_logs(&self.job_id).await?;
if let Some(lines_blob) = result.0.get("amalthea-session") {
let lines: Vec<&str> = lines_blob.lines().collect();
if lines.len() > seen {
Expand All @@ -63,20 +59,16 @@ impl Input {
Ok(seen)
}

async fn is_session_finished(&self, ctx: &Context) -> Result<bool, Error> {
let details = ctx
.client
.get_session(&self.job_id)
.await
.context(HttpClientSnafu)?;
async fn is_session_finished(&self, ctx: &Context) -> Result<bool, httpclient::Error> {
let details = ctx.client.get_session(&self.job_id).await?;

match &details {
None => Ok(true),
Some(d) => Ok(!d.status.state.is_running()),
}
}

async fn follow_logs(&self, ctx: Context) -> Result<(), Error> {
pub async fn follow_logs(&self, ctx: Context) -> Result<(), httpclient::Error> {
let mut seen: usize = self.show_logs(&ctx, 0).await?;
if self.is_session_finished(&ctx).await? {
return Ok(());
Expand Down
28 changes: 25 additions & 3 deletions src/cli/cmd/job/start.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
cli::complete::complete_job_launcher_id,
data::submission_id::SubmissionId,
cli::{cmd::job::logs, complete::complete_job_launcher_id},
data::{simple_message::SimpleMessage, submission_id::SubmissionId},
httpclient::{self, data::SessionStartRequest},
};

Expand Down Expand Up @@ -33,6 +33,10 @@ pub struct Input {
#[arg(long)]
pub command: Vec<String>,

/// Start the job and show the logs until it ends or the user cancels with Ctrl-C.
#[arg(long, default_value_t = false)]
pub wait: bool,

/// These arguments are passed to the renku job command.
#[arg(trailing_var_arg = true, allow_hyphen_values = true, num_args = 0.., value_name = "ARGS")]
pub passthrough: Vec<String>,
Expand Down Expand Up @@ -76,6 +80,24 @@ impl Input {
.await
.context(HttpClientSnafu)?;

ctx.write_result(&result).await.context(WriteResultSnafu)
if self.wait {
ctx.write_result(&SimpleMessage {
message: format!(
"Started job {} (submission_id: {}). Waiting for logs...",
result.name,
result.submission_id.unwrap_or("-".to_string())
),
})
.await
.context(WriteResultSnafu)?;
let log_input = logs::Input {
job_id: result.name,
follow: true,
follow_interval: 2,
};
log_input.follow_logs(ctx).await.context(HttpClientSnafu)
} else {
ctx.write_result(&result).await.context(WriteResultSnafu)
}
}
}
Loading