@@ -10,10 +10,12 @@ import (
1010 "github.com/sirupsen/logrus"
1111 "github.com/spf13/cobra"
1212 "strings"
13+ "sync"
1314)
1415
1516var (
16- flagSyncer string
17+ flagSyncer string
18+ flagParallelism int
1719)
1820
1921var syncCmd = & cobra.Command {
@@ -27,7 +29,12 @@ var syncCmd = &cobra.Command{
2729 defer cache .Close ()
2830
2931 // iterate syncer's
32+ var wg sync.WaitGroup
33+ pos := 0
34+
3035 for _ , syncerConfig := range config .Config .Syncer {
36+ syncerConfig := syncerConfig
37+
3138 log := log .WithField ("syncer" , syncerConfig .Name )
3239
3340 // skip disabled syncer(s)
@@ -43,54 +50,71 @@ var syncCmd = &cobra.Command{
4350 }
4451
4552 // create syncer
46- sync , err := syncer .New (config .Config , & syncerConfig , syncerConfig .Name )
53+ syncr , err := syncer .New (config .Config , & syncerConfig , syncerConfig .Name )
4754 if err != nil {
4855 log .WithError (err ).Error ("Failed initializing syncer, skipping..." )
4956 continue
5057 }
5158
52- serviceAccountCount := sync .RemoteServiceAccountFiles .ServiceAccountsCount ()
59+ serviceAccountCount := syncr .RemoteServiceAccountFiles .ServiceAccountsCount ()
5360 if serviceAccountCount > 0 {
54- sync .Log .WithField ("found_files" , serviceAccountCount ).Info ("Loaded service accounts" )
61+ syncr .Log .WithField ("found_files" , serviceAccountCount ).Info ("Loaded service accounts" )
5562 } else {
5663 // no service accounts were loaded
5764 // check to see if any of the copy or sync remote(s) are banned
58- banned , expiry := rclone .AnyRemotesBanned (sync .Config .Remotes .Copy )
65+ banned , expiry := rclone .AnyRemotesBanned (syncr .Config .Remotes .Copy )
5966 if banned && ! expiry .IsZero () {
6067 // one of the copy remotes is banned, abort
61- sync .Log .WithFields (logrus.Fields {
68+ syncr .Log .WithFields (logrus.Fields {
6269 "expires_time" : expiry ,
6370 "expires_in" : humanize .Time (expiry ),
6471 }).Warn ("Cannot proceed with sync as a copy remote is banned" )
6572 continue
6673 }
6774
68- banned , expiry = rclone .AnyRemotesBanned (sync .Config .Remotes .Sync )
75+ banned , expiry = rclone .AnyRemotesBanned (syncr .Config .Remotes .Sync )
6976 if banned && ! expiry .IsZero () {
7077 // one of the sync remotes is banned, abort
71- sync .Log .WithFields (logrus.Fields {
78+ syncr .Log .WithFields (logrus.Fields {
7279 "expires_time" : expiry ,
7380 "expires_in" : humanize .Time (expiry ),
7481 }).Warn ("Cannot proceed with sync as a sync remote is banned" )
7582 continue
7683 }
7784 }
7885
86+ pos += 1
7987 log .Info ("Syncer commencing..." )
8088
8189 // perform sync
82- if err := performSync (sync ); err != nil {
83- sync .Log .WithError (err ).Error ("Error occurred while running syncer, skipping..." )
84- continue
90+ wg .Add (1 )
91+ go func (s * syncer.Syncer , w * sync.WaitGroup ) {
92+ defer w .Done ()
93+
94+ // run syncer
95+ if err := performSync (s ); err != nil {
96+ s .Log .WithError (err ).Error ("Error occurred while running syncer, skipping..." )
97+ }
98+ }(syncr , & wg )
99+
100+ // parallelism limit reached?
101+ if pos % flagParallelism == 0 {
102+ log .Info ("Waiting before starting next syncer" )
103+ wg .Wait ()
85104 }
86105 }
106+
107+ // wait for all syncers to finish
108+ log .Info ("Waiting for syncer(s) to finish" )
109+ wg .Wait ()
87110 },
88111}
89112
90113func init () {
91114 rootCmd .AddCommand (syncCmd )
92115
93116 syncCmd .Flags ().StringVarP (& flagSyncer , "syncer" , "s" , "" , "Run for a specific syncer" )
117+ syncCmd .Flags ().IntVarP (& flagParallelism , "parallelism" , "p" , 1 , "Max parallel syncers" )
94118}
95119
96120func performSync (s * syncer.Syncer ) error {
0 commit comments