-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchdog.go
More file actions
158 lines (130 loc) · 2.89 KB
/
Copy pathwatchdog.go
File metadata and controls
158 lines (130 loc) · 2.89 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// MIT License
// Copyright (c) 2025 Pooyan Khanjankhani
package main
import (
"context"
"errors"
"fmt"
"sync/atomic"
"github.com/thekhanj/ella/config"
)
type WatchdogSignal int
const (
WatchdogSigStarted WatchdogSignal = iota
WatchdogSigStopped
WatchdogSigFailed
)
var WatchdogErrAlreadyRunning = errors.New("an active process is already running")
type Watchdog interface {
Start() (chan WatchdogSignal, error)
Stop() error
Reload() error
Procs() *Procs
}
func NewWatchdogFromConfig(
cfg config.Watchdog,
exec func() (*Proc, error),
stop, reload ProcAction,
) (Watchdog, error) {
// TODO: make this watchdog config simpler, no need for this complexity
if _, ok := cfg.(*config.SimpleWatchdog); ok {
return NewSimpleWatchdog(exec, stop, reload), nil
} else {
return nil, fmt.Errorf("invalid watchdog config: %v", cfg)
}
}
type SimpleWatchdog struct {
procs *Procs
exec func() (*Proc, error)
stop ProcAction
reload ProcAction
running atomic.Bool
cancel func()
}
func (this *SimpleWatchdog) Start() (chan WatchdogSignal, error) {
if this.running.Load() {
return nil, WatchdogErrAlreadyRunning
}
proc, err := this.exec()
if err != nil {
return nil, err
}
this.running.Store(true)
go this.procs.Push(proc)
signals := make(chan WatchdogSignal)
ctx, cancel := context.WithCancel(context.Background())
this.cancel = cancel
go this.run(ctx, proc, signals)
return signals, nil
}
func (this *SimpleWatchdog) Stop() error {
proc, err := this.procs.Last()
if err != nil {
return err
}
this.cancel()
this.running.Store(false)
return this.stop.Exec(proc)
}
func (this *SimpleWatchdog) Reload() error {
proc, err := this.procs.Last()
if err != nil {
return err
}
return this.reload.Exec(proc)
}
func (this *SimpleWatchdog) Procs() *Procs {
return this.procs
}
func (this *SimpleWatchdog) run(
ctx context.Context,
proc *Proc, signals chan WatchdogSignal,
) {
states := proc.Sub()
defer func() {
proc.Unsub(states)
close(signals)
this.running.Store(false)
}()
go func() {
err := proc.Run(ctx)
if err != nil {
fmt.Println("watchdog: process:", err)
}
}()
for state := range states {
if state == ProcStateStarted {
// TODO: think about coroutine or not
this.signal(signals, WatchdogSigStarted)
}
if state == ProcStateStopped {
code, err := proc.GetExitCode()
if err != nil {
panic("unreachable code")
}
if code == 0 || this.running.Load() == false {
this.signal(signals, WatchdogSigStopped)
} else {
this.signal(signals, WatchdogSigFailed)
}
}
}
}
func (this *SimpleWatchdog) signal(
sigs chan WatchdogSignal, sig WatchdogSignal,
) {
sigs <- sig
}
var _ Watchdog = (*SimpleWatchdog)(nil)
func NewSimpleWatchdog(
exec func() (*Proc, error),
stop, reload ProcAction,
) *SimpleWatchdog {
return &SimpleWatchdog{
procs: NewProcs(),
exec: exec,
stop: stop,
reload: reload,
running: atomic.Bool{},
}
}