Skip to content

Commit

Permalink
enable cron mode #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Mar 28, 2021
1 parent 794aa32 commit e9853e2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ go get -u github.com/bakito/adguardhome-sync
## Run

```bash

export ORIGIN_URL=https://192.168.1.2:3000
export ORIGIN_USERNAME=username
export ORIGIN_PASSWORD=password
export REPLICA_URL=http://192.168.1.3
export REPLICA_USERNAME=username
export REPLICA_PASSWORD=password

# run once
adguardhome-sync run

# run as daemon
adguardhome-sync run --cron "*/10 * * * *"
```
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
)

const (
configCron = "cron"

configOriginURL = "origin.url"
configOriginAPIPath = "origin.apiPath"
configOriginUsername = "origin.username"
Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var doCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(doCmd)
doCmd.PersistentFlags().String("cron", "", "The cron expression to run in daemon mode")
_ = viper.BindPFlag(configCron, doCmd.PersistentFlags().Lookup("cron"))

doCmd.PersistentFlags().String("origin-url", "", "Origin instance url")
_ = viper.BindPFlag(configOriginURL, doCmd.PersistentFlags().Lookup("origin-url"))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/go-resty/resty/v2 v2.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/robfig/cron/v3 v3.0.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
go.uber.org/zap v1.16.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
38 changes: 30 additions & 8 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,76 @@ import (
"github.com/bakito/adguardhome-sync/pkg/client"
"github.com/bakito/adguardhome-sync/pkg/log"
"github.com/bakito/adguardhome-sync/pkg/types"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)

var (
l = log.GetLogger("sync")
)

// Sync config from origin to replica
func Sync(cfg *types.Config) {
l := log.GetLogger("sync")
if cfg.Cron != "" {
c := cron.New()
cl := l.With("cron", cfg.Cron)
_, err := c.AddFunc(cfg.Cron, func() {
sync(cfg)
})
if err != nil {
cl.With("error", err).Error("Error creating cron job")
return
}
cl.Info("Starting cronjob")
c.Run()
} else {
sync(cfg)
}
}

func sync(cfg *types.Config) {
oc, err := client.New(cfg.Origin)
if err != nil {
l.With("error", err, "url", cfg.Origin.URL).Error("Error creating origin client")
return
}

l = l.With("from", oc.Host())
sl := l.With("from", oc.Host())

o := &origin{}

o.status, err = oc.Status()
if err != nil {
l.With("error", err).Error("Error getting origin status")
sl.With("error", err).Error("Error getting origin status")
return
}

o.rewrites, err = oc.RewriteList()
if err != nil {
l.With("error", err).Error("Error getting origin rewrites")
sl.With("error", err).Error("Error getting origin rewrites")
return
}

o.services, err = oc.Services()
if err != nil {
l.With("error", err).Error("Error getting origin services")
sl.With("error", err).Error("Error getting origin services")
return
}

o.filters, err = oc.Filtering()
if err != nil {
l.With("error", err).Error("Error getting origin filters")
sl.With("error", err).Error("Error getting origin filters")
return
}
o.clients, err = oc.Clients()
if err != nil {
l.With("error", err).Error("Error getting origin clients")
sl.With("error", err).Error("Error getting origin clients")
return
}

replicas := cfg.UniqueReplicas()
for _, replica := range replicas {
syncTo(l, o, replica)
syncTo(sl, o, replica)
}
}

Expand Down

0 comments on commit e9853e2

Please sign in to comment.