From e374f833771fc0b5da317d4907ba1ca89609e20e Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Thu, 19 Jan 2023 16:13:53 +0100 Subject: [PATCH] Fix config timestamps created_at represents the time when the configuration has been persisted to disk. loaded_at represents the time when the configuration has actually been used. If created_at is larger than loaded_at, then the Core needs a reload in order to apply the latest configuration. if created_at is lower than laoded_at, then the Core applied the latest configuration. The value of updated_at is irrelevant and shouldn't be used. --- app/api/api.go | 2 ++ config/data.go | 6 +++--- config/store/json.go | 14 ++++---------- http/handler/api/config.go | 8 ++++++++ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/api/api.go b/app/api/api.go index 170c44d6..d8722c30 100644 --- a/app/api/api.go +++ b/app/api/api.go @@ -257,6 +257,8 @@ func (a *api) Reload() error { return fmt.Errorf("not all variables are set or valid") } + cfg.LoadedAt = time.Now() + store.SetActive(cfg) a.config.store = store diff --git a/config/data.go b/config/data.go index a44cb0b2..6d9d509f 100644 --- a/config/data.go +++ b/config/data.go @@ -10,9 +10,9 @@ import ( // Data is the actual configuration data for the app type Data struct { - CreatedAt time.Time `json:"created_at"` - LoadedAt time.Time `json:"-"` - UpdatedAt time.Time `json:"-"` + CreatedAt time.Time `json:"created_at"` // When this config has been persisted + LoadedAt time.Time `json:"-"` // When this config has been actually used + UpdatedAt time.Time `json:"-"` // Irrelevant Version int64 `json:"version" jsonschema:"minimum=3,maximum=3" format:"int64"` ID string `json:"id"` Name string `json:"name"` diff --git a/config/store/json.go b/config/store/json.go index a63ba627..a6b93bfe 100644 --- a/config/store/json.go +++ b/config/store/json.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "path/filepath" - "time" "github.com/datarhei/core/v16/config" v1 "github.com/datarhei/core/v16/config/v1" @@ -57,14 +56,10 @@ func (c *jsonStore) Set(d *config.Config) error { data := d.Clone() - data.CreatedAt = time.Now() - if err := c.store(data); err != nil { return fmt.Errorf("failed to write JSON to '%s': %w", c.path, err) } - data.UpdatedAt = time.Now() - c.data["base"] = data return nil @@ -89,7 +84,9 @@ func (c *jsonStore) SetActive(d *config.Config) error { return fmt.Errorf("configuration data has errors after validation") } - c.data["merged"] = d.Clone() + data := d.Clone() + + c.data["merged"] = data return nil } @@ -129,15 +126,12 @@ func (c *jsonStore) load(cfg *config.Config) error { cfg.Data = *data - cfg.LoadedAt = time.Now() - cfg.UpdatedAt = cfg.LoadedAt + cfg.UpdatedAt = cfg.CreatedAt return nil } func (c *jsonStore) store(data *config.Config) error { - data.CreatedAt = time.Now() - if len(c.path) == 0 { return nil } diff --git a/http/handler/api/config.go b/http/handler/api/config.go index cde44f79..d2484de2 100644 --- a/http/handler/api/config.go +++ b/http/handler/api/config.go @@ -3,6 +3,7 @@ package api import ( "io" "net/http" + "time" cfgstore "github.com/datarhei/core/v16/config/store" cfgvars "github.com/datarhei/core/v16/config/vars" @@ -71,6 +72,10 @@ func (p *ConfigHandler) Set(c echo.Context) error { } cfg := p.store.Get() + cfgActive := p.store.GetActive() + + // Copy the timestamp of when this config has been used + cfg.LoadedAt = cfgActive.LoadedAt // For each version, set the current config as default config value. This will // allow to set a partial config without destroying the other values. @@ -119,6 +124,9 @@ func (p *ConfigHandler) Set(c echo.Context) error { return api.Err(http.StatusBadRequest, "Invalid config version", "version %d", version.Version) } + cfg.CreatedAt = time.Now() + cfg.UpdatedAt = cfg.CreatedAt + // Now we make a copy from the config and merge it with the environment // variables. If this configuration is valid, we will store the un-merged // one to disk.