github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/retrystrategy/manifold.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Copyright 2016 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package retrystrategy
     6  
     7  import (
     8  	"github.com/juju/errors"
     9  	"github.com/juju/worker/v3"
    10  	"github.com/juju/worker/v3/dependency"
    11  
    12  	"github.com/juju/juju/agent"
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/cmd/jujud/agent/engine"
    15  	"github.com/juju/juju/rpc/params"
    16  )
    17  
    18  // Logger represents the methods used by the worker to log information.
    19  type Logger interface {
    20  	Debugf(string, ...interface{})
    21  }
    22  
    23  // logger is here to stop the desire of creating a package level logger.
    24  // Don't do this, instead use the one passed as manifold config.
    25  type logger interface{}
    26  
    27  var _ logger = struct{}{}
    28  
    29  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    30  type ManifoldConfig struct {
    31  	AgentName     string
    32  	APICallerName string
    33  	NewFacade     func(base.APICaller) Facade
    34  	NewWorker     func(WorkerConfig) (worker.Worker, error)
    35  	Logger        Logger
    36  }
    37  
    38  // Manifold returns a dependency manifold that runs a hook retry strategy worker,
    39  // using the agent name and the api connection resources named in the supplied config.
    40  func Manifold(config ManifoldConfig) dependency.Manifold {
    41  	typedConfig := engine.AgentAPIManifoldConfig{
    42  		AgentName:     config.AgentName,
    43  		APICallerName: config.APICallerName,
    44  	}
    45  	manifold := engine.AgentAPIManifold(typedConfig, config.start)
    46  	manifold.Output = config.output
    47  	return manifold
    48  }
    49  
    50  func (mc ManifoldConfig) start(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    51  	agentTag := a.CurrentConfig().Tag()
    52  	retryStrategyFacade := mc.NewFacade(apiCaller)
    53  	initialRetryStrategy, err := retryStrategyFacade.RetryStrategy(agentTag)
    54  	if err != nil {
    55  		return nil, errors.Trace(err)
    56  	}
    57  	return mc.NewWorker(WorkerConfig{
    58  		Facade:        retryStrategyFacade,
    59  		AgentTag:      agentTag,
    60  		RetryStrategy: initialRetryStrategy,
    61  		Logger:        mc.Logger,
    62  	})
    63  }
    64  
    65  func (mc ManifoldConfig) output(in worker.Worker, out interface{}) error {
    66  	inWorker, _ := in.(*RetryStrategyWorker)
    67  	if inWorker == nil {
    68  		return errors.Errorf("in should be a *retryStrategyWorker; is %T", in)
    69  	}
    70  	switch outPointer := out.(type) {
    71  	case *params.RetryStrategy:
    72  		*outPointer = inWorker.GetRetryStrategy()
    73  	default:
    74  		return errors.Errorf("out should be a *params.RetryStrategy; is %T", out)
    75  
    76  	}
    77  	return nil
    78  }