Skip to content

Commit 96bdfae

Browse files
committed
TO SQUASH OR REMOVE
1 parent 97ec3cc commit 96bdfae

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

cli/running/running.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -518,21 +518,17 @@ func Start(cmdCtx *cmdcontext.CmdCtx, run *InstanceCtx) error {
518518
provider := providerImpl{cmdCtx: cmdCtx, instanceCtx: run}
519519
wd := NewWatchdog(run.Restartable, 5*time.Second, logger, &provider)
520520

521-
// The signal handling loop must be started before the instance
522-
// get started for avoiding a race condition between tt start
523-
// and tt stop. This way we avoid a situation when we receive
524-
// a signal before starting a handler for it.
525-
wd.StartSignalHandling()
526-
527-
if err := process_utils.CreatePIDFile(run.PIDFile); err != nil {
528-
return err
529-
}
530-
531521
defer func() {
532522
cleanup(run)
533523
}()
534524

535-
wd.Start()
525+
preStartAction := func() error {
526+
if err := process_utils.CreatePIDFile(run.PIDFile); err != nil {
527+
return err
528+
}
529+
return nil
530+
}
531+
wd.Start(preStartAction)
536532
return nil
537533
}
538534

cli/running/watchdog.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,36 @@ func NewWatchdog(restartable bool, restartTimeout time.Duration, logger *ttlog.L
5757
provider: provider}
5858

5959
wd.done = make(chan bool, 1)
60-
wd.shouldStop = false
6160

6261
return &wd
6362
}
6463

6564
// Start starts the Instance and signal handling.
66-
func (wd *Watchdog) Start() {
65+
func (wd *Watchdog) Start(preStartAction func() error) error {
66+
var err error
67+
// Create Instance.
68+
if wd.Instance, err = wd.provider.CreateInstance(wd.logger); err != nil {
69+
wd.logger.Printf(`Watchdog(ERROR): "%v".`, err)
70+
return err
71+
}
72+
wd.logger = wd.Instance.logger
73+
// The signal handling loop must be started before the instance
74+
// get started for avoiding a race condition between tt start
75+
// and tt stop. This way we avoid a situation when we receive
76+
// a signal before starting a handler for it.
77+
wd.startSignalHandling()
78+
79+
if err = preStartAction(); err != nil {
80+
wd.logger.Printf(`Pre-start action error: %v`, err)
81+
// Finish the signal handling goroutine.
82+
wd.done <- true
83+
return err
84+
}
85+
6786
// The Instance must be restarted on completion if the "restartable"
6887
// parameter is set to "true".
6988
for {
7089
var err error
71-
// Create Instance.
72-
if wd.Instance, err = wd.provider.CreateInstance(wd.logger); err != nil {
73-
wd.logger.Printf(`Watchdog(ERROR): "%v".`, err)
74-
break
75-
}
76-
wd.logger = wd.Instance.logger
7790

7891
wd.instanceStateMutex.Lock()
7992
if !wd.shouldStop {
@@ -87,7 +100,7 @@ func (wd *Watchdog) Start() {
87100
} else {
88101
wd.logger.Printf(`Watchdog(ERROR): terminated before instance start.`)
89102
wd.instanceStateMutex.Unlock()
90-
return
103+
return nil
91104
}
92105
wd.instanceStateMutex.Unlock()
93106

@@ -125,13 +138,21 @@ func (wd *Watchdog) Start() {
125138
time.Sleep(wd.restartTimeout)
126139

127140
wd.shouldStop = false
141+
142+
// Create Instance.
143+
if wd.Instance, err = wd.provider.CreateInstance(wd.logger); err != nil {
144+
wd.logger.Printf(`Watchdog(ERROR): "%v".`, err)
145+
return err
146+
}
147+
wd.logger = wd.Instance.logger
128148
// Before the restart of an instance start a new signal handling loop.
129-
wd.StartSignalHandling()
149+
wd.startSignalHandling()
130150
}
151+
return nil
131152
}
132153

133-
// StartSignalHandling starts signal handling in a separate goroutine.
134-
func (wd *Watchdog) StartSignalHandling() {
154+
// startSignalHandling starts signal handling in a separate goroutine.
155+
func (wd *Watchdog) startSignalHandling() {
135156
sigChan := make(chan os.Signal, 1)
136157
// Reset the signal mask before starting of the new loop.
137158
signal.Reset()

cli/running/watchdog_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ func TestWatchdogBase(t *testing.T) {
112112
t.Cleanup(func() { cleanupWatchdog(wd) })
113113

114114
wdDoneChan := make(chan bool, 1)
115+
testPreAction := func() error { return nil }
115116
go func() {
116-
wd.Start()
117+
wd.Start(testPreAction)
117118
wdDoneChan <- true
118119
}()
119120
waitProcessStart()
@@ -140,8 +141,9 @@ func TestWatchdogNotRestartable(t *testing.T) {
140141
t.Cleanup(func() { cleanupWatchdog(wd) })
141142

142143
wdDoneChan := make(chan bool, 1)
144+
testPreAction := func() error { return nil }
143145
go func() {
144-
wd.Start()
146+
wd.Start(testPreAction)
145147
wdDoneChan <- true
146148
}()
147149
waitProcessStart()

0 commit comments

Comments
 (0)