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
+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