mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 12:22:28 +08:00
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.
This commit is contained in:
@@ -257,6 +257,8 @@ func (a *api) Reload() error {
|
|||||||
return fmt.Errorf("not all variables are set or valid")
|
return fmt.Errorf("not all variables are set or valid")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cfg.LoadedAt = time.Now()
|
||||||
|
|
||||||
store.SetActive(cfg)
|
store.SetActive(cfg)
|
||||||
|
|
||||||
a.config.store = store
|
a.config.store = store
|
||||||
|
@@ -10,9 +10,9 @@ import (
|
|||||||
|
|
||||||
// Data is the actual configuration data for the app
|
// Data is the actual configuration data for the app
|
||||||
type Data struct {
|
type Data struct {
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"` // When this config has been persisted
|
||||||
LoadedAt time.Time `json:"-"`
|
LoadedAt time.Time `json:"-"` // When this config has been actually used
|
||||||
UpdatedAt time.Time `json:"-"`
|
UpdatedAt time.Time `json:"-"` // Irrelevant
|
||||||
Version int64 `json:"version" jsonschema:"minimum=3,maximum=3" format:"int64"`
|
Version int64 `json:"version" jsonschema:"minimum=3,maximum=3" format:"int64"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/datarhei/core/v16/config"
|
"github.com/datarhei/core/v16/config"
|
||||||
v1 "github.com/datarhei/core/v16/config/v1"
|
v1 "github.com/datarhei/core/v16/config/v1"
|
||||||
@@ -57,14 +56,10 @@ func (c *jsonStore) Set(d *config.Config) error {
|
|||||||
|
|
||||||
data := d.Clone()
|
data := d.Clone()
|
||||||
|
|
||||||
data.CreatedAt = time.Now()
|
|
||||||
|
|
||||||
if err := c.store(data); err != nil {
|
if err := c.store(data); err != nil {
|
||||||
return fmt.Errorf("failed to write JSON to '%s': %w", c.path, err)
|
return fmt.Errorf("failed to write JSON to '%s': %w", c.path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data.UpdatedAt = time.Now()
|
|
||||||
|
|
||||||
c.data["base"] = data
|
c.data["base"] = data
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -89,7 +84,9 @@ func (c *jsonStore) SetActive(d *config.Config) error {
|
|||||||
return fmt.Errorf("configuration data has errors after validation")
|
return fmt.Errorf("configuration data has errors after validation")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.data["merged"] = d.Clone()
|
data := d.Clone()
|
||||||
|
|
||||||
|
c.data["merged"] = data
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -129,15 +126,12 @@ func (c *jsonStore) load(cfg *config.Config) error {
|
|||||||
|
|
||||||
cfg.Data = *data
|
cfg.Data = *data
|
||||||
|
|
||||||
cfg.LoadedAt = time.Now()
|
cfg.UpdatedAt = cfg.CreatedAt
|
||||||
cfg.UpdatedAt = cfg.LoadedAt
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonStore) store(data *config.Config) error {
|
func (c *jsonStore) store(data *config.Config) error {
|
||||||
data.CreatedAt = time.Now()
|
|
||||||
|
|
||||||
if len(c.path) == 0 {
|
if len(c.path) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
cfgstore "github.com/datarhei/core/v16/config/store"
|
cfgstore "github.com/datarhei/core/v16/config/store"
|
||||||
cfgvars "github.com/datarhei/core/v16/config/vars"
|
cfgvars "github.com/datarhei/core/v16/config/vars"
|
||||||
@@ -71,6 +72,10 @@ func (p *ConfigHandler) Set(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg := p.store.Get()
|
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
|
// For each version, set the current config as default config value. This will
|
||||||
// allow to set a partial config without destroying the other values.
|
// 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)
|
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
|
// 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
|
// variables. If this configuration is valid, we will store the un-merged
|
||||||
// one to disk.
|
// one to disk.
|
||||||
|
Reference in New Issue
Block a user