convert systemd unit to Type=notify and report readiness

This commit is contained in:
Pavel Sviderski
2024-09-20 23:11:00 +10:00
parent ea8dfca14c
commit 73be824f30
5 changed files with 35 additions and 6 deletions
+14
View File
@@ -3,6 +3,7 @@ package daemon
import (
"context"
"fmt"
systemd "github.com/coreos/go-systemd/daemon"
"log/slog"
"uncloud/internal/machine"
)
@@ -27,5 +28,18 @@ func New(dataDir string) (*Daemon, error) {
func (d *Daemon) Run(ctx context.Context) error {
slog.Info("Starting machine.")
// Notify systemd that the daemon is ready when the machine is started.
go func() {
select {
case <-d.machine.Started():
_, err := systemd.SdNotify(false, systemd.SdNotifyReady)
if err != nil {
slog.Error("Failed to notify systemd that the daemon is ready.", "error", err)
}
case <-ctx.Done():
}
}()
return d.machine.Run(ctx)
}
+9
View File
@@ -37,6 +37,8 @@ type Machine struct {
config Config
state *State
// started is closed when the machine is ready to serve requests on the local API server.
started chan struct{}
// initialised is signalled when the machine is configured as a member of a cluster.
initialised chan struct{}
@@ -90,6 +92,7 @@ func NewMachine(config *Config) (*Machine, error) {
m := &Machine{
config: *config,
state: state,
started: make(chan struct{}),
initialised: make(chan struct{}, 1),
cluster: c,
newMachinesCh: c.WatchNewMachines(),
@@ -110,6 +113,11 @@ func newGRPCServer(m pb.MachineServer, c pb.ClusterServer) *grpc.Server {
return s
}
// Started returns a channel that is closed when the machine is ready to serve requests on the local API server.
func (m *Machine) Started() <-chan struct{} {
return m.started
}
// IsInitialised returns true if the machine has been configured as a member of a cluster,
// either by initialising a new cluster on it or joining an existing one.
func (m *Machine) IsInitialised() bool {
@@ -141,6 +149,7 @@ func (m *Machine) Run(ctx context.Context) error {
return nil
},
)
close(m.started)
// Control loop for managing the network controller.
errGroup.Go(