Example #1
0
// 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
		},
	}
}
Example #2
0
// 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)
}
Example #3
0
// 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)
}
Example #4
0
// 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)
}
Example #5
0
// 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)
}
Example #6
0
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)
}
Example #7
0
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)
}
Example #8
0
// 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
}
Example #9
0
// 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)
}
Example #10
0
File: cleanup.go Project: bac/juju
// 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)
}
Example #11
0
// 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)
}
Example #12
0
// 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
		},
	}
}
Example #13
0
// 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,
	}
}
Example #14
0
}

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")
}
Example #15
0
// 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
	}