add opptimistic sleep on corrosion service start to wait for schema initialisation

This commit is contained in:
Pavel Sviderski
2024-10-04 16:19:07 +10:00
parent fd3ad91136
commit e5bda92ec6
4 changed files with 29 additions and 18 deletions
+4 -2
View File
@@ -1,7 +1,9 @@
package corroservice package corroservice
import "context"
type Service interface { type Service interface {
Start() error Start(ctx context.Context) error
Restart() error Restart(ctx context.Context) error
Running() bool Running() bool
} }
+21 -12
View File
@@ -1,9 +1,11 @@
package corroservice package corroservice
import ( import (
"context"
"fmt" "fmt"
"log/slog" "log/slog"
"os/exec" "os/exec"
"time"
) )
const DefaultSystemdUnit = "uncloud-corrosion.service" const DefaultSystemdUnit = "uncloud-corrosion.service"
@@ -21,23 +23,30 @@ func DefaultSystemdService(dataDir string) *SystemdService {
} }
} }
func (s *SystemdService) Start() error { func (s *SystemdService) Start(ctx context.Context) error {
if _, err := exec.Command("systemctl", "start", s.Unit).Output(); err != nil { return s.startOrRestart(ctx, "start")
return fmt.Errorf("systemctl start %s: %w", s.Unit, err)
} }
slog.Info("Corrosion systemd service started.", "unit", s.Unit)
// TODO: run a goroutine to check the status of the service and log any errors in the uncloud log. func (s *SystemdService) Restart(ctx context.Context) error {
s.running = true return s.startOrRestart(ctx, "restart")
}
func (s *SystemdService) startOrRestart(ctx context.Context, cmd string) error {
if _, err := exec.Command("systemctl", cmd, s.Unit).Output(); err != nil {
return fmt.Errorf("systemctl %s %s: %w", cmd, s.Unit, err)
}
slog.Info(fmt.Sprintf("Corrosion systemd service %sed.", cmd), "unit", s.Unit)
// Optimistically wait for the corrosion service to start and initialise the database schema before proceeding.
timer := time.NewTimer(2 * time.Second)
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
return nil return nil
} }
func (s *SystemdService) Restart() error {
if _, err := exec.Command("systemctl", "restart", s.Unit).Output(); err != nil {
return fmt.Errorf("systemctl restart %s: %w", s.Unit, err)
}
slog.Info("Corrosion systemd service restarted.", "unit", s.Unit)
// TODO: run a goroutine to check the status of the service and log any errors in the uncloud log. // TODO: run a goroutine to check the status of the service and log any errors in the uncloud log.
s.running = true s.running = true
return nil return nil
+1 -1
View File
@@ -170,7 +170,7 @@ func (m *Machine) Run(ctx context.Context) error {
} }
slog.Info("Configured corrosion service.", "dir", m.config.CorrosionDir) slog.Info("Configured corrosion service.", "dir", m.config.CorrosionDir)
if err := m.config.CorrosionService.Start(); err != nil { if err := m.config.CorrosionService.Start(ctx); err != nil {
return fmt.Errorf("start corrosion service: %w", err) return fmt.Errorf("start corrosion service: %w", err)
} }
} }
+2 -2
View File
@@ -64,11 +64,11 @@ func (nc *networkController) Run(ctx context.Context) error {
if nc.corroService.Running() { if nc.corroService.Running() {
// Corrosion service was running before the WireGuard network was configured so we need to restart it. // Corrosion service was running before the WireGuard network was configured so we need to restart it.
if err := nc.corroService.Restart(); err != nil { if err := nc.corroService.Restart(ctx); err != nil {
return fmt.Errorf("restart corrosion service: %w", err) return fmt.Errorf("restart corrosion service: %w", err)
} }
} else { } else {
if err := nc.corroService.Start(); err != nil { if err := nc.corroService.Start(ctx); err != nil {
return fmt.Errorf("start corrosion service: %w", err) return fmt.Errorf("start corrosion service: %w", err)
} }
} }