2023-10-10 21:14:40 +02:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
2023-10-20 13:41:06 +02:00
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-10 21:14:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SyncScheduler: Loops through all nodes in the mesh and runs a schedule to
|
|
|
|
// sync each event
|
|
|
|
type SyncScheduler interface {
|
|
|
|
Run() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:46 +02:00
|
|
|
// SyncSchedulerImpl scheduler for sync scheduling
|
2023-10-10 21:14:40 +02:00
|
|
|
type SyncSchedulerImpl struct {
|
2023-10-20 13:41:06 +02:00
|
|
|
syncRate int
|
|
|
|
quit chan struct{}
|
|
|
|
server *ctrlserver.MeshCtrlServer
|
|
|
|
syncer Syncer
|
2023-10-10 21:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements SyncScheduler.
|
|
|
|
func (s *SyncSchedulerImpl) Run() error {
|
2023-10-20 13:41:06 +02:00
|
|
|
ticker := time.NewTicker(time.Duration(s.syncRate) * time.Second)
|
2023-10-10 21:14:40 +02:00
|
|
|
|
|
|
|
quit := make(chan struct{})
|
|
|
|
s.quit = quit
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2023-10-20 13:41:06 +02:00
|
|
|
err := s.syncer.SyncMeshes()
|
|
|
|
|
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
2023-10-20 13:41:06 +02:00
|
|
|
}
|
2023-10-10 21:14:40 +02:00
|
|
|
break
|
|
|
|
case <-quit:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop implements SyncScheduler.
|
|
|
|
func (s *SyncSchedulerImpl) Stop() error {
|
|
|
|
close(s.quit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
func NewSyncScheduler(s *ctrlserver.MeshCtrlServer, syncRequester SyncRequester, syncRate int) SyncScheduler {
|
|
|
|
syncer := NewSyncer(s.MeshManager, syncRequester)
|
|
|
|
return &SyncSchedulerImpl{server: s, syncRate: syncRate, syncer: syncer}
|
2023-10-10 21:14:40 +02:00
|
|
|
}
|