mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
add opptimistic sleep on corrosion service start to wait for schema initialisation
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user