github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/toolsversionchecker/worker.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package toolsversionchecker
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/juju/worker.v1"
    11  
    12  	jworker "github.com/juju/juju/worker"
    13  )
    14  
    15  // VersionCheckerParams holds params for the version checker worker..
    16  type VersionCheckerParams struct {
    17  	CheckInterval time.Duration
    18  }
    19  
    20  type Facade interface {
    21  	UpdateToolsVersion() error
    22  }
    23  
    24  // New returns a worker that periodically wakes up to try to find out and
    25  // record the latest version of the tools so the update possibility can be
    26  // displayed to the users on status.
    27  var New = func(api Facade, params *VersionCheckerParams) worker.Worker {
    28  	w := &toolsVersionWorker{
    29  		api:    api,
    30  		params: params,
    31  	}
    32  
    33  	f := func(stop <-chan struct{}) error {
    34  		return w.doCheck()
    35  	}
    36  	return jworker.NewPeriodicWorker(f, params.CheckInterval, jworker.NewTimer)
    37  }
    38  
    39  type toolsVersionWorker struct {
    40  	api    Facade
    41  	params *VersionCheckerParams
    42  }
    43  
    44  func (w *toolsVersionWorker) doCheck() error {
    45  	err := w.api.UpdateToolsVersion()
    46  	return errors.Annotate(err, "cannot update agent binaries information")
    47  }