2019-06-16 00:05:06 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-06-16 16:53:30 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-06-16 00:05:06 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/cloudbox/providers"
|
|
|
|
)
|
|
|
|
|
2019-06-16 16:53:30 +00:00
|
|
|
type SyncConfig struct {
|
|
|
|
ForceUseChecksum bool `yaml:"force_use_checksum"`
|
|
|
|
ScanInterval time.Duration `yaml:"scan_interval"`
|
|
|
|
}
|
|
|
|
|
2019-06-16 00:05:06 +00:00
|
|
|
type Sync struct {
|
|
|
|
db *sql.DB
|
2019-06-16 16:53:30 +00:00
|
|
|
conf SyncConfig
|
2019-06-16 00:05:06 +00:00
|
|
|
local, remote providers.CloudProvider
|
2019-06-16 16:53:30 +00:00
|
|
|
|
|
|
|
stop chan struct{}
|
2019-06-16 00:05:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 16:53:30 +00:00
|
|
|
func New(local, remote providers.CloudProvider, db *sql.DB, conf SyncConfig) *Sync {
|
2019-06-16 00:05:06 +00:00
|
|
|
return &Sync{
|
|
|
|
db: db,
|
2019-06-16 16:53:30 +00:00
|
|
|
conf: conf,
|
2019-06-16 00:05:06 +00:00
|
|
|
local: local,
|
|
|
|
remote: remote,
|
2019-06-16 16:53:30 +00:00
|
|
|
|
|
|
|
stop: make(chan struct{}),
|
2019-06-16 00:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sync) Run() error {
|
2019-06-16 16:53:30 +00:00
|
|
|
if err := s.initSchema(); err != nil {
|
|
|
|
return errors.Wrap(err, "Unable to initialize database schema")
|
2019-06-16 00:05:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 16:53:30 +00:00
|
|
|
var (
|
|
|
|
hashMethod = s.remote.GetChecksumMethod()
|
|
|
|
refresh = time.NewTimer(s.conf.ScanInterval)
|
|
|
|
useChecksum = s.remote.Capabilities().Has(providers.CapAutoChecksum) || s.conf.ForceUseChecksum
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-refresh.C:
|
|
|
|
// TODO: Execute rescan & sync
|
|
|
|
refresh.Reset(s.conf.ScanInterval)
|
|
|
|
|
|
|
|
case <-s.stop:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 00:05:06 +00:00
|
|
|
}
|
2019-06-16 16:53:30 +00:00
|
|
|
|
|
|
|
func (s *Sync) Stop() { s.stop <- struct{}{} }
|