Skip to content

Commit

Permalink
post status
Browse files Browse the repository at this point in the history
  • Loading branch information
isaaguilar committed Sep 15, 2023
1 parent e6e3742 commit 18e92af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ edition = "2021"
[dependencies]
reqwest = { version = "0.11", features = ["blocking", "json"] }
tokio = { version = "1", features = ["full"] }
base64 = "0.21.3"
serde = { version = "1.0.104", features = ["derive"] }
serde_json = "1.0.105"
72 changes: 45 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::process::exit;
use std::thread;
use std::time::Duration;

Expand All @@ -8,13 +10,6 @@ use serde::Deserialize;
use serde_json;
use tokio;

// Query status of this resource (cluster/namespace/name)
// Parse the json response of status
// Fail if not status 200
// On data, complete on wf completion with status report
// Restart policy shall be OnFailure?
// TTL should be 0 to clean up environment quickly?

#[derive(Debug, Deserialize)]
struct Response {
status_info: StatusInfo,
Expand All @@ -26,6 +21,10 @@ impl Response {
self.status_info.status_code == 200
}

fn is_unauthorized(&self) -> bool {
self.status_info.status_code == 401
}

fn is_complete(&self) -> bool {
if self.data.len() == 0 {
return false;
Expand All @@ -45,7 +44,7 @@ struct DataItem {
did_start: bool,
did_complete: bool,
current_state: String,
current_task: String,
// current_task: String,
}

struct APIClient {
Expand All @@ -62,17 +61,17 @@ impl APIClient {
}

#[tokio::main]
async fn status_check(&self) -> Result<&str, Box<dyn Error>> {
async fn status_check(&self) -> Result<(), Box<dyn Error>> {
let url = format!("{}/api/v1/task/status", self.url);

let client = reqwest::Client::new();

let mut token_header = reqwest::header::HeaderMap::new();
token_header.insert(
"Token",
reqwest::header::HeaderValue::from_str(self.token.as_str()).unwrap(),
);
loop {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
"Token",
reqwest::header::HeaderValue::from_str(self.token.as_str()).unwrap(),
);
let headers = token_header.clone();

let body = client
.get(url.as_str())
Expand All @@ -85,20 +84,33 @@ impl APIClient {
let response: Response =
serde_json::from_str(&body).expect("response body in wrong format");

if response.is_status_ok() & response.is_complete() {
// send a query to update status in database
println!("{}", response.data[0].current_state);
break;
} else if !response.is_status_ok() {
println!("{}", response.status_info.message);
} else if !response.is_complete() {
println!("workflow is still running");
if response.is_status_ok() {
let headers = token_header.clone();
let mut map = HashMap::new();
map.insert("status", response.data[0].current_state.as_str());
client
.post(url.as_str())
.headers(headers)
.json(&map)
.send()
.await?;

if !response.is_complete() {
println!("workflow is still running");
} else {
println!("workflow is {}", response.data[0].current_state);
break;
}
} else {
println!("status query failed with {}", response.status_info.message);
if response.is_unauthorized() {
// The token has expired and future calls will break
break;
}
}

thread::sleep(Duration::from_secs(30));
}

Ok((""))
Ok(())
}
}

Expand All @@ -107,5 +119,11 @@ fn main() {
let token = env::var("TFO_API_LOG_TOKEN").expect("$TFO_API_LOG_TOKEN is not set");
let client = APIClient::new(url.as_str(), token.as_str());

let response = client.status_check();
match client.status_check() {
Ok(()) => exit(0),
Err(err) => {
println!("{}", err);
exit(1)
}
};
}

0 comments on commit 18e92af

Please sign in to comment.