Skip to content

Commit f868678

Browse files
committed
refactor(services): discard the 'force' flag to operation that stop services
1 parent d2b2091 commit f868678

12 files changed

Lines changed: 43 additions & 52 deletions

File tree

internal/advancer/advancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (s *Service) processInputs(ctx context.Context, app *Application, inputs []
311311
"epoch", input.EpochIndex,
312312
"index", input.Index,
313313
"error", err)
314-
s.Supervisor.Stop(true) // triggers graceful shutdown of all services
314+
s.Supervisor.Stop() // shutdown all services
315315
return err
316316
}
317317

internal/advancer/advancer_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ func (s *AdvancerSuite) TestServiceInterface() {
118118
require.Contains(tickErr.Error(), "list epochs error")
119119

120120
// Stop must be called last to cleanly shut down the service.
121-
// It should complete without returning any errors.
122-
require.NoError(advancer.Supervisor.Stop(false))
121+
advancer.Supervisor.Stop()
123122
})
124123
}
125124

internal/advancer/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ func (s *Service) Tick(ctx context.Context) (bool, error) {
138138
return hadWork, err
139139
}
140140

141-
func (s *Service) Stop(b bool) error {
141+
func (s *Service) Stop() error {
142142
var errs []error
143143
if s.machineManager != nil {
144144
s.Logger.Info("Closing machine manager")
145145
if err := s.machineManager.Close(); err != nil {
146146
errs = append(errs, fmt.Errorf("failed to close machine manager: %w", err))
147147
}
148148
}
149-
if err := s.TickServiceTemplate.Stop(b); err != nil {
149+
if err := s.TickServiceTemplate.Stop(); err != nil {
150150
errs = append(errs, err)
151151
}
152152
return errors.Join(errs...)

internal/evmreader/service_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestCreateAcceptsRequestTimeoutBelowPollingInterval(t *testing.T) {
9797
Repository: repo,
9898
})
9999
require.NoError(t, err)
100-
defer svc.Stop(false)
100+
defer svc.Stop()
101101

102102
repo.AssertExpectations(t)
103103
}

internal/node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func createServices(
102102

103103
if len(errs) > 0 {
104104
for _, svc := range services {
105-
stopErr := svc.Stop(true)
105+
stopErr := svc.Stop()
106106
if stopErr != nil {
107107
errs = append(errs, stopErr)
108108
}

pkg/service/http_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func InitHTTPServiceTemplate(
9191
return nil
9292
}
9393

94-
func (s *HTTPServiceTemplate) Stop(bool) error {
94+
func (s *HTTPServiceTemplate) Stop() error {
9595
s.Logger.Info("Shutting down HTTP service", "addr", s.Server.Addr)
9696
ctx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
9797
defer cancel()

pkg/service/service.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ var (
6666
ErrServiceStopped = fmt.Errorf("service was stopped")
6767
)
6868

69-
// Public interface with methods to manipulate the service.
70-
type IService interface {
69+
// Basic methods to manipulate a service.
70+
type BaseService interface {
7171
Alive() bool
7272
Ready() bool
7373
Reload() error
74-
Stop(bool) error
7574
Serve() error
7675
String() string
7776
}
7877

7978
// Service interface with a service supervisor.
8079
type SupervisedService interface {
81-
IService
80+
BaseService
81+
Stop() error
8282
SetSupervisor(*Supervisor)
8383
}
8484

@@ -116,10 +116,10 @@ func InitServiceTemplate(c *BaseConfigs, s *BaseTemplate) error {
116116

117117
// Default implementation of some abstract methods (except `Serve`).
118118
// Remove them to force concrete services to provide implementation for them.
119-
func (s *BaseTemplate) Reload() error { return nil }
120-
func (s *BaseTemplate) Stop(bool) error { return nil }
121-
func (s *BaseTemplate) Alive() bool { return true }
122-
func (s *BaseTemplate) Ready() bool { return true }
119+
func (s *BaseTemplate) Reload() error { return nil }
120+
func (s *BaseTemplate) Stop() error { return nil }
121+
func (s *BaseTemplate) Alive() bool { return true }
122+
func (s *BaseTemplate) Ready() bool { return true }
123123
func (s *BaseTemplate) String() string { return s.Name }
124124

125125
func (s *BaseTemplate) SetSupervisor(supervisor *Supervisor) {

pkg/service/service_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type delayedCloseImpl struct {
1515
onStopInitChan chan struct{}
1616
}
1717

18-
func (s *delayedCloseImpl) Stop(bool) error {
18+
func (s *delayedCloseImpl) Stop() error {
1919
<-s.onStopInitChan // wait signal to initiate stop
2020
return nil
2121
}
@@ -51,13 +51,12 @@ func (s *ServeSuite) TestServeExitsAfterStopIsComplete() {
5151
close(onServeEndChan)
5252
}()
5353

54-
onStopEndChan := make(chan error)
54+
onStopEndChan := make(chan struct{})
5555
select {
5656
case <-svc.onServeInitChan: // wait service to initiate, so can be stopped.
5757
// initiate service shutdown through context cancelation
5858
go func() {
59-
err := supervisor.Stop(true)
60-
onStopEndChan <- err // signal stop ended and provide the errors
59+
supervisor.Stop()
6160
close(onStopEndChan)
6261
}()
6362
case <-time.After(2 * time.Second):
@@ -86,8 +85,7 @@ func (s *ServeSuite) TestServeExitsAfterStopIsComplete() {
8685

8786
// Stop() should exit without errors.
8887
select {
89-
case err := <-onStopEndChan:
90-
s.NoError(err)
88+
case <-onStopEndChan:
9189
case <-time.After(2 * time.Second):
9290
s.Fail("Stop() did not exit within 2 seconds after 'OnStop' concluded")
9391
}

pkg/service/supervisor.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import (
1414
"time"
1515
)
1616

17+
type IService interface {
18+
BaseService
19+
Stop()
20+
}
21+
1722
type SupervisorConfigs struct {
1823
BaseConfigs
1924
Services []SupervisedService
@@ -164,13 +169,12 @@ func (s *Supervisor) Serve() error {
164169
if err != nil {
165170
s.Logger.Error("Service failed to restart", "error", err)
166171
}
172+
continue
167173
case <-s.sigShutdown:
168-
s.Stop(false) // Graceful shutdown; errors are logged by Stop.
169-
return
170174
case <-s.context.Done():
171-
s.Stop(true) // Stop logs errors internally.
172-
return
173175
}
176+
s.Stop()
177+
return
174178
}
175179
}()
176180

@@ -188,7 +192,7 @@ func (s *Supervisor) Serve() error {
188192
"service", svc.String(),
189193
"error", err,
190194
)
191-
s.Stop(false)
195+
s.Stop()
192196
case s.stopped.Load():
193197
s.Logger.Info("Service stopped",
194198
"service", svc.String(),
@@ -209,19 +213,19 @@ func (s *Supervisor) Serve() error {
209213
}
210214
}
211215

212-
go s.Stop(true)
216+
go s.Stop()
213217
<-s.stoppedChan
214218

215219
return errors.Join(allErrs...)
216220
}
217221

218-
func (s *Supervisor) Stop(force bool) error {
222+
func (s *Supervisor) Stop() {
219223
// CAS achieves once-semantics: the second caller returns immediately
220224
// (fire-and-forget) rather than blocking like sync.Once. This is safe
221225
// because the orchestrator calls Cancel() after Stop() and waits for
222226
// the Serve goroutine to exit.
223227
if !s.stopped.CompareAndSwap(false, true) {
224-
return nil // already stopped
228+
return // already stopped
225229
}
226230

227231
if s.sigShutdown != nil {
@@ -231,29 +235,19 @@ func (s *Supervisor) Stop(force bool) error {
231235
signal.Stop(s.sigHangUp)
232236
}
233237

234-
var err error
235238
for i := len(s.services)-1; i >= 0; i-- {
236239
svc := s.services[i]
237240
start := time.Now()
238-
svcErr := svc.Stop(force)
241+
err := svc.Stop()
239242
elapsed := time.Since(start)
240243

241-
if svcErr != nil {
242-
s.Logger.Error("Stop",
243-
"force", force,
244-
"duration", elapsed,
245-
"error", svcErr)
244+
if err != nil {
245+
s.Logger.Error("Stop", "duration", elapsed, "error", err)
246246
} else {
247-
s.Logger.Info("Stop",
248-
"force", force,
249-
"duration", elapsed)
247+
s.Logger.Info("Stop", "duration", elapsed)
250248
}
251-
252-
err = errors.Join(err, svcErr)
253249
}
254250

255251
s.cancelContext()
256252
close(s.stoppedChan)
257-
258-
return err
259253
}

pkg/service/supervisor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (s *SupervisorSuite) TestNodeStopCancelsChildContexts() {
8383
s.Fail("child-2 did not start")
8484
}
8585

86-
supervisor.Stop(false)
86+
supervisor.Stop()
8787

8888
select {
8989
case <-child1.done:
@@ -144,7 +144,7 @@ func (c *stopAwareChildImpl) Serve() error {
144144
return nil
145145
}
146146

147-
func (c *stopAwareChildImpl) Stop(bool) error {
147+
func (c *stopAwareChildImpl) Stop() error {
148148
c.stopOnce.Do(func() { close(c.stopped) })
149149
return nil
150150
}

0 commit comments

Comments
 (0)