Skip to content

Commit 3901e20

Browse files
committed
test watchloop
1 parent 55ad3c4 commit 3901e20

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

internal/pkg/agent/cmd/watch.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"os"
11-
"os/signal"
1211
"runtime"
13-
"syscall"
1412
"time"
1513

1614
"github.com/spf13/cobra"
@@ -211,52 +209,6 @@ func isWindows() bool {
211209
return runtime.GOOS == "windows"
212210
}
213211

214-
func watch(ctx context.Context, tilGrace time.Duration, errorCheckInterval time.Duration, log *logger.Logger) error {
215-
errChan := make(chan error)
216-
217-
ctx, cancel := context.WithCancel(ctx)
218-
219-
//cleanup
220-
defer func() {
221-
cancel()
222-
close(errChan)
223-
}()
224-
225-
agentWatcher := upgrade.NewAgentWatcher(errChan, log, errorCheckInterval)
226-
go agentWatcher.Run(ctx)
227-
228-
signals := make(chan os.Signal, 1)
229-
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
230-
231-
t := time.NewTimer(tilGrace)
232-
defer t.Stop()
233-
234-
WATCHLOOP:
235-
for {
236-
select {
237-
case s := <-signals:
238-
log.Infof("received signal: (%d): %v during watch", s, s)
239-
if s == syscall.SIGINT || s == syscall.SIGTERM {
240-
log.Infof("received signal: (%d): %v. Exiting watch", s, s)
241-
return ErrWatchCancelled
242-
}
243-
continue
244-
case <-ctx.Done():
245-
break WATCHLOOP
246-
// grace period passed, agent is considered stable
247-
case <-t.C:
248-
log.Info("Grace period passed, not watching")
249-
break WATCHLOOP
250-
// Agent in degraded state.
251-
case err := <-errChan:
252-
log.Errorf("Agent Error detected: %s", err.Error())
253-
return err
254-
}
255-
}
256-
257-
return nil
258-
}
259-
260212
// gracePeriod returns true if it is within grace period and time until grace period ends.
261213
// otherwise it returns false and 0
262214
func gracePeriod(marker *upgrade.UpdateMarker, gracePeriodDuration time.Duration) (bool, time.Duration) {

internal/pkg/agent/cmd/watch_impl.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package cmd
66

77
import (
88
"context"
9+
"os"
10+
"os/signal"
11+
"syscall"
912
"time"
1013

1114
"github.com/elastic/elastic-agent-libs/logp"
@@ -29,3 +32,50 @@ func (a upgradeInstallationModifier) Cleanup(log *logger.Logger, topDirPath, cur
2932
func (a upgradeInstallationModifier) Rollback(ctx context.Context, log *logger.Logger, c client.Client, topDirPath, prevVersionedHome, prevHash string) error {
3033
return upgrade.Rollback(ctx, log, c, topDirPath, prevVersionedHome, prevHash)
3134
}
35+
36+
func watch(ctx context.Context, tilGrace time.Duration, errorCheckInterval time.Duration, log *logger.Logger) error {
37+
errChan := make(chan error)
38+
39+
ctx, cancel := context.WithCancel(ctx)
40+
41+
//cleanup
42+
defer func() {
43+
cancel()
44+
close(errChan)
45+
}()
46+
47+
agtWatcher := upgrade.NewAgentWatcher(errChan, log, errorCheckInterval)
48+
go agtWatcher.Run(ctx)
49+
50+
signals := make(chan os.Signal, 1)
51+
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
52+
53+
graceTimer := time.NewTimer(tilGrace)
54+
defer graceTimer.Stop()
55+
56+
return watchLoop(ctx, log, signals, errChan, graceTimer.C)
57+
}
58+
59+
func watchLoop(ctx context.Context, log *logger.Logger, signals <-chan os.Signal, errChan <-chan error, graceTimer <-chan time.Time) error {
60+
for {
61+
select {
62+
case s := <-signals:
63+
log.Infof("received signal: (%d): %v during watch", s, s)
64+
if s == syscall.SIGINT || s == syscall.SIGTERM {
65+
log.Infof("received signal: (%d): %v. Exiting watch", s, s)
66+
return ErrWatchCancelled
67+
}
68+
continue
69+
case <-ctx.Done():
70+
return nil
71+
// grace period passed, agent is considered stable
72+
case <-graceTimer:
73+
log.Info("Grace period passed, not watching")
74+
return nil
75+
// Agent in degraded state.
76+
case err := <-errChan:
77+
log.Errorf("Agent Error detected: %s", err.Error())
78+
return err
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)