fix(storage): only log warning if flushing merged servers to file fails

This commit is contained in:
Quentin McGaw
2025-11-17 19:04:13 +00:00
parent 9ed6cd978d
commit 41cd8fb30d
6 changed files with 35 additions and 21 deletions

View File

@@ -7,3 +7,4 @@ func newNoopLogger() *noopLogger {
} }
func (l *noopLogger) Info(string) {} func (l *noopLogger) Info(string) {}
func (l *noopLogger) Warn(string) {}

View File

@@ -1,3 +1,3 @@
package storage package storage
//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Infoer //go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Logger

View File

@@ -1,5 +1,5 @@
// Code generated by MockGen. DO NOT EDIT. // Code generated by MockGen. DO NOT EDIT.
// Source: github.com/qdm12/gluetun/internal/storage (interfaces: Infoer) // Source: github.com/qdm12/gluetun/internal/storage (interfaces: Logger)
// Package storage is a generated GoMock package. // Package storage is a generated GoMock package.
package storage package storage
@@ -10,37 +10,49 @@ import (
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
) )
// MockInfoer is a mock of Infoer interface. // MockLogger is a mock of Logger interface.
type MockInfoer struct { type MockLogger struct {
ctrl *gomock.Controller ctrl *gomock.Controller
recorder *MockInfoerMockRecorder recorder *MockLoggerMockRecorder
} }
// MockInfoerMockRecorder is the mock recorder for MockInfoer. // MockLoggerMockRecorder is the mock recorder for MockLogger.
type MockInfoerMockRecorder struct { type MockLoggerMockRecorder struct {
mock *MockInfoer mock *MockLogger
} }
// NewMockInfoer creates a new mock instance. // NewMockLogger creates a new mock instance.
func NewMockInfoer(ctrl *gomock.Controller) *MockInfoer { func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
mock := &MockInfoer{ctrl: ctrl} mock := &MockLogger{ctrl: ctrl}
mock.recorder = &MockInfoerMockRecorder{mock} mock.recorder = &MockLoggerMockRecorder{mock}
return mock return mock
} }
// EXPECT returns an object that allows the caller to indicate expected use. // EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockInfoer) EXPECT() *MockInfoerMockRecorder { func (m *MockLogger) EXPECT() *MockLoggerMockRecorder {
return m.recorder return m.recorder
} }
// Info mocks base method. // Info mocks base method.
func (m *MockInfoer) Info(arg0 string) { func (m *MockLogger) Info(arg0 string) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
m.ctrl.Call(m, "Info", arg0) m.ctrl.Call(m, "Info", arg0)
} }
// Info indicates an expected call of Info. // Info indicates an expected call of Info.
func (mr *MockInfoerMockRecorder) Info(arg0 interface{}) *gomock.Call { func (mr *MockLoggerMockRecorder) Info(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockInfoer)(nil).Info), arg0) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockLogger)(nil).Info), arg0)
}
// Warn mocks base method.
func (m *MockLogger) Warn(arg0 string) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Warn", arg0)
}
// Warn indicates an expected call of Warn.
func (mr *MockLoggerMockRecorder) Warn(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLogger)(nil).Warn), arg0)
} }

View File

@@ -95,7 +95,7 @@ func Test_extractServersFromBytes(t *testing.T) {
t.Parallel() t.Parallel()
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
logger := NewMockInfoer(ctrl) logger := NewMockLogger(ctrl)
var previousLogCall *gomock.Call var previousLogCall *gomock.Call
for _, logged := range testCase.logged { for _, logged := range testCase.logged {
call := logger.EXPECT().Info(logged) call := logger.EXPECT().Info(logged)

View File

@@ -13,19 +13,20 @@ type Storage struct {
// the embedded JSON file on every call to the // the embedded JSON file on every call to the
// SyncServers method. // SyncServers method.
hardcodedServers models.AllServers hardcodedServers models.AllServers
logger Infoer logger Logger
filepath string filepath string
} }
type Infoer interface { type Logger interface {
Info(s string) Info(s string)
Warn(s string)
} }
// New creates a new storage and reads the servers from the // New creates a new storage and reads the servers from the
// embedded servers file and the file on disk. // embedded servers file and the file on disk.
// Passing an empty filepath disables the reading and writing of // Passing an empty filepath disables the reading and writing of
// servers. // servers.
func New(logger Infoer, filepath string) (storage *Storage, err error) { func New(logger Logger, filepath string) (storage *Storage, err error) {
// A unit test prevents any error from being returned // A unit test prevents any error from being returned
// and ensures all providers are part of the servers returned. // and ensures all providers are part of the servers returned.
hardcodedServers, _ := parseHardcodedServers() hardcodedServers, _ := parseHardcodedServers()

View File

@@ -52,7 +52,7 @@ func (s *Storage) syncServers() (err error) {
err = s.flushToFile(s.filepath) err = s.flushToFile(s.filepath)
if err != nil { if err != nil {
return fmt.Errorf("writing servers to file: %w", err) s.logger.Warn("failed writing servers to file: " + err.Error())
} }
return nil return nil
} }