diff --git a/internal/machine/docker/manager.go b/internal/machine/docker/manager.go index 1d15231a..21eb8705 100644 --- a/internal/machine/docker/manager.go +++ b/internal/machine/docker/manager.go @@ -20,6 +20,12 @@ const ( EventsDebounceInterval = 100 * time.Millisecond // SyncInterval defines a regular interval to sync containers to the cluster store. SyncInterval = 30 * time.Second + + // SyncStatusSynced indicates that a container record is synchronised with the Docker state. + SyncStatusSynced = "synced" + // SyncStatusOutdated indicates that a container record may be outdated, for example, due to being unable + // to retrieve the container's state from the Docker daemon or when the machine is being stopped or restarted. + SyncStatusOutdated = "outdated" ) type Manager struct { @@ -91,6 +97,7 @@ func (d *Manager) WatchAndSyncContainers(ctx context.Context, store *store.Store select { case e := <-eventCh: switch e.Action { + // Actions that may trigger a container state change. case events.ActionStart, events.ActionStop, events.ActionPause, diff --git a/internal/machine/network.go b/internal/machine/network.go index b6d3fa98..c0f766c6 100644 --- a/internal/machine/network.go +++ b/internal/machine/network.go @@ -188,16 +188,15 @@ func (nc *networkController) prepareAndWatchDocker(ctx context.Context) error { } if err = manager.EnsureUncloudNetwork(ctx, nc.state.Network.Subnet); err != nil { - return err + return fmt.Errorf("ensure Docker network: %w", err) } slog.Info("Docker network configured.") - slog.Info("Watching Docker containers and syncing them to the cluster store.") + slog.Info("Watching Docker containers and syncing them to cluster store.") // Retry to watch and sync containers until the context is done. boff := backoff.WithContext(backoff.NewExponentialBackOff( backoff.WithInitialInterval(100*time.Millisecond), backoff.WithMaxInterval(5*time.Second), - // Retry indefinitely. backoff.WithMaxElapsedTime(0), ), ctx) watchAndSync := func() error { diff --git a/internal/machine/store/schema.sql b/internal/machine/store/schema.sql index e8f6463e..8ce87966 100644 --- a/internal/machine/store/schema.sql +++ b/internal/machine/store/schema.sql @@ -23,6 +23,8 @@ CREATE TABLE containers machine_id TEXT NOT NULL DEFAULT '', service_id TEXT AS (json_extract(container, '$.Labels."uncloud.service.id"')), service_name TEXT AS (json_extract(container, '$.Labels."uncloud.service.name"')), + -- sync_status indicates if the record reflects the actual Docker state of the container. + sync_status TEXT NOT NULL DEFAULT '', -- updated_at is the last time the record was updated. updated_at TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00' );