Skip to content

Commit

Permalink
Improve logging flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Oct 18, 2023
1 parent 77e406d commit c512880
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ reqwest = { version = "0.11.20", default-features = false, features = [
] }
serde = "1"
serde_json = "1"
termion = "2.0.1"
thiserror = "1.0.32"
tokio = { version = "1.18.2", features = ["full", "sync"] }
tower-http = "0.4"
Expand Down
17 changes: 13 additions & 4 deletions src/command/deployments/create.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#![allow(clippy::enum_variant_names)]

use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};

use anyhow::Result;
use clap::Args;
use graphql_client::{GraphQLQuery, Response};
use termion::event::Key;
use termion::input::TermRead;

use self::create_deployment::ServiceInput;
use crate::{
Expand Down Expand Up @@ -122,12 +129,14 @@ impl CreateArgs {
}

let service = match &self.create_commands {
CreateCommands::Katana(_) => Service::Katana,
CreateCommands::Torii(_) => Service::Torii,
CreateCommands::Katana(_) => "katana",
CreateCommands::Torii(_) => "torii",
};

let reader = LogReader::new(service, self.project.clone());
reader.stream(None).await?;
println!(
"\nStream logs with `slot deployments logs {} {service} -f`",
self.project
);

Ok(())
}
Expand Down
31 changes: 22 additions & 9 deletions src/command/deployments/logs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashSet,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Expand All @@ -13,7 +14,7 @@ use tokio::time::sleep;

use crate::{api::ApiClient, command::deployments::logs::deployment_logs::DeploymentService};

use self::deployment_logs::{ResponseData, Variables};
use self::deployment_logs::{DeploymentLogsDeploymentLogs, ResponseData, Variables};

use super::services::Service;

Expand Down Expand Up @@ -56,7 +57,8 @@ impl LogsArgs {
if self.follow {
reader.stream(self.since.clone()).await?;
} else {
reader.query(self.since.clone(), self.limit).await?;
let logs = reader.query(self.since.clone(), self.limit).await?;
println!("{}", logs.content);
}

Ok(())
Expand All @@ -78,7 +80,11 @@ impl LogReader {
}
}

pub async fn query(&self, since: Option<String>, limit: i64) -> Result<String> {
pub async fn query(
&self,
since: Option<String>,
limit: i64,
) -> Result<DeploymentLogsDeploymentLogs> {
let service = match self.service {
Service::Katana => DeploymentService::katana,
Service::Torii => DeploymentService::torii,
Expand Down Expand Up @@ -107,9 +113,7 @@ impl LogReader {
.map(|deployment| deployment.logs)
.unwrap();

println!("{}", logs.content);

Ok(logs.until)
Ok(logs)
}

pub async fn stream(&self, since: Option<String>) -> Result<()> {
Expand All @@ -120,11 +124,20 @@ impl LogReader {
})
.expect("Error setting Ctrl-C handler");

let mut since = self.query(since, 25).await?;
let mut logs = self.query(since, 1).await?;
let mut printed_logs = HashSet::new();

let mut since = logs.until;
while running.load(Ordering::SeqCst) {
println!("{since}");
sleep(Duration::from_millis(1000)).await;
since = self.query(Some(since.clone()), 25).await?;
logs = self.query(Some(since.clone()), 25).await?;

if !printed_logs.contains(&logs.content) {
println!("{}", logs.content);
printed_logs.insert(logs.content.clone()); // Add the log to the buffer
}

since = logs.until
}

Ok(())
Expand Down

0 comments on commit c512880

Please sign in to comment.