Skip to content

Commit cc819bb

Browse files
aledbfroboquat
authored andcommitted
[ws-daemon] Refactor watch of configuration file
1 parent 577892f commit cc819bb

File tree

2 files changed

+22
-64
lines changed

2 files changed

+22
-64
lines changed

components/ws-daemon/cmd/run.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
3030
"github.com/gitpod-io/gitpod/common-go/log"
3131
"github.com/gitpod-io/gitpod/common-go/pprof"
32+
"github.com/gitpod-io/gitpod/common-go/watch"
3233
"github.com/gitpod-io/gitpod/ws-daemon/pkg/config"
3334
"github.com/gitpod-io/gitpod/ws-daemon/pkg/daemon"
3435
)
@@ -128,7 +129,27 @@ var runCmd = &cobra.Command{
128129
log.WithError(err).Fatal("cannot start daemon")
129130
}
130131

131-
go config.Watch(configFile, dmn.ReloadConfig)
132+
ctx, cancel := context.WithCancel(context.Background())
133+
defer cancel()
134+
135+
err = watch.File(ctx, configFile, func() {
136+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
137+
defer cancel()
138+
139+
cfg, err := config.Read(configFile)
140+
if err != nil {
141+
log.WithError(err).Warn("cannot reload configuration")
142+
return
143+
}
144+
145+
err = dmn.ReloadConfig(ctx, &cfg.Daemon)
146+
if err != nil {
147+
log.WithError(err).Warn("cannot reload configuration")
148+
}
149+
})
150+
if err != nil {
151+
log.WithError(err).Fatal("cannot start watch of configuration file")
152+
}
132153

133154
// run until we're told to stop
134155
sigChan := make(chan os.Signal, 1)

components/ws-daemon/pkg/config/config.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@ package config
66

77
import (
88
"bytes"
9-
"context"
10-
"crypto/sha256"
119
"crypto/tls"
12-
"encoding/hex"
1310
"encoding/json"
14-
"io"
1511
"os"
16-
"time"
1712

1813
"golang.org/x/xerrors"
1914
"google.golang.org/grpc"
2015
"google.golang.org/grpc/credentials"
2116

2217
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
23-
"github.com/gitpod-io/gitpod/common-go/log"
2418
"github.com/gitpod-io/gitpod/ws-daemon/pkg/daemon"
2519
)
2620

@@ -41,63 +35,6 @@ func Read(fn string) (*Config, error) {
4135
return &cfg, nil
4236
}
4337

44-
// watchConfig watches the configuration file and if changed reloads the static layer
45-
func Watch(fn string, cb func(context.Context, *daemon.Config) error) {
46-
hashConfig := func() (hash string, err error) {
47-
f, err := os.Open(fn)
48-
if err != nil {
49-
return "", err
50-
}
51-
defer f.Close()
52-
53-
h := sha256.New()
54-
_, err = io.Copy(h, f)
55-
if err != nil {
56-
return "", err
57-
}
58-
59-
return hex.EncodeToString(h.Sum(nil)), nil
60-
}
61-
reloadConfig := func() error {
62-
cfg, err := Read(fn)
63-
if err != nil {
64-
return err
65-
}
66-
67-
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
68-
defer cancel()
69-
70-
return cb(ctx, &cfg.Daemon)
71-
}
72-
73-
var (
74-
tick = time.NewTicker(30 * time.Second)
75-
oldHash string
76-
)
77-
defer tick.Stop()
78-
for range tick.C {
79-
currentHash, err := hashConfig()
80-
if err != nil {
81-
log.WithError(err).Warn("cannot check if config has changed")
82-
}
83-
84-
if oldHash == "" {
85-
oldHash = currentHash
86-
}
87-
if currentHash == oldHash {
88-
continue
89-
}
90-
oldHash = currentHash
91-
92-
err = reloadConfig()
93-
if err == nil {
94-
log.Info("configuration was updated - reloaded static layer config")
95-
} else {
96-
log.WithError(err).Error("cannot reload config - config hot reloading did not work")
97-
}
98-
}
99-
}
100-
10138
type Config struct {
10239
Daemon daemon.Config `json:"daemon"`
10340
Service AddrTLS `json:"service"`

0 commit comments

Comments
 (0)