-
Notifications
You must be signed in to change notification settings - Fork 151
/
plugin.go
73 lines (62 loc) · 1.69 KB
/
plugin.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package versioncheck
import (
"context"
"time"
"go.uber.org/dig"
"github.com/iotaledger/hive.go/timeutil"
"github.com/iotaledger/hornet/pkg/app"
"github.com/iotaledger/hornet/pkg/node"
"github.com/iotaledger/hornet/pkg/shutdown"
"github.com/iotaledger/hornet/pkg/version"
)
func init() {
Plugin = &node.Plugin{
Status: node.StatusEnabled,
Pluggable: node.Pluggable{
Name: "VersionCheck",
DepsFunc: func(cDeps dependencies) { deps = cDeps },
Provide: provide,
Configure: configure,
Run: run,
},
}
}
var (
Plugin *node.Plugin
deps dependencies
)
type dependencies struct {
dig.In
AppInfo *app.AppInfo
VersionChecker *version.VersionChecker
}
func provide(c *dig.Container) {
if err := c.Provide(func(appInfo *app.AppInfo) *version.VersionChecker {
return version.NewVersionChecker("iotaledger", "hornet", appInfo.Version)
}); err != nil {
Plugin.LogPanic(err)
}
}
func configure() {
checkLatestVersion()
}
func run() {
// create a background worker that checks for latest version every hour
if err := Plugin.Node.Daemon().BackgroundWorker("Version update checker", func(ctx context.Context) {
ticker := timeutil.NewTicker(checkLatestVersion, 1*time.Hour, ctx)
ticker.WaitForGracefulShutdown()
}, shutdown.PriorityUpdateCheck); err != nil {
Plugin.LogPanicf("failed to start worker: %s", err)
}
}
func checkLatestVersion() {
res, err := deps.VersionChecker.CheckForUpdates()
if err != nil {
Plugin.LogWarnf("Update check failed: %s", err)
return
}
if res.Outdated {
Plugin.LogInfof("Update to %s available on https://github.com/iotaledger/hornet/releases/latest", res.Current)
deps.AppInfo.LatestGitHubVersion = res.Current
}
}