add sync_status field to containers table

This commit is contained in:
Pavel Sviderski
2024-11-18 19:50:40 +10:00
parent 54a8b09c9f
commit 2819ba63aa
3 changed files with 11 additions and 3 deletions
+7
View File
@@ -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,
+2 -3
View File
@@ -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 {
+2
View File
@@ -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'
);