mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
Nodeconf error and periodicsync partial rename
This commit is contained in:
parent
fabaf676b8
commit
ab74809973
3 changed files with 31 additions and 30 deletions
|
@ -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) {
|
||||
err = s.updateConfiguration(ctx)
|
||||
if err != nil {
|
||||
if err == ErrConfigurationNotChanged {
|
||||
if err == ErrConfigurationNotChanged || err == ErrConfigurationNotFound {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,64 +15,65 @@ type PeriodicSync interface {
|
|||
|
||||
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 = logger.CtxWithFields(ctx, zap.String("rootOp", "periodicSync"))
|
||||
return &periodicSync{
|
||||
syncer: syncer,
|
||||
ctx = logger.CtxWithFields(ctx, zap.String("rootOp", "periodicCall"))
|
||||
return &periodicCall{
|
||||
caller: caller,
|
||||
log: l,
|
||||
syncCtx: ctx,
|
||||
syncCancel: cancel,
|
||||
syncLoopDone: make(chan struct{}),
|
||||
loopCtx: ctx,
|
||||
loopCancel: cancel,
|
||||
loopDone: make(chan struct{}),
|
||||
periodSeconds: periodSeconds,
|
||||
timeout: timeout,
|
||||
}
|
||||
}
|
||||
|
||||
type periodicSync struct {
|
||||
type periodicCall struct {
|
||||
log logger.CtxLogger
|
||||
syncer SyncerFunc
|
||||
syncCtx context.Context
|
||||
syncCancel context.CancelFunc
|
||||
syncLoopDone chan struct{}
|
||||
caller SyncerFunc
|
||||
loopCtx context.Context
|
||||
loopCancel context.CancelFunc
|
||||
loopDone chan struct{}
|
||||
periodSeconds int
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (p *periodicSync) Run() {
|
||||
go p.syncLoop(p.periodSeconds)
|
||||
func (p *periodicCall) Run() {
|
||||
go p.loop(p.periodSeconds)
|
||||
}
|
||||
|
||||
func (p *periodicSync) syncLoop(periodSeconds int) {
|
||||
func (p *periodicCall) loop(periodSeconds int) {
|
||||
period := time.Duration(periodSeconds) * time.Second
|
||||
defer close(p.syncLoopDone)
|
||||
doSync := func() {
|
||||
ctx := p.syncCtx
|
||||
defer close(p.loopDone)
|
||||
doCall := func() {
|
||||
ctx := p.loopCtx
|
||||
if p.timeout != 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(p.syncCtx, p.timeout)
|
||||
ctx, cancel = context.WithTimeout(p.loopCtx, p.timeout)
|
||||
defer cancel()
|
||||
}
|
||||
if err := p.syncer(ctx); err != nil {
|
||||
p.log.Warn("periodic sync error", zap.Error(err))
|
||||
if err := p.caller(ctx); err != nil {
|
||||
p.log.Warn("periodic call error", zap.Error(err))
|
||||
}
|
||||
}
|
||||
doSync()
|
||||
doCall()
|
||||
if period > 0 {
|
||||
ticker := time.NewTicker(period)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-p.syncCtx.Done():
|
||||
case <-p.loopCtx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
doSync()
|
||||
doCall()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *periodicSync) Close() {
|
||||
p.syncCancel()
|
||||
<-p.syncLoopDone
|
||||
func (p *periodicCall) Close() {
|
||||
p.loopCancel()
|
||||
<-p.loopDone
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ func TestPeriodicSync_Run(t *testing.T) {
|
|||
|
||||
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
|
||||
times := 0
|
||||
diffSyncer := func(ctx context.Context) (err error) {
|
||||
|
@ -30,7 +30,7 @@ func TestPeriodicSync_Run(t *testing.T) {
|
|||
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
|
||||
|
||||
times := 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue