From a1afac3b748dcbefca66b4b9e2a8df2b44d29459 Mon Sep 17 00:00:00 2001 From: stormcat24 Date: Mon, 27 Jul 2015 01:15:42 +0900 Subject: [PATCH] :bug: stopped wathing when task stopped #12 --- service/service.go | 48 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/service/service.go b/service/service.go index 257482d..32edb7a 100644 --- a/service/service.go +++ b/service/service.go @@ -16,6 +16,14 @@ import ( "errors" ) +type TaskWatchStatus int + +const ( + WatchContinue TaskWatchStatus = iota + WatchFinish + WatchTerminate +) + type ServiceController struct { Ecs *aws.ECSManager TargetResource string @@ -259,6 +267,9 @@ func (self *ServiceController) WaitActiveService(cluster string, service string) api := self.Ecs.ServiceApi() taskApi := self.Ecs.TaskApi() + var flag = false + var taskARNs []*string + for { time.Sleep(5 * time.Second) @@ -276,36 +287,47 @@ func (self *ServiceController) WaitActiveService(cluster string, service string) // The status of the service. The valid values are ACTIVE, DRAINING, or INACTIVE. logger.Main.Infof("service '%s@%s' status = %s ...", service, cluster, *target.Status) + if *target.Status == "ACTIVE" { if len(target.Events) > 0 && strings.Contains(*target.Events[0].Message, "was unable to place a task"){ return errors.New(*target.Events[0].Message) } - reslt, errlt := taskApi.ListTasks(cluster, service) - if errlt != nil { - return errlt - } - - if len(reslt.TaskARNs) == 0 { - continue + if !flag { + reslt, errlt := taskApi.ListTasks(cluster, service) + if errlt != nil { + return errlt + } + + if len(reslt.TaskARNs) == 0 { + continue + } else { + taskARNs = reslt.TaskARNs + flag = true + } } - resdt, errdt := taskApi.DescribeTasks(cluster, reslt.TaskARNs) + resdt, errdt := taskApi.DescribeTasks(cluster, taskARNs) if errdt != nil { return errdt } - if self.checkRunningTask(resdt) { + + watchStatus := self.checkRunningTask(resdt) + if watchStatus == WatchFinish { logger.Main.Info("At least one of task has started successfully.") return nil + } else if watchStatus == WatchTerminate { + logger.Main.Error("Stopped watching task, because task has stopped.") + return errors.New("Task has been stopped for some reason.") } } } } -func (self *ServiceController) checkRunningTask(dto *ecs.DescribeTasksOutput) bool { +func (self *ServiceController) checkRunningTask(dto *ecs.DescribeTasksOutput) TaskWatchStatus { logger.Main.Info("Current task conditions as follows:") @@ -328,11 +350,13 @@ func (self *ServiceController) checkRunningTask(dto *ecs.DescribeTasksOutput) bo // if RUNNING at least one, ecs-formation deals with ok. for _, s := range status { if s == "RUNNING" { - return true + return WatchFinish + } else if s == "STOPPED" { + return WatchTerminate } } - return false + return WatchContinue } func (self *ServiceController) RoundColorStatus(status string) *color.Escape {