-
Notifications
You must be signed in to change notification settings - Fork 6
cancel all possible tasks #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -676,6 +676,7 @@ func (execCtx *executionContext) retrieveLogs(ctx context.Context) error { | |
|
|
||
| } | ||
| func (e *commandContext) Cleanup(ctx context.Context, jobID string, c *cluster.Cluster) error { | ||
|
|
||
| cleanupMethod.CountRequest() | ||
| // Resolve cluster context to get cluster name | ||
| clusterContext := &clusterContext{} | ||
|
|
@@ -691,8 +692,10 @@ func (e *commandContext) Cleanup(ctx context.Context, jobID string, c *cluster.C | |
| ecsClient := ecs.NewFromConfig(cfg) | ||
|
|
||
| // List all tasks started by this job | ||
| var allTaskARNs []string | ||
| for taskNum := 0; taskNum < e.TaskCount; taskNum++ { | ||
| maxTaskCount := clusterContext.MaxTaskCount | ||
|
|
||
| taskARNs := make(map[string]struct{}) | ||
| for taskNum := 0; taskNum < maxTaskCount; taskNum++ { | ||
| startedByValue := fmt.Sprintf("%s%s-%d", startedByPrefix, jobID, taskNum) | ||
|
|
||
| listTasksOutput, err := ecsClient.ListTasks(ctx, &ecs.ListTasksInput{ | ||
|
|
@@ -703,44 +706,33 @@ func (e *commandContext) Cleanup(ctx context.Context, jobID string, c *cluster.C | |
| cleanupMethod.CountError("list_tasks") | ||
| return err | ||
| } | ||
| allTaskARNs = append(allTaskARNs, listTasksOutput.TaskArns...) | ||
|
|
||
| for _, arn := range listTasksOutput.TaskArns { | ||
| taskARNs[arn] = struct{}{} | ||
| } | ||
|
|
||
| time.Sleep(100 * time.Millisecond) // prevent API throttling | ||
| } | ||
|
|
||
| if len(allTaskARNs) == 0 { | ||
| if len(taskARNs) == 0 { | ||
| // No tasks found, nothing to clean up | ||
| cleanupMethod.CountSuccess("no_tasks_found") | ||
| return nil | ||
| } | ||
|
|
||
| // Bulk describe all tasks to check their LastStatus | ||
| describeOutput, err := ecsClient.DescribeTasks(ctx, &ecs.DescribeTasksInput{ | ||
| Cluster: aws.String(clusterContext.ClusterName), | ||
| Tasks: allTaskARNs, | ||
| }) | ||
| if err != nil { | ||
| cleanupMethod.CountError("describe_tasks") | ||
| return err | ||
| } | ||
|
|
||
| // Stop all tasks where LastStatus != STOPPED or SUCCEEDED | ||
| for _, task := range describeOutput.Tasks { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not necessary. The API will not fail if you try to stop an already-stopped task. |
||
| // Skip tasks that are already stopped | ||
| if aws.ToString(task.LastStatus) == "STOPPED" || aws.ToString(task.LastStatus) == "SUCCEEDED" { | ||
| continue | ||
| } | ||
|
|
||
| // Stop all tasks we found. StopTask is safe to call even if the task is already stopping/stopped. | ||
| for taskARN := range taskARNs { | ||
| stopTaskInput := &ecs.StopTaskInput{ | ||
| Cluster: aws.String(clusterContext.ClusterName), | ||
| Task: task.TaskArn, | ||
| Task: aws.String(taskARN), | ||
| Reason: aws.String(errJobTerminated), | ||
| } | ||
| _, err := ecsClient.StopTask(ctx, stopTaskInput) | ||
| if err != nil { | ||
| if _, err := ecsClient.StopTask(ctx, stopTaskInput); err != nil { | ||
| // Log error but continue stopping other tasks | ||
| cleanupMethod.LogAndCountError(err, fmt.Sprintf("failed to stop task %s", aws.ToString(task.TaskArn))) | ||
| continue | ||
| cleanupMethod.LogAndCountError(err, fmt.Sprintf("failed to stop task %s", taskARN)) | ||
|
||
| } | ||
| cleanupMethod.CountSuccess("stop_task") | ||
|
|
||
| time.Sleep(100 * time.Millisecond) // prevent API throttling | ||
| } | ||
| cleanupMethod.CountSuccess() | ||
| return nil | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use list