Nodeconf error and periodicsync partial rename

This commit is contained in:
mcrakhman 2023-04-25 14:00:38 +02:00 committed by Mikhail Iudin
parent fabaf676b8
commit ab74809973
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 31 additions and 30 deletions

View File

@ -57,7 +57,7 @@ func (s *service) Init(a *app.App) (err error) {
s.sync = periodicsync.NewPeriodicSync(updatePeriodSec, 0, func(ctx context.Context) (err error) { s.sync = periodicsync.NewPeriodicSync(updatePeriodSec, 0, func(ctx context.Context) (err error) {
err = s.updateConfiguration(ctx) err = s.updateConfiguration(ctx)
if err != nil { if err != nil {
if err == ErrConfigurationNotChanged { if err == ErrConfigurationNotChanged || err == ErrConfigurationNotFound {
err = nil err = nil
} }
} }

View File

@ -15,64 +15,65 @@ type PeriodicSync interface {
type SyncerFunc func(ctx context.Context) error type SyncerFunc func(ctx context.Context) error
func NewPeriodicSync(periodSeconds int, timeout time.Duration, syncer SyncerFunc, l logger.CtxLogger) PeriodicSync { func NewPeriodicSync(periodSeconds int, timeout time.Duration, caller SyncerFunc, l logger.CtxLogger) PeriodicSync {
// TODO: rename to PeriodicCall
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
ctx = logger.CtxWithFields(ctx, zap.String("rootOp", "periodicSync")) ctx = logger.CtxWithFields(ctx, zap.String("rootOp", "periodicCall"))
return &periodicSync{ return &periodicCall{
syncer: syncer, caller: caller,
log: l, log: l,
syncCtx: ctx, loopCtx: ctx,
syncCancel: cancel, loopCancel: cancel,
syncLoopDone: make(chan struct{}), loopDone: make(chan struct{}),
periodSeconds: periodSeconds, periodSeconds: periodSeconds,
timeout: timeout, timeout: timeout,
} }
} }
type periodicSync struct { type periodicCall struct {
log logger.CtxLogger log logger.CtxLogger
syncer SyncerFunc caller SyncerFunc
syncCtx context.Context loopCtx context.Context
syncCancel context.CancelFunc loopCancel context.CancelFunc
syncLoopDone chan struct{} loopDone chan struct{}
periodSeconds int periodSeconds int
timeout time.Duration timeout time.Duration
} }
func (p *periodicSync) Run() { func (p *periodicCall) Run() {
go p.syncLoop(p.periodSeconds) go p.loop(p.periodSeconds)
} }
func (p *periodicSync) syncLoop(periodSeconds int) { func (p *periodicCall) loop(periodSeconds int) {
period := time.Duration(periodSeconds) * time.Second period := time.Duration(periodSeconds) * time.Second
defer close(p.syncLoopDone) defer close(p.loopDone)
doSync := func() { doCall := func() {
ctx := p.syncCtx ctx := p.loopCtx
if p.timeout != 0 { if p.timeout != 0 {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(p.syncCtx, p.timeout) ctx, cancel = context.WithTimeout(p.loopCtx, p.timeout)
defer cancel() defer cancel()
} }
if err := p.syncer(ctx); err != nil { if err := p.caller(ctx); err != nil {
p.log.Warn("periodic sync error", zap.Error(err)) p.log.Warn("periodic call error", zap.Error(err))
} }
} }
doSync() doCall()
if period > 0 { if period > 0 {
ticker := time.NewTicker(period) ticker := time.NewTicker(period)
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-p.syncCtx.Done(): case <-p.loopCtx.Done():
return return
case <-ticker.C: case <-ticker.C:
doSync() doCall()
} }
} }
} }
} }
func (p *periodicSync) Close() { func (p *periodicCall) Close() {
p.syncCancel() p.loopCancel()
<-p.syncLoopDone <-p.loopDone
} }

View File

@ -16,7 +16,7 @@ func TestPeriodicSync_Run(t *testing.T) {
l := logger.NewNamed("sync") l := logger.NewNamed("sync")
t.Run("diff syncer 1 time", func(t *testing.T) { t.Run("loop call 1 time", func(t *testing.T) {
secs := 0 secs := 0
times := 0 times := 0
diffSyncer := func(ctx context.Context) (err error) { diffSyncer := func(ctx context.Context) (err error) {
@ -30,7 +30,7 @@ func TestPeriodicSync_Run(t *testing.T) {
require.Equal(t, 1, times) require.Equal(t, 1, times)
}) })
t.Run("diff syncer 2 times", func(t *testing.T) { t.Run("loop call 2 times", func(t *testing.T) {
secs := 1 secs := 1
times := 0 times := 0