use periodicSync
This commit is contained in:
parent
7348f39eff
commit
adb800d56a
@ -5,10 +5,10 @@ import (
|
||||
commonaccount "github.com/anytypeio/any-sync/accountservice"
|
||||
"github.com/anytypeio/any-sync/app"
|
||||
"github.com/anytypeio/any-sync/app/logger"
|
||||
"github.com/anytypeio/any-sync/util/periodicsync"
|
||||
"github.com/anytypeio/go-chash"
|
||||
"go.uber.org/zap"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CName = "common.nodeconf"
|
||||
@ -36,12 +36,10 @@ type service struct {
|
||||
store Store
|
||||
last NodeConf
|
||||
mu sync.RWMutex
|
||||
updateCtx context.Context
|
||||
updateCtxCancel context.CancelFunc
|
||||
sync periodicsync.PeriodicSync
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.updateCtx, s.updateCtxCancel = context.WithCancel(context.Background())
|
||||
s.config = a.MustComponent("config").(ConfigGetter).GetNodeConf()
|
||||
s.accountId = a.MustComponent(commonaccount.CName).(commonaccount.Service).Account().PeerId
|
||||
s.source = a.MustComponent(CNameSource).(Source)
|
||||
@ -51,6 +49,15 @@ func (s *service) Init(a *app.App) (err error) {
|
||||
lastStored = s.config
|
||||
err = nil
|
||||
}
|
||||
s.sync = periodicsync.NewPeriodicSync(600, 0, func(ctx context.Context) (err error) {
|
||||
err = s.updateConfiguration(ctx)
|
||||
if err != nil {
|
||||
if err == ErrConfigurationNotChanged {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
return
|
||||
}, log)
|
||||
return s.setLastConfiguration(lastStored)
|
||||
}
|
||||
|
||||
@ -59,36 +66,10 @@ func (s *service) Name() (name string) {
|
||||
}
|
||||
|
||||
func (s *service) Run(_ context.Context) (err error) {
|
||||
go s.updateLoop(context.Background())
|
||||
s.sync.Run()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) updateLoop(ctx context.Context) {
|
||||
ticker := time.NewTicker(time.Minute * 10)
|
||||
defer ticker.Stop()
|
||||
|
||||
updateConf := func() {
|
||||
err := s.updateConfiguration(ctx)
|
||||
if err != nil {
|
||||
if err == ErrConfigurationNotChanged {
|
||||
return
|
||||
}
|
||||
log.Info("can't update configuration", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
updateConf()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-s.updateCtx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
updateConf()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *service) updateConfiguration(ctx context.Context) (err error) {
|
||||
last, err := s.source.GetLast(ctx, s.Configuration().Id)
|
||||
if err != nil {
|
||||
@ -220,8 +201,6 @@ func (s *service) NodeTypes(nodeId string) []NodeType {
|
||||
}
|
||||
|
||||
func (s *service) Close(ctx context.Context) (err error) {
|
||||
if s.updateCtxCancel != nil {
|
||||
s.updateCtxCancel()
|
||||
}
|
||||
s.sync.Close()
|
||||
return
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user