-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync.go
54 lines (46 loc) · 1021 Bytes
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package gitdb
import (
"fmt"
"github.com/bouggo/log"
"time"
)
func (g *gitdb) Sync() error {
g.syncMu.Lock()
defer g.syncMu.Unlock()
if len(g.config.OnlineRemote) == 0 {
return ErrNoOnlineRemote
}
// if client PC has at least 20% battery life
if !hasSufficientBatteryPower(20) {
return ErrLowBattery
}
log.Info("Syncing database...")
changedFiles := g.driver.changedFiles()
if err := g.driver.sync(); err != nil {
log.Error(err.Error())
return ErrDBSyncFailed
}
// reset loaded blocks
g.loadedBlocks = nil
g.buildIndexSmart(changedFiles)
return nil
}
func (g *gitdb) startSyncClock() {
go func(g *gitdb) {
log.Test(fmt.Sprintf("starting sync clock @ interval %s", g.config.SyncInterval))
ticker := time.NewTicker(g.config.SyncInterval)
for {
select {
case <-g.shutdown:
log.Test("shutting down sync clock")
return
case <-ticker.C:
g.writeMu.Lock()
if err := g.Sync(); err != nil {
log.Error(err.Error())
}
g.writeMu.Unlock()
}
}
}(g)
}