@@ -35,6 +35,7 @@ import (
3535 "github.com/arangodb/kube-arangodb/pkg/deployment/member"
3636 "github.com/arangodb/kube-arangodb/pkg/deployment/reconciler"
3737 "github.com/arangodb/kube-arangodb/pkg/logging"
38+ "github.com/arangodb/kube-arangodb/pkg/util"
3839 "github.com/arangodb/kube-arangodb/pkg/util/errors"
3940 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
4041 inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
@@ -104,6 +105,11 @@ type ActionLocalsContext interface {
104105
105106 Get (action api.Action , key api.PlanLocalKey ) (string , bool )
106107 Add (key api.PlanLocalKey , value string , override bool ) bool
108+
109+ SetTime (key api.PlanLocalKey , t time.Time ) bool
110+ GetTime (action api.Action , key api.PlanLocalKey ) (time.Time , bool )
111+
112+ BackoffExecution (action api.Action , key api.PlanLocalKey , duration time.Duration ) bool
107113}
108114
109115// newActionContext creates a new ActionContext implementation.
@@ -144,6 +150,39 @@ func (ac *actionContext) Get(action api.Action, key api.PlanLocalKey) (string, b
144150 return ac .locals .GetWithParent (action .Locals , key )
145151}
146152
153+ func (ac * actionContext ) BackoffExecution (action api.Action , key api.PlanLocalKey , duration time.Duration ) bool {
154+ t , ok := ac .GetTime (action , key )
155+ if ! ok {
156+ // Reset as zero time
157+ t = time.Time {}
158+ }
159+
160+ if t .IsZero () || time .Since (t ) > duration {
161+ // Execution is needed
162+ ac .SetTime (key , time .Now ())
163+ return true
164+ }
165+
166+ return false
167+ }
168+
169+ func (ac * actionContext ) SetTime (key api.PlanLocalKey , t time.Time ) bool {
170+ return ac .Add (key , t .Format (util .TimeLayout ), true )
171+ }
172+
173+ func (ac * actionContext ) GetTime (action api.Action , key api.PlanLocalKey ) (time.Time , bool ) {
174+ s , ok := ac .locals .GetWithParent (action .Locals , key )
175+ if ! ok {
176+ return time.Time {}, false
177+ }
178+
179+ if t , err := time .Parse (util .TimeLayout , s ); err != nil {
180+ return time.Time {}, false
181+ } else {
182+ return t , true
183+ }
184+ }
185+
147186func (ac * actionContext ) Add (key api.PlanLocalKey , value string , override bool ) bool {
148187 return ac .locals .Add (key , value , override )
149188}
0 commit comments