github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controllerutil/requeue_errors.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package controllerutil
    21  
    22  import (
    23  	"fmt"
    24  	"time"
    25  )
    26  
    27  type RequeueError interface {
    28  	RequeueAfter() time.Duration
    29  	Reason() string
    30  }
    31  
    32  type DelayedRequeueError interface {
    33  	RequeueError
    34  	Delayed()
    35  }
    36  
    37  func NewRequeueError(after time.Duration, reason string) error {
    38  	return &requeueError{
    39  		reason:       reason,
    40  		requeueAfter: after,
    41  	}
    42  }
    43  
    44  // NewDelayedRequeueError creates a delayed requeue error which only returns in the last step of the DAG.
    45  func NewDelayedRequeueError(after time.Duration, reason string) error {
    46  	return &delayedRequeueError{
    47  		requeueError{
    48  			reason:       reason,
    49  			requeueAfter: after,
    50  		},
    51  	}
    52  }
    53  
    54  func IsDelayedRequeueError(err error) bool {
    55  	_, ok := err.(DelayedRequeueError)
    56  	return ok
    57  }
    58  
    59  // IsRequeueError checks if the error is the RequeueError.
    60  func IsRequeueError(err error) bool {
    61  	_, ok := err.(RequeueError)
    62  	return ok
    63  }
    64  
    65  type requeueError struct {
    66  	reason       string
    67  	requeueAfter time.Duration
    68  }
    69  
    70  type delayedRequeueError struct {
    71  	requeueError
    72  }
    73  
    74  var _ RequeueError = &requeueError{}
    75  var _ DelayedRequeueError = &delayedRequeueError{}
    76  
    77  func (r *requeueError) Error() string {
    78  	return fmt.Sprintf("requeue after: %v as: %s", r.requeueAfter, r.reason)
    79  }
    80  
    81  func (r *requeueError) RequeueAfter() time.Duration {
    82  	return r.requeueAfter
    83  }
    84  
    85  func (r *requeueError) Reason() string {
    86  	return r.reason
    87  }
    88  
    89  func (r *delayedRequeueError) Delayed() {}