|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/aws/amazon-ecs-agent/ecs-agent/acs/model/ecsacs" |
| 7 | + "github.com/aws/amazon-ecs-agent/ecs-agent/logger" |
| 8 | + "github.com/aws/amazon-ecs-agent/ecs-agent/logger/field" |
| 9 | + "github.com/aws/amazon-ecs-agent/ecs-agent/metrics" |
| 10 | + "github.com/aws/amazon-ecs-agent/ecs-agent/wsclient" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go/aws" |
| 13 | + "github.com/pkg/errors" |
| 14 | +) |
| 15 | + |
| 16 | +type TaskStopper interface { |
| 17 | + StopTask(taskARN string) |
| 18 | +} |
| 19 | + |
| 20 | +const ( |
| 21 | + TaskStopVerificationACKMessageName = "TaskStopVerificationACKMessage" |
| 22 | +) |
| 23 | + |
| 24 | +// taskStopVerificationACKResponder processes task stop verification ACK messages from ACS. |
| 25 | +// It processes the message and sets the desired status of all tasks from the message to STOPPED. |
| 26 | +type taskStopVerificationACKResponder struct { |
| 27 | + taskStopper TaskStopper |
| 28 | + messageIDAccessor ManifestMessageIDAccessor |
| 29 | + metricsFactory metrics.EntryFactory |
| 30 | + respond wsclient.RespondFunc |
| 31 | +} |
| 32 | + |
| 33 | +// NewTaskStopVerificationACKResponder returns an instance of the taskStopVerificationACKResponder struct. |
| 34 | +func NewTaskStopVerificationACKResponder( |
| 35 | + taskStopper TaskStopper, |
| 36 | + messageIDAccessor ManifestMessageIDAccessor, |
| 37 | + metricsFactory metrics.EntryFactory, |
| 38 | + responseSender wsclient.RespondFunc) *taskStopVerificationACKResponder { |
| 39 | + r := &taskStopVerificationACKResponder{ |
| 40 | + taskStopper: taskStopper, |
| 41 | + messageIDAccessor: messageIDAccessor, |
| 42 | + metricsFactory: metricsFactory, |
| 43 | + } |
| 44 | + r.respond = ResponseToACSSender(r.Name(), responseSender) |
| 45 | + return r |
| 46 | +} |
| 47 | + |
| 48 | +func (*taskStopVerificationACKResponder) Name() string { return "task stop verification ACK responder" } |
| 49 | + |
| 50 | +func (r *taskStopVerificationACKResponder) HandlerFunc() wsclient.RequestHandler { |
| 51 | + return r.handleTaskStopVerificationACK |
| 52 | +} |
| 53 | + |
| 54 | +// handleTaskStopVerificationACK goes through the list of verified tasks to be stopped |
| 55 | +// and stops each one by setting the desired status of each task to STOPPED. |
| 56 | +func (r *taskStopVerificationACKResponder) handleTaskStopVerificationACK(message *ecsacs.TaskStopVerificationAck) error { |
| 57 | + logger.Debug(fmt.Sprintf("Handling %s", TaskStopVerificationACKMessageName)) |
| 58 | + |
| 59 | + // Ensure that message is valid and that a corresponding task manifest message has been processed before. |
| 60 | + ackMessageID := aws.StringValue(message.MessageId) |
| 61 | + manifestMessageID := r.messageIDAccessor.GetMessageID() |
| 62 | + if ackMessageID == "" || ackMessageID != manifestMessageID { |
| 63 | + return errors.New("Invalid messageID received: " + ackMessageID + " Manifest Message ID: " + manifestMessageID) |
| 64 | + } |
| 65 | + |
| 66 | + // Reset the message id so that the message with same message id is not processed twice. |
| 67 | + r.messageIDAccessor.SetMessageID("") |
| 68 | + |
| 69 | + // Loop through all tasks in the verified stop list and set the desired status of each one to STOPPED. |
| 70 | + tasksToStop := message.StopTasks |
| 71 | + for _, task := range tasksToStop { |
| 72 | + taskARN := aws.StringValue(task.TaskArn) |
| 73 | + metricFields := map[string]interface{}{ |
| 74 | + "MessageID": aws.StringValue(message.MessageId), |
| 75 | + "TaskARN": taskARN, |
| 76 | + } |
| 77 | + r.metricsFactory.New(metrics.TaskStoppedMetricName).WithFields(metricFields).Done(nil) |
| 78 | + |
| 79 | + // Send request to the task stopper to stop the task this will be executed asynchronously |
| 80 | + // in the context of the task stopper's message channel. |
| 81 | + logger.Info("Sending message to task stopper to stop task", logger.Fields{ |
| 82 | + field.TaskARN: taskARN, |
| 83 | + }) |
| 84 | + r.taskStopper.StopTask(taskARN) |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
0 commit comments