Skip to content

Commit 99d87c6

Browse files
committed
fix: tests
1 parent 7297638 commit 99d87c6

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

backend/xray/jobs.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func (x *Xray) checkXrayStatus() error {
2727
for {
2828
select {
2929
case lastLog := <-logChan:
30-
if strings.Contains(lastLog, "Xray "+version) {
30+
// Check for the actual "started" message - this is more reliable
31+
// Xray outputs: [Warning] core: Xray {version} started
32+
if strings.Contains(lastLog, "core:") &&
33+
strings.Contains(lastLog, "Xray "+version) &&
34+
strings.Contains(lastLog, "started") {
3135
return nil
3236
}
3337

@@ -52,27 +56,36 @@ func (x *Xray) checkXrayStatus() error {
5256
}
5357

5458
func (x *Xray) checkXrayHealth(baseCtx context.Context) {
59+
consecutiveFailures := 0
60+
maxFailures := 3 // Allow a few failures before restarting
61+
5562
for {
5663
select {
5764
case <-baseCtx.Done():
5865
return
5966
default:
6067
ctx, cancel := context.WithTimeout(baseCtx, time.Second*3)
6168
_, err := x.GetSysStats(ctx)
62-
cancel() // Always call cancel to avoid context leak
69+
cancel()
6370

6471
if err != nil {
6572
if errors.Is(err, context.Canceled) {
66-
// Context was canceled due to x.ctx cancellation
67-
return // Exit gracefully
73+
return
6874
}
6975

70-
// Handle other errors by attempting restart
71-
if err = x.Restart(); err != nil {
72-
log.Println(err.Error())
73-
} else {
74-
log.Println("xray restarted")
76+
consecutiveFailures++
77+
// Only restart after multiple consecutive failures
78+
if consecutiveFailures >= maxFailures {
79+
log.Printf("xray health check failed %d times, restarting...", consecutiveFailures)
80+
if err = x.Restart(); err != nil {
81+
log.Println(err.Error())
82+
} else {
83+
log.Println("xray restarted")
84+
consecutiveFailures = 0 // Reset counter after restart
85+
}
7586
}
87+
} else {
88+
consecutiveFailures = 0 // Reset on success
7689
}
7790
}
7891
time.Sleep(time.Second * 5)

backend/xray/xray.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ func NewXray(ctx context.Context, port int, cfg *config.Config) (*Xray, error) {
8686
return nil, err
8787
}
8888
xray.handler = handler
89-
go xray.checkXrayHealth(xCtx)
89+
90+
// Wait a bit for Xray to fully initialize before starting health checks
91+
// This prevents false positives during startup
92+
go func() {
93+
time.Sleep(time.Second * 1) // Give Xray time to fully start
94+
xray.checkXrayHealth(xCtx)
95+
}()
9096

9197
log.Println("xray started, Version:", xray.Version())
9298

0 commit comments

Comments
 (0)