Skip to content

Commit

Permalink
Handle case where task is not yet created when we try to fetch info o…
Browse files Browse the repository at this point in the history
…n it
  • Loading branch information
dalen committed Aug 16, 2018
1 parent 526c8bd commit 64a6911
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,27 @@ fn main() {
let mut previous_status = task.clone();
println!("Started task {}", &task_id);
loop {
let task_status = fetch_task(&ecs_client, &cluster.to_string(), &task);
match fetch_task(&ecs_client, &cluster.to_string(), &task) {
// Task is likely not started yet, retry in a while
None => thread::sleep(time::Duration::from_millis(500)),
// Task was started, continue
Some(task_status) => {
if task_status.stopped_at != None {
break;
}

if task_status.stopped_at != None {
break;
}

// Check if status has changed
if let (Some(ref old), Some(ref new)) =
(&task_status.last_status, &previous_status.last_status)
{
if old != new {
println!("Status: {}", new);
// Check if status has changed
if let (Some(ref old), Some(ref new)) =
(&task_status.last_status, &previous_status.last_status)
{
if old != new {
println!("Status: {}", new);
}
}
thread::sleep(time::Duration::from_millis(500));
previous_status = task_status;
}
}
thread::sleep(time::Duration::from_millis(500));
previous_status = task_status;
}
println!("Task finished, fetching logs");

Expand Down Expand Up @@ -168,7 +173,11 @@ fn fetch_logs(
result.unwrap()
}

fn fetch_task(client: &EcsClient, cluster: &str, task: &rusoto_ecs::Task) -> rusoto_ecs::Task {
fn fetch_task(
client: &EcsClient,
cluster: &str,
task: &rusoto_ecs::Task,
) -> Option<rusoto_ecs::Task> {
let task_arn = task.clone().task_arn.unwrap();

let result = client
Expand All @@ -182,12 +191,9 @@ fn fetch_task(client: &EcsClient, cluster: &str, task: &rusoto_ecs::Task) -> rus
.tasks
.expect("Task definition response contained no tasks");
if tasks.len() == 0 {
panic!(format!(
"No task definitions matched cluster: {} arn: {}",
&cluster, &task_arn
))
None
} else {
tasks[0].clone()
Some(tasks[0].clone())
}
}

Expand Down

0 comments on commit 64a6911

Please sign in to comment.