fix: race on cluster init by waiting for corrosion service to become ready with schema applied

This commit is contained in:
Pasha Sviderski
2025-07-25 16:28:32 +10:00
parent 3faaac4da7
commit 4166474ee8
5 changed files with 83 additions and 30 deletions
+6 -12
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"log/slog"
"os/exec"
"time"
)
const DefaultSystemdUnit = "uncloud-corrosion.service"
@@ -44,20 +43,15 @@ 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)
slog.Debug(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.Debug("Waiting for corrosion service to be ready.")
if err := WaitReady(ctx, s.DataDir); err != nil {
return err
}
// TODO: run a goroutine to check the status of the service and log any errors in the uncloud log.
slog.Debug("Corrosion service is ready.")
s.running = true
return nil
}