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
import "context"
type Service interface {
Start() error
Restart() error
Start(ctx context.Context) error
Restart(ctx context.Context) error
Running() bool
}
+22 -13
View File
@@ -1,9 +1,11 @@
package corroservice
import (
"context"
"fmt"
"log/slog"
"os/exec"
"time"
)
const DefaultSystemdUnit = "uncloud-corrosion.service"
@@ -21,22 +23,29 @@ func DefaultSystemdService(dataDir string) *SystemdService {
}
}
func (s *SystemdService) Start() error {
if _, err := exec.Command("systemctl", "start", s.Unit).Output(); err != nil {
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.
s.running = true
return nil
func (s *SystemdService) Start(ctx context.Context) error {
return s.startOrRestart(ctx, "start")
}
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)
func (s *SystemdService) Restart(ctx context.Context) error {
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
}
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.
s.running = true
+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)
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)
}
}
+2 -2
View File
@@ -64,11 +64,11 @@ func (nc *networkController) Run(ctx context.Context) error {
if nc.corroService.Running() {
// 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)
}
} else {
if err := nc.corroService.Start(); err != nil {
if err := nc.corroService.Start(ctx); err != nil {
return fmt.Errorf("start corrosion service: %w", err)
}
}