forked from juju/juju
/
worker.go
47 lines (37 loc) · 1.1 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package toolsversionchecker
import (
"time"
"github.com/juju/errors"
"gopkg.in/juju/worker.v1"
jworker "github.com/juju/juju/worker"
)
// VersionCheckerParams holds params for the version checker worker..
type VersionCheckerParams struct {
CheckInterval time.Duration
}
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 jworker.NewPeriodicWorker(f, params.CheckInterval, jworker.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")
}