Skip to content

Commit

Permalink
Merge pull request #17 from stormcat24/feature/handle_service_error
Browse files Browse the repository at this point in the history
🐛 stopped wathing when task stopped #12
  • Loading branch information
Akinori Yamada committed Jul 26, 2015
2 parents b207963 + a1afac3 commit a92ec1c
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (
"errors"
)

type TaskWatchStatus int

const (
WatchContinue TaskWatchStatus = iota
WatchFinish
WatchTerminate
)

type ServiceController struct {
Ecs *aws.ECSManager
TargetResource string
Expand Down Expand Up @@ -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)

Expand All @@ -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:")

Expand All @@ -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 {
Expand Down

0 comments on commit a92ec1c

Please sign in to comment.