(ctx *funcletCtx.Context, params *api.ResetRequest)
| 57 | } |
| 58 | |
| 59 | func (f *Funclet) ResetContainers(ctx *funcletCtx.Context, params *api.ResetRequest) (err error, response *api.ResetResponse) { |
| 60 | ctx.Logger.Infof("start reset container") |
| 61 | defer func() { |
| 62 | if err != nil { |
| 63 | ctx.Logger.Infof("reset container failed: %s", err) |
| 64 | } else { |
| 65 | ctx.Logger.Infof("reset container success") |
| 66 | } |
| 67 | }() |
| 68 | |
| 69 | var needScaleDown bool |
| 70 | if params.ScaleDownRecommendation != nil { |
| 71 | needScaleDown = true |
| 72 | response = api.NewResetResponse() |
| 73 | } |
| 74 | |
| 75 | // reset target container |
| 76 | if err := f.ResetContainer(ctx); err != nil { |
| 77 | if !needScaleDown { |
| 78 | return err, nil |
| 79 | } |
| 80 | IDs := params.ScaleDownRecommendation.ResetContainers |
| 81 | IDs = append(IDs, ctx.Container.ContainerID) |
| 82 | |
| 83 | if list, listErr := f.BulkGetContainers(IDs); listErr == nil { |
| 84 | response.ScaleDownResult.Fails = list |
| 85 | } |
| 86 | return err, response |
| 87 | } |
| 88 | |
| 89 | if !needScaleDown { |
| 90 | return nil, nil |
| 91 | } |
| 92 | |
| 93 | failedIDs := make([]string, 0) |
| 94 | successIDs := make([]string, 0) |
| 95 | wg := sync.WaitGroup{} |
| 96 | wg.Add(len(params.ScaleDownRecommendation.ResetContainers)) |
| 97 | for _, cID := range params.ScaleDownRecommendation.ResetContainers { |
| 98 | go func(ID string) { |
| 99 | info, _ := f.ContainerManager.ContainerMap.GetContainer(ID) |
| 100 | newCtx := funcletCtx.Context{RequestID: ctx.RequestID, Logger: ctx.Logger} |
| 101 | newCtx.SetContainer(info) |
| 102 | if err := f.ResetContainer(&newCtx); err != nil { |
| 103 | failedIDs = append(failedIDs, ID) |
| 104 | } else { |
| 105 | successIDs = append(successIDs, ID) |
| 106 | } |
| 107 | wg.Done() |
| 108 | }(cID) |
| 109 | } |
| 110 | wg.Wait() |
| 111 | |
| 112 | response.ScaleDownResult.Success = successIDs |
| 113 | if list, listErr := f.BulkGetContainers(failedIDs); listErr == nil { |
| 114 | response.ScaleDownResult.Fails = list |
| 115 | } |
| 116 | return nil, response |
no test coverage detected