|
| 1 | +// Copyright 2024 Jakub Zelenka and The WST Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sequential |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "github.com/pkg/errors" |
| 20 | + "github.com/wstool/wst/app" |
| 21 | + "github.com/wstool/wst/conf/types" |
| 22 | + "github.com/wstool/wst/run/actions/action" |
| 23 | + "github.com/wstool/wst/run/instances/runtime" |
| 24 | + "github.com/wstool/wst/run/services" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +type Maker interface { |
| 29 | + Make( |
| 30 | + config *types.SequentialAction, |
| 31 | + sl services.ServiceLocator, |
| 32 | + defaultTimeout int, |
| 33 | + actionMaker action.Maker, |
| 34 | + ) (action.Action, error) |
| 35 | +} |
| 36 | + |
| 37 | +type ActionMaker struct { |
| 38 | + fnd app.Foundation |
| 39 | + runtimeMaker runtime.Maker |
| 40 | +} |
| 41 | + |
| 42 | +func CreateActionMaker(fnd app.Foundation, runtimeMaker runtime.Maker) *ActionMaker { |
| 43 | + return &ActionMaker{ |
| 44 | + fnd: fnd, |
| 45 | + runtimeMaker: runtimeMaker, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (m *ActionMaker) Make( |
| 50 | + config *types.SequentialAction, |
| 51 | + sl services.ServiceLocator, |
| 52 | + defaultTimeout int, |
| 53 | + actionMaker action.Maker, |
| 54 | +) (action.Action, error) { |
| 55 | + if config.Timeout == 0 { |
| 56 | + config.Timeout = defaultTimeout |
| 57 | + } |
| 58 | + |
| 59 | + var sequentialActions []action.Action |
| 60 | + for _, configAction := range config.Actions { |
| 61 | + newAction, err := actionMaker.MakeAction(configAction, sl, config.Timeout) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + sequentialActions = append(sequentialActions, newAction) |
| 66 | + } |
| 67 | + return &Action{ |
| 68 | + fnd: m.fnd, |
| 69 | + runtimeMaker: m.runtimeMaker, |
| 70 | + actions: sequentialActions, |
| 71 | + timeout: time.Duration(config.Timeout * 1e6), |
| 72 | + when: action.When(config.When), |
| 73 | + }, nil |
| 74 | +} |
| 75 | + |
| 76 | +type Action struct { |
| 77 | + fnd app.Foundation |
| 78 | + runtimeMaker runtime.Maker |
| 79 | + actions []action.Action |
| 80 | + timeout time.Duration |
| 81 | + when action.When |
| 82 | +} |
| 83 | + |
| 84 | +func (a *Action) When() action.When { |
| 85 | + return a.when |
| 86 | +} |
| 87 | + |
| 88 | +func (a *Action) Timeout() time.Duration { |
| 89 | + return a.timeout |
| 90 | +} |
| 91 | + |
| 92 | +func (a *Action) Execute(ctx context.Context, runData runtime.Data) (bool, error) { |
| 93 | + logger := a.fnd.Logger() |
| 94 | + logger.Infof("Executing sequential action") |
| 95 | + |
| 96 | + failedActionsCount := 0 |
| 97 | + var lastErr error = nil |
| 98 | + for pos, act := range a.actions { |
| 99 | + // Execute action with context |
| 100 | + when := act.When() |
| 101 | + if when == action.Always || |
| 102 | + (failedActionsCount == 0 && when == action.OnSuccess) || |
| 103 | + (failedActionsCount > 0 && when == action.OnFailure) { |
| 104 | + actTimeout := act.Timeout() |
| 105 | + logger.Debugf("Executing sequential action %d with timeout %s", pos, actTimeout) |
| 106 | + // Create context for action |
| 107 | + actCtx, cancel := a.runtimeMaker.MakeContextWithTimeout(ctx, actTimeout) |
| 108 | + success, err := act.Execute(actCtx, runData) |
| 109 | + cancel() // Cancel the context immediately after action completion |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + lastErr = err |
| 113 | + logger.Errorf("Sequential action %d failed with error: %v", pos, err) |
| 114 | + } |
| 115 | + |
| 116 | + if !success { |
| 117 | + failedActionsCount++ |
| 118 | + logger.Debugf("Sequential action %d failed", pos) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if a.fnd.DryRun() { |
| 124 | + return true, nil |
| 125 | + } |
| 126 | + |
| 127 | + result := true |
| 128 | + if failedActionsCount > 0 { |
| 129 | + logger.Debugf("Sequential action failed on %d actions", failedActionsCount) |
| 130 | + result = false |
| 131 | + } |
| 132 | + |
| 133 | + if lastErr != nil { |
| 134 | + return result, errors.Errorf("Sequential action failed with error: %v", lastErr) |
| 135 | + } |
| 136 | + |
| 137 | + return result, lastErr |
| 138 | +} |
0 commit comments