// Manifold creates a metric sender manifold. func Manifold(config ManifoldConfig) dependency.Manifold { return dependency.Manifold{ Inputs: []string{ config.APICallerName, config.MetricSpoolName, }, Start: func(getResource dependency.GetResourceFunc) (worker.Worker, error) { var apicaller base.APICaller var factory spool.MetricFactory err := getResource(config.APICallerName, &apicaller) if err != nil { return nil, errors.Trace(err) } err = getResource(config.MetricSpoolName, &factory) if err != nil { return nil, errors.Trace(err) } client := newMetricAdderClient(apicaller) s := newSender(client, factory) return worker.NewPeriodicWorker(s.Do, period, worker.NewTimer), nil }, } }
// New returns a worker.Worker for history Pruner. func New(params *HistoryPrunerParams) worker.Worker { w := &pruneWorker{ st: st, params: params, } return worker.NewPeriodicWorker(w.doPruning, w.params.PruneInterval, worker.NewTimer) }
// NewWorker returns a worker that lists published cloud // images metadata, and records them in state. func NewWorker(cl *imagemetadata.Client) worker.Worker { // TODO (anastasiamac 2015-09-02) Bug#1491353 - don't ignore stop channel. f := func(stop <-chan struct{}) error { return cl.UpdateFromPublishedImages() } return worker.NewPeriodicWorker(f, updatePublicImageMetadataPeriod, worker.NewTimer) }
// NewWorker returns a worker that lists block devices // attached to the machine, and records them in state. func NewWorker(l ListBlockDevicesFunc, b BlockDeviceSetter) worker.Worker { var old []storage.BlockDevice f := func(stop <-chan struct{}) error { return doWork(l, b, &old) } return worker.NewPeriodicWorker(f, listBlockDevicesPeriod) }
// New returns a worker.Worker for history Pruner. func New(st *state.State, params *HistoryPrunerParams) worker.Worker { w := &pruneWorker{ st: st, params: params, pruner: state.PruneStatusHistory, } return worker.NewPeriodicWorker(w.doPruning, w.params.PruneInterval, worker.NewTimer) }
func NewPruneWorker(st *state.State, params *HistoryPrunerParams, t worker.NewTimerFunc, psh pruneHistoryFunc) worker.Worker { w := &pruneWorker{ st: st, params: params, pruner: psh, } return worker.NewPeriodicWorker(w.doPruning, w.params.PruneInterval, t) }
func NewPeriodicWorkerForTests(api Facade, params *VersionCheckerParams) worker.Worker { w := &toolsVersionWorker{ api: api, params: params, } periodicCall := func(stop <-chan struct{}) error { return w.doCheck() } return worker.NewPeriodicWorker(periodicCall, params.CheckInterval, worker.NewTimer) }
// New returns a worker.Worker for history Pruner. func New(conf Config) (worker.Worker, error) { if err := conf.Validate(); err != nil { return nil, errors.Trace(err) } doPruning := func(stop <-chan struct{}) error { err := conf.Facade.Prune(int(conf.MaxLogsPerEntity)) if err != nil { return errors.Trace(err) } return nil } return worker.NewPeriodicWorker(doPruning, conf.PruneInterval, conf.NewTimer), nil }
// NewSender creates a new periodic worker that sends metrics // to a collection service. func NewSender(client metricsmanager.MetricsManagerClient) worker.Worker { f := func(stopCh <-chan struct{}) error { err := client.SendMetrics() if err != nil { senderLogger.Warningf("failed to send metrics %v - will retry later", err) return nil } select { case notify <- "senderCalled": default: } return nil } return worker.NewPeriodicWorker(f, senderPeriod, worker.NewTimer) }
// NewCleanup creates a new periodic worker that calls the CleanupOldMetrics api. func newCleanup(client metricsmanager.MetricsManagerClient, notify chan string) worker.Worker { f := func(stopCh <-chan struct{}) error { err := client.CleanupOldMetrics() if err != nil { cleanupLogger.Warningf("failed to cleanup %v - will retry later", err) return nil } select { case notify <- "cleanupCalled": default: } return nil } return worker.NewPeriodicWorker(f, cleanupPeriod, worker.NewTimer) }
// NewCleanup creates a new periodic worker that calls the CleanupOldMetrics api. // If a notify channel is provided it will be signalled everytime the call is made. func NewCleanup(client *metricsmanager.Client, notify chan struct{}) worker.Worker { f := func(stopCh <-chan struct{}) error { err := client.CleanupOldMetrics() if err != nil { logger.Warningf("failed to cleanup %v - will retry later", err) return nil } select { case notify <- struct{}{}: default: } return nil } return worker.NewPeriodicWorker(f, time.Second) }
// Manifold returns a collect-metrics manifold. func Manifold(config ManifoldConfig) dependency.Manifold { return dependency.Manifold{ Inputs: []string{ config.AgentName, config.APICallerName, config.MetricSpoolName, config.CharmDirName, }, Start: func(getResource dependency.GetResourceFunc) (worker.Worker, error) { collector, err := newCollect(config, getResource) if err != nil { return nil, err } return worker.NewPeriodicWorker(collector.Do, collector.period, worker.NewTimer), nil }, } }
// NewPeriodicWorker returns a periodic worker, that will call a stop function // when it is killed. func NewPeriodicWorker(do worker.PeriodicWorkerCall, period time.Duration, newTimer func(time.Duration) worker.PeriodicTimer, stop func()) worker.Worker { return &periodicWorker{ Worker: worker.NewPeriodicWorker(do, period, newTimer), stop: stop, } }
} type Facade interface { UpdateToolsVersion() error } // New returns a worker that periodically wakes up to try to find out and // record the latest version of the tools so the update possibility can be // displayed to the users on status. var New = func(api Facade, params *VersionCheckerParams) worker.Worker { w := &toolsVersionWorker{ api: api, params: params, } f := func(stop <-chan struct{}) error { return w.doCheck() } return worker.NewPeriodicWorker(f, params.CheckInterval, worker.NewTimer) } type toolsVersionWorker struct { api Facade params *VersionCheckerParams } func (w *toolsVersionWorker) doCheck() error { err := w.api.UpdateToolsVersion() return errors.Annotate(err, "cannot update tools information") }
// ListBlockDevicesFunc is the type of a function that is supplied to // NewWorker for listing block devices available on the local host. type ListBlockDevicesFunc func() ([]storage.BlockDevice, error) // DefaultListBlockDevices is the default function for listing block // devices for the operating system of the local host. var DefaultListBlockDevices ListBlockDevicesFunc // NewWorker returns a worker that lists block devices // attached to the machine, and records them in state. var NewWorker = func(l ListBlockDevicesFunc, b BlockDeviceSetter) worker.Worker { var old []storage.BlockDevice f := func(stop <-chan struct{}) error { return doWork(l, b, &old) } return worker.NewPeriodicWorker(f, listBlockDevicesPeriod, worker.NewTimer) } func doWork(listf ListBlockDevicesFunc, b BlockDeviceSetter, old *[]storage.BlockDevice) error { blockDevices, err := listf() if err != nil { return err } storage.SortBlockDevices(blockDevices) for _, blockDevice := range blockDevices { sort.Strings(blockDevice.DeviceLinks) } if reflect.DeepEqual(blockDevices, *old) { logger.Tracef("no changes to block devices detected") return nil }