Skip to content

Commit 34384b8

Browse files
Minor fixes
1 parent effb852 commit 34384b8

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

lifetime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type TerminationSignal func() syscall.Signal
1111
func InterruptSignal() syscall.Signal {
1212
signals := make(chan os.Signal, 1)
1313
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
14-
<-signals
14+
sig := <-signals
1515

16-
return syscall.Signal(0)
16+
return sig.(syscall.Signal)
1717
}

manager.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ func (m *Manager) setupComponents() error {
8383
for _, s := range m.components {
8484
setupable, ok := s.Component.(setupable)
8585
if ok {
86-
m.logger.Info(fmt.Sprintf("[UnixCycle] Setting up component %q", s.name), slog.String("component_name", s.name))
86+
m.logInfo(fmt.Sprintf("Setting up component %q", s.name), slog.String("component_name", s.name))
8787
err := funcOrTimeout(setupable.Setup, m.setupTimeout)
8888
if errors.Is(err, errTimeout) {
89-
m.logger.Error(fmt.Sprintf("[UnixCycle] Setup timed out for component %q", s.name), slog.String("component_name", s.name))
89+
m.logError(fmt.Sprintf("Setup timed out for component %q", s.name), slog.String("component_name", s.name))
9090
return err
9191
}
9292
if err != nil {
93-
m.logger.Error(fmt.Sprintf("[UnixCycle] Failure during setup for component %q: %v", s.name, err), slog.String("component_name", s.name))
93+
m.logError(fmt.Sprintf("Failure during setup for component %q: %v", s.name, err), slog.String("component_name", s.name))
9494
return err
9595
}
9696
}
@@ -102,17 +102,17 @@ func (m *Manager) startComponents() {
102102
for _, s := range m.components {
103103
startable, ok := s.Component.(startable)
104104
if ok {
105-
m.logger.Info(fmt.Sprintf("[UnixCycle] Starting component %q", s.name), slog.String("component_name", s.name))
105+
m.logInfo(fmt.Sprintf("Starting component %q", s.name), slog.String("component_name", s.name))
106106
go func() {
107107
defer func() {
108108
if r := recover(); r != nil {
109-
m.logger.Error(fmt.Sprintf("[UnixCycle] Panic during start for component %q: %v", s.name, r), slog.String("component_name", s.name))
109+
m.logError(fmt.Sprintf("Panic during start for component %q: %v", s.name, r), slog.String("component_name", s.name))
110110
m.exitSignal <- syscall.SIGABRT
111111
}
112112
}()
113113
err := startable.Start() // Blocking for go routine
114114
if err != nil {
115-
m.logger.Error(fmt.Sprintf("[UnixCycle] Failure during start for component %q: %v", s.name, err), slog.String("component_name", s.name))
115+
m.logError(fmt.Sprintf("Failure during start for component %q: %v", s.name, err), slog.String("component_name", s.name))
116116
m.exitSignal <- syscall.SIGABRT
117117
}
118118
}()
@@ -122,26 +122,30 @@ func (m *Manager) startComponents() {
122122

123123
func (m *Manager) waitForSignal() syscall.Signal {
124124
go func() {
125-
m.exitSignal <- m.lifetime()
125+
select {
126+
case m.exitSignal <- m.lifetime():
127+
default:
128+
// Signal already sent, don't block
129+
}
126130
}()
127131

128132
signal := <-m.exitSignal
129-
m.logger.Info(fmt.Sprintf("[UnixCycle] Received signal: %v", signal), slog.String("signal", signal.String()))
133+
m.logInfo(fmt.Sprintf("Received signal: %v", signal), slog.String("signal", signal.String()))
130134
return signal
131135
}
132136

133137
func (m *Manager) closeComponents() error {
134138
for _, s := range slices.Backward(m.components) {
135139
closable, ok := s.Component.(closable)
136140
if ok {
137-
m.logger.Info(fmt.Sprintf("[UnixCycle] Closing component %q", s.name), slog.String("component_name", s.name))
141+
m.logInfo(fmt.Sprintf("Closing component %q", s.name), slog.String("component_name", s.name))
138142
err := funcOrTimeout(closable.Close, m.closeTimeout)
139143
if errors.Is(err, errTimeout) {
140-
m.logger.Error(fmt.Sprintf("[UnixCycle] Close timed out for component %q", s.name), slog.String("component_name", s.name))
144+
m.logError(fmt.Sprintf("Close timed out for component %q", s.name), slog.String("component_name", s.name))
141145
return err
142146
}
143147
if err != nil {
144-
m.logger.Error(fmt.Sprintf("[UnixCycle] Failure during close for component %q: %v", s.name, err), slog.String("component_name", s.name))
148+
m.logError(fmt.Sprintf("Failure during close for component %q: %v", s.name, err), slog.String("component_name", s.name))
145149
return err
146150
}
147151
}
@@ -150,6 +154,15 @@ func (m *Manager) closeComponents() error {
150154
return nil
151155
}
152156

157+
func (m *Manager) logInfo(msg string, attrs ...any) {
158+
m.logger.Info("[UnixCycle] "+msg, attrs...)
159+
}
160+
161+
func (m *Manager) logError(msg string, attrs ...any) {
162+
m.logger.Error("[UnixCycle] "+msg, attrs...)
163+
}
164+
165+
// NOTE: goroutine may leak on timeout, but acceptable since timeout usually always leaves to a library shutdown
153166
func funcOrTimeout(f func() error, timeout time.Duration) error {
154167
errs := make(chan error, 1)
155168
go func() {

testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestMain(m TestingM, manager *Manager, prober ProberFunc, testFixtures ...C
3232
managerStopped = make(chan syscall.Signal)
3333
proberLifetime = func() syscall.Signal {
3434
if err := prober(context.Background()); err != nil {
35-
manager.logger.Error("[UnixCycle] unable to run tests due to prober failing with error", "error", err)
35+
manager.logError("unable to run tests due to prober failing with error", "error", err)
3636
return syscall.SIGUSR1
3737
}
3838
return syscall.Signal(m.Run())

0 commit comments

Comments
 (0)