github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"gopkg.in/juju/worker.v1"
    10  	"gopkg.in/juju/worker.v1/dependency"
    11  
    12  	"github.com/juju/juju/agent"
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/cmd/jujud/agent/engine"
    16  )
    17  
    18  // ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
    19  type ManifoldConfig struct {
    20  	AgentName     string
    21  	APICallerName string
    22  	NewFacade     func(base.APICaller) Facade
    23  	NewWorker     func(WorkerConfig) (worker.Worker, error)
    24  }
    25  
    26  // Manifold returns a dependency manifold that runs a hook retry strategy worker,
    27  // using the agent name and the api connection resources named in the supplied config.
    28  func Manifold(config ManifoldConfig) dependency.Manifold {
    29  	typedConfig := engine.AgentAPIManifoldConfig{
    30  		AgentName:     config.AgentName,
    31  		APICallerName: config.APICallerName,
    32  	}
    33  	manifold := engine.AgentAPIManifold(typedConfig, config.start)
    34  	manifold.Output = config.output
    35  	return manifold
    36  }
    37  
    38  func (mc ManifoldConfig) start(a agent.Agent, apiCaller base.APICaller) (worker.Worker, error) {
    39  	agentTag := a.CurrentConfig().Tag()
    40  	retryStrategyFacade := mc.NewFacade(apiCaller)
    41  	initialRetryStrategy, err := retryStrategyFacade.RetryStrategy(agentTag)
    42  	if err != nil {
    43  		return nil, errors.Trace(err)
    44  	}
    45  	return mc.NewWorker(WorkerConfig{
    46  		Facade:        retryStrategyFacade,
    47  		AgentTag:      agentTag,
    48  		RetryStrategy: initialRetryStrategy,
    49  	})
    50  }
    51  
    52  func (mc ManifoldConfig) output(in worker.Worker, out interface{}) error {
    53  	inWorker, _ := in.(*RetryStrategyWorker)
    54  	if inWorker == nil {
    55  		return errors.Errorf("in should be a *retryStrategyWorker; is %T", in)
    56  	}
    57  	switch outPointer := out.(type) {
    58  	case *params.RetryStrategy:
    59  		*outPointer = inWorker.GetRetryStrategy()
    60  	default:
    61  		return errors.Errorf("out should be a *params.RetryStrategy; is %T", out)
    62  
    63  	}
    64  	return nil
    65  }