Manager: Add custom Duration type for the configuration file
Add a custom `time.Duration` wrapper that can be marshalled to JSON as well. Go's built-in marshaller just treats it as an `int64` and therefore the values are nanoseconds. This new wrapper keeps the JSON representation the same as the YAML marshaller (which uses the `time.Duration.String()` function). In preparation for !104406.
This commit is contained in:
parent
f0fca60427
commit
4492d824cb
@ -193,8 +193,8 @@ func runFlamencoManager() bool {
|
|||||||
e := buildWebService(flamenco, persist, ssdp, socketio, urls, localStorage)
|
e := buildWebService(flamenco, persist, ssdp, socketio, urls, localStorage)
|
||||||
|
|
||||||
timeoutChecker := timeout_checker.New(
|
timeoutChecker := timeout_checker.New(
|
||||||
configService.Get().TaskTimeout,
|
time.Duration(configService.Get().TaskTimeout),
|
||||||
configService.Get().WorkerTimeout,
|
time.Duration(configService.Get().WorkerTimeout),
|
||||||
timeService, persist, taskStateMachine, logStorage, eventBroker)
|
timeService, persist, taskStateMachine, logStorage, eventBroker)
|
||||||
|
|
||||||
// The main context determines the lifetime of the application. All
|
// The main context determines the lifetime of the application. All
|
||||||
@ -240,7 +240,7 @@ func runFlamencoManager() bool {
|
|||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
persist.PeriodicIntegrityCheck(mainCtx,
|
persist.PeriodicIntegrityCheck(mainCtx,
|
||||||
configService.Get().DBIntegrityCheck,
|
time.Duration(configService.Get().DBIntegrityCheck),
|
||||||
mainCtxCancel)
|
mainCtxCancel)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ type Base struct {
|
|||||||
ManagerName string `yaml:"manager_name"`
|
ManagerName string `yaml:"manager_name"`
|
||||||
|
|
||||||
DatabaseDSN string `yaml:"database"`
|
DatabaseDSN string `yaml:"database"`
|
||||||
DBIntegrityCheck time.Duration `yaml:"database_check_period"`
|
DBIntegrityCheck Duration `yaml:"database_check_period"`
|
||||||
|
|
||||||
Listen string `yaml:"listen"`
|
Listen string `yaml:"listen"`
|
||||||
|
|
||||||
@ -92,8 +92,8 @@ type Base struct {
|
|||||||
|
|
||||||
Shaman shaman_config.Config `yaml:"shaman"`
|
Shaman shaman_config.Config `yaml:"shaman"`
|
||||||
|
|
||||||
TaskTimeout time.Duration `yaml:"task_timeout"`
|
TaskTimeout Duration `yaml:"task_timeout"`
|
||||||
WorkerTimeout time.Duration `yaml:"worker_timeout"`
|
WorkerTimeout Duration `yaml:"worker_timeout"`
|
||||||
|
|
||||||
/* This many failures (on a given job+task type combination) will ban a worker
|
/* This many failures (on a given job+task type combination) will ban a worker
|
||||||
* from that task type on that job. */
|
* from that task type on that job. */
|
||||||
@ -109,9 +109,9 @@ type Base struct {
|
|||||||
// GarbageCollect contains the config options for the GC.
|
// GarbageCollect contains the config options for the GC.
|
||||||
type ShamanGarbageCollect struct {
|
type ShamanGarbageCollect struct {
|
||||||
// How frequently garbage collection is performed on the file store:
|
// How frequently garbage collection is performed on the file store:
|
||||||
Period time.Duration `yaml:"period"`
|
Period Duration `yaml:"period"`
|
||||||
// How old files must be before they are GC'd:
|
// How old files must be before they are GC'd:
|
||||||
MaxAge time.Duration `yaml:"maxAge"`
|
MaxAge Duration `yaml:"maxAge"`
|
||||||
// Paths to check for symlinks before GC'ing files.
|
// Paths to check for symlinks before GC'ing files.
|
||||||
ExtraCheckoutDirs []string `yaml:"extraCheckoutPaths"`
|
ExtraCheckoutDirs []string `yaml:"extraCheckoutPaths"`
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ var defaultConfig = Conf{
|
|||||||
ManagerName: "Flamenco",
|
ManagerName: "Flamenco",
|
||||||
Listen: ":8080",
|
Listen: ":8080",
|
||||||
DatabaseDSN: "flamenco-manager.sqlite",
|
DatabaseDSN: "flamenco-manager.sqlite",
|
||||||
DBIntegrityCheck: 10 * time.Minute,
|
DBIntegrityCheck: Duration(10 * time.Minute),
|
||||||
SSDPDiscovery: true,
|
SSDPDiscovery: true,
|
||||||
LocalManagerStoragePath: "./flamenco-manager-storage",
|
LocalManagerStoragePath: "./flamenco-manager-storage",
|
||||||
SharedStoragePath: "", // Empty string means "first run", and should trigger the config setup assistant.
|
SharedStoragePath: "", // Empty string means "first run", and should trigger the config setup assistant.
|
||||||
@ -34,8 +34,8 @@ var defaultConfig = Conf{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
TaskTimeout: 10 * time.Minute,
|
TaskTimeout: Duration(10 * time.Minute),
|
||||||
WorkerTimeout: 1 * time.Minute,
|
WorkerTimeout: Duration(1 * time.Minute),
|
||||||
|
|
||||||
BlocklistThreshold: 3,
|
BlocklistThreshold: 3,
|
||||||
TaskFailAfterSoftFailCount: 3,
|
TaskFailAfterSoftFailCount: 3,
|
||||||
|
57
internal/manager/config/time_duration.go
Normal file
57
internal/manager/config/time_duration.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Duration is a time.Duration with custom JSON/YAML marshallers.
|
||||||
|
type Duration time.Duration
|
||||||
|
|
||||||
|
var _ json.Unmarshaler = (*Duration)(nil)
|
||||||
|
var _ json.Marshaler = Duration(0)
|
||||||
|
var _ yaml.Unmarshaler = (*Duration)(nil)
|
||||||
|
var _ yaml.Marshaler = Duration(0)
|
||||||
|
|
||||||
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(time.Duration(d).String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||||
|
var v any
|
||||||
|
if err := json.Unmarshal(b, &v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return d.unmarshal(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Duration) MarshalYAML() (any, error) {
|
||||||
|
return time.Duration(d).String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Duration) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
stringValue := ""
|
||||||
|
if err := unmarshal(&stringValue); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return d.unmarshal(stringValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Duration) unmarshal(v any) error {
|
||||||
|
switch value := v.(type) {
|
||||||
|
case string:
|
||||||
|
timeDuration, err := time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*d = Duration(timeDuration)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return errors.New("invalid duration")
|
||||||
|
}
|
||||||
|
}
|
104
internal/manager/config/time_duration_test.go
Normal file
104
internal/manager/config/time_duration_test.go
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDuration_UnmarshalJSON(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
want Duration
|
||||||
|
input []byte
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"60s", Duration(time.Second * 60), []byte(`"60s"`), false},
|
||||||
|
{"1m", Duration(time.Second * 60), []byte(`"1m"`), false},
|
||||||
|
{"int", Duration(0), []byte("1"), true},
|
||||||
|
{"float", Duration(0), []byte("1.0"), true},
|
||||||
|
{"empty", Duration(0), []byte{}, true},
|
||||||
|
{"undefined", Duration(0), []byte("undefined"), true},
|
||||||
|
{"null", Duration(0), []byte("null"), true},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var got Duration
|
||||||
|
err := got.UnmarshalJSON(tt.input)
|
||||||
|
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Duration.UnmarshalJSON(%v) error = %v, wantErr %v", tt.input, err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("Duration.UnmarshalJSON(%v) got = %v, want = %v", tt.input, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDuration_MarshalJSON(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
want []byte
|
||||||
|
input Duration
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"zero", []byte(`"0s"`), Duration(0), false},
|
||||||
|
{"1ns", []byte(`"1ns"`), Duration(1), false},
|
||||||
|
{"1m", []byte(`"1m0s"`), Duration(time.Second * 60), false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := tt.input.MarshalJSON()
|
||||||
|
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Duration.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("Duration.MarshalJSON() got = %v, want = %v", string(got), string(tt.want))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDuration_JSONDocument(t *testing.T) {
|
||||||
|
type TestStruct struct {
|
||||||
|
TestValue Duration `json:"test_value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
testValue := TestStruct{Duration(time.Hour * 3)}
|
||||||
|
jsonBytes, err := json.Marshal(testValue)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, `{"test_value":"3h0m0s"}`, string(jsonBytes))
|
||||||
|
|
||||||
|
roundtripValue := TestStruct{}
|
||||||
|
err = json.Unmarshal(jsonBytes, &roundtripValue)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, testValue, roundtripValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDuration_YAMLDocument(t *testing.T) {
|
||||||
|
type TestStruct struct {
|
||||||
|
TestValue Duration `yaml:"test_value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
testValue := TestStruct{Duration(time.Hour * 3)}
|
||||||
|
yamlBytes, err := yaml.Marshal(testValue)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "test_value: 3h0m0s\n", string(yamlBytes))
|
||||||
|
|
||||||
|
roundtripValue := TestStruct{}
|
||||||
|
err = yaml.Unmarshal(yamlBytes, &roundtripValue)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, testValue, roundtripValue)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user