Skip to content

Commit

Permalink
Allow ps on clusters with large task count
Browse files Browse the repository at this point in the history
  • Loading branch information
ipmb committed Mar 25, 2024
1 parent bd924d7 commit 57d1a09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Upgraded to Go 1.22 and updated dependencies

### Fixed

* Allow `ps` to work on clusters with large active task counts

## [4.1.0] - 2023-01-10

### Added
Expand Down
37 changes: 27 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/sirupsen/logrus"
)

const maxEcsDescribeTaskCount = 100

var (
maxLifetime = 12 * 60 * 60
waitForConnect = 60
Expand Down Expand Up @@ -785,33 +787,48 @@ func (a *App) DescribeTasks() ([]*ecs.Task, error) {
return nil, err
}
ecsSvc := ecs.New(a.Session)
var taskARNs []*string
chunkedTaskARNs := [][]*string{{}}
input := ecs.ListTasksInput{
Cluster: &a.Settings.Cluster.ARN,
}
logrus.WithFields(logrus.Fields{"cluster": a.Settings.Cluster.ARN}).Debug("fetching task list")

// handle chunking logic
addTaskARNToChunk := func(taskARN *string) {
if len(chunkedTaskARNs[len(chunkedTaskARNs)-1]) >= maxEcsDescribeTaskCount {
chunkedTaskARNs = append(chunkedTaskARNs, []*string{})
}
chunkedTaskARNs[len(chunkedTaskARNs)-1] = append(chunkedTaskARNs[len(chunkedTaskARNs)-1], taskARN)
}

err = ecsSvc.ListTasksPages(&input, func(resp *ecs.ListTasksOutput, lastPage bool) bool {
for _, taskARN := range resp.TaskArns {
if taskARN == nil {
continue
}
taskARNs = append(taskARNs, taskARN)
addTaskARNToChunk(taskARN)
}

return !lastPage
})
if err != nil {
return nil, err
}
describeTasksOutput, err := ecsSvc.DescribeTasks(&ecs.DescribeTasksInput{
Tasks: taskARNs,
Cluster: &a.Settings.Cluster.ARN,
Include: []*string{aws.String("TAGS")},
})
if err != nil {
return nil, err
var describedTasks []*ecs.Task
for i := range chunkedTaskARNs {
logrus.WithFields(logrus.Fields{"count": len(chunkedTaskARNs[i])}).Debug("fetching task descriptions")
describeTasksOutput, err := ecsSvc.DescribeTasks(&ecs.DescribeTasksInput{
Tasks: chunkedTaskARNs[i],
Cluster: &a.Settings.Cluster.ARN,
Include: []*string{aws.String("TAGS")},
})
if err != nil {
return nil, err
}
describedTasks = append(describedTasks, describeTasksOutput.Tasks...)
}
var appTasks []*ecs.Task
for _, task := range describeTasksOutput.Tasks {
for _, task := range describedTasks {
isApp := false
isReviewApp := false
for _, t := range task.Tags {
Expand Down

0 comments on commit 57d1a09

Please sign in to comment.