mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
cmd/ctrld: wait ctrld started during restart command
This commit is contained in:
+47
-27
@@ -559,6 +559,15 @@ func initCLI() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if doTasks(tasks) {
|
if doTasks(tasks) {
|
||||||
|
dir, err := userHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
mainLog.Load().Warn().Err(err).Msg("Service was restarted, but could not ping the control server")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if cc := newSocketControlClient(s, dir); cc == nil {
|
||||||
|
mainLog.Load().Notice().Msg("Service was not restarted")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
mainLog.Load().Notice().Msg("Service restarted")
|
mainLog.Load().Notice().Msg("Service restarted")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -1147,34 +1156,12 @@ func selfCheckStatus(s service.Service) service.Status {
|
|||||||
mainLog.Load().Error().Err(err).Msg("failed to check ctrld listener status: could not get home directory")
|
mainLog.Load().Error().Err(err).Msg("failed to check ctrld listener status: could not get home directory")
|
||||||
return service.StatusUnknown
|
return service.StatusUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
bo := backoff.NewBackoff("self-check", logf, 10*time.Second)
|
|
||||||
bo.LogLongerThan = 10 * time.Second
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
mainLog.Load().Debug().Msg("waiting for ctrld listener to be ready")
|
mainLog.Load().Debug().Msg("waiting for ctrld listener to be ready")
|
||||||
cc := newControlClient(filepath.Join(dir, ctrldControlUnixSock))
|
cc := newSocketControlClient(s, dir)
|
||||||
|
if cc == nil {
|
||||||
// The socket control server may not start yet, so attempt to ping
|
return service.StatusUnknown
|
||||||
// it until we got a response. For each iteration, check ctrld status
|
|
||||||
// to make sure ctrld is still running.
|
|
||||||
for {
|
|
||||||
curStatus, err := s.Status()
|
|
||||||
if err != nil {
|
|
||||||
mainLog.Load().Warn().Err(err).Msg("could not get service status while doing self-check")
|
|
||||||
return status
|
|
||||||
}
|
|
||||||
if curStatus != service.StatusRunning {
|
|
||||||
return curStatus
|
|
||||||
}
|
|
||||||
if _, err := cc.post("/", nil); err == nil {
|
|
||||||
// Server was started, stop pinging.
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// The socket control server is not ready yet, backoff for waiting it to be ready.
|
|
||||||
bo.BackOff(ctx, err)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cc.post(startedPath, nil)
|
resp, err := cc.post(startedPath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Err(err).Msg("failed to connect to control server")
|
mainLog.Load().Error().Err(err).Msg("failed to connect to control server")
|
||||||
@@ -1188,8 +1175,9 @@ func selfCheckStatus(s service.Service) service.Status {
|
|||||||
|
|
||||||
mainLog.Load().Debug().Msg("ctrld listener is ready")
|
mainLog.Load().Debug().Msg("ctrld listener is ready")
|
||||||
mainLog.Load().Debug().Msg("performing self-check")
|
mainLog.Load().Debug().Msg("performing self-check")
|
||||||
bo = backoff.NewBackoff("self-check", logf, 10*time.Second)
|
bo := backoff.NewBackoff("self-check", logf, 10*time.Second)
|
||||||
bo.LogLongerThan = 500 * time.Millisecond
|
bo.LogLongerThan = 500 * time.Millisecond
|
||||||
|
ctx := context.Background()
|
||||||
maxAttempts := 20
|
maxAttempts := 20
|
||||||
c := new(dns.Client)
|
c := new(dns.Client)
|
||||||
var (
|
var (
|
||||||
@@ -1705,3 +1693,35 @@ func removeProvTokenFromArgs(sc *service.Config) {
|
|||||||
}
|
}
|
||||||
sc.Arguments = a
|
sc.Arguments = a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newSocketControlClient returns new control client after control server was started.
|
||||||
|
func newSocketControlClient(s service.Service, dir string) *controlClient {
|
||||||
|
bo := backoff.NewBackoff("self-check", logf, 10*time.Second)
|
||||||
|
bo.LogLongerThan = 10 * time.Second
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
cc := newControlClient(filepath.Join(dir, ctrldControlUnixSock))
|
||||||
|
|
||||||
|
// The socket control server may not start yet, so attempt to ping
|
||||||
|
// it until we got a response. For each iteration, check ctrld status
|
||||||
|
// to make sure ctrld is still running.
|
||||||
|
for {
|
||||||
|
curStatus, err := s.Status()
|
||||||
|
if err != nil {
|
||||||
|
mainLog.Load().Warn().Err(err).Msg("could not get service status while doing self-check")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if curStatus != service.StatusRunning {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := cc.post("/", nil); err == nil {
|
||||||
|
// Server was started, stop pinging.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// The socket control server is not ready yet, backoff for waiting it to be ready.
|
||||||
|
bo.BackOff(ctx, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return cc
|
||||||
|
}
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ case "$1" in
|
|||||||
echo "failed to stop $name"
|
echo "failed to stop $name"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
exit 1
|
exit 0
|
||||||
;;
|
;;
|
||||||
restart)
|
restart)
|
||||||
$0 stop
|
$0 stop
|
||||||
|
|||||||
@@ -304,7 +304,7 @@ case "$1" in
|
|||||||
logger -c "failed to stop $name"
|
logger -c "failed to stop $name"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
exit 1
|
exit 0
|
||||||
;;
|
;;
|
||||||
restart)
|
restart)
|
||||||
$0 stop
|
$0 stop
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ start() {
|
|||||||
stop() {
|
stop() {
|
||||||
if ! is_running; then
|
if ! is_running; then
|
||||||
elog "$NAME is not running."
|
elog "$NAME is not running."
|
||||||
exit 1
|
exit 0
|
||||||
fi
|
fi
|
||||||
elog "Shutting down $NAME Services: "
|
elog "Shutting down $NAME Services: "
|
||||||
kill -SIGTERM "$(get_pid)"
|
kill -SIGTERM "$(get_pid)"
|
||||||
|
|||||||
Reference in New Issue
Block a user