Simply queueing to parallel manager (#3468)

This commit is contained in:
Anis Elleuch
2020-10-24 14:02:35 -07:00
committed by GitHub
parent fa4da03a4f
commit 6c2325129f
3 changed files with 28 additions and 26 deletions
+6 -7
View File
@@ -455,12 +455,11 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
var quitCh = make(chan struct{})
var statusCh = make(chan URLs)
parallel, queueCh := newParallelManager(statusCh)
parallel := newParallelManager(statusCh)
go func() {
gracefulStop := func() {
close(queueCh)
parallel.wait()
parallel.stopAndWait()
close(statusCh)
}
@@ -531,13 +530,13 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
// Verify if previously copied, notify progress bar.
if isCopied != nil && isCopied(cpURLs.SourceContent.URL.String()) {
queueCh <- func() URLs {
parallel.queueTask(func() URLs {
return doCopyFake(ctx, cpURLs, pg, isMvCmd)
}
})
} else {
queueCh <- func() URLs {
parallel.queueTask(func() URLs {
return doCopy(ctx, cpURLs, pg, encKeyDB, isMvCmd, preserve)
}
})
}
}
}
+13 -16
View File
@@ -202,7 +202,6 @@ type mirrorJob struct {
// Hold operation status information
status Status
queueCh chan func() URLs
parallel *ParallelManager
// channel for status messages
@@ -479,9 +478,9 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo)
// to avoid copying it.
continue
}
mj.queueCh <- func() URLs {
mj.parallel.queueTask(func() URLs {
return mj.doMirrorWatch(ctx, targetPath, tgtSSE, mirrorURL)
}
})
} else if event.Type == EventRemove {
if strings.Contains(event.UserAgent, uaMirrorAppName) {
continue
@@ -498,9 +497,9 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo)
mirrorURL.TotalCount = mj.status.GetCounts()
mirrorURL.TotalSize = mj.status.Get()
if mirrorURL.TargetContent != nil && (mj.opts.isRemove || mj.opts.activeActive) {
mj.queueCh <- func() URLs {
mj.parallel.queueTask(func() URLs {
return mj.doRemove(ctx, mirrorURL)
}
})
}
}
}
@@ -528,9 +527,9 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, stopParallel func()) {
return
}
if err != nil {
mj.queueCh <- func() URLs {
mj.parallel.queueTask(func() URLs {
return URLs{Error: err}
}
})
}
case <-globalContext.Done():
stopParallel()
@@ -585,13 +584,13 @@ func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.Cance
sURLs.TotalSize = mj.status.Get()
if sURLs.SourceContent != nil {
mj.queueCh <- func() URLs {
mj.parallel.queueTask(func() URLs {
return mj.doMirror(ctx, sURLs)
}
})
} else if sURLs.TargetContent != nil && mj.opts.isRemove {
mj.queueCh <- func() URLs {
mj.parallel.queueTask(func() URLs {
return mj.doRemove(ctx, sURLs)
}
})
}
case <-globalContext.Done():
stopParallel()
@@ -614,8 +613,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc
go func() {
defer wg.Done()
stopParallel := func() {
close(mj.queueCh)
mj.parallel.wait()
mj.parallel.stopAndWait()
cancelMirror()
}
mj.watchMirror(ctx, stopParallel)
@@ -628,8 +626,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc
defer wg.Done()
stopParallel := func() {
if !mj.opts.isWatch {
close(mj.queueCh)
mj.parallel.wait()
mj.parallel.stopAndWait()
cancelMirror()
}
}
@@ -657,7 +654,7 @@ func newMirrorJob(srcURL, dstURL string, opts mirrorOptions) *mirrorJob {
watcher: NewWatcher(UTCNow()),
}
mj.parallel, mj.queueCh = newParallelManager(mj.statusCh)
mj.parallel = newParallelManager(mj.statusCh)
// we'll define the status to use here,
// do we want the quiet status? or the progressbar
+9 -3
View File
@@ -50,6 +50,7 @@ type ParallelManager struct {
// Channel to receive tasks to run
queueCh chan func() URLs
// Channel to send back results
resultCh chan URLs
@@ -134,14 +135,19 @@ func (p *ParallelManager) monitorProgress() {
}()
}
func (p *ParallelManager) queueTask(fn func() URLs) {
p.queueCh <- fn
}
// Wait for all workers to finish tasks before shutting down Parallel
func (p *ParallelManager) wait() {
func (p *ParallelManager) stopAndWait() {
close(p.queueCh)
p.wg.Wait()
close(p.stopMonitorCh)
}
// newParallelManager starts new workers waiting for executing tasks
func newParallelManager(resultCh chan URLs) (*ParallelManager, chan func() URLs) {
func newParallelManager(resultCh chan URLs) *ParallelManager {
p := &ParallelManager{
wg: &sync.WaitGroup{},
workersNum: 0,
@@ -158,5 +164,5 @@ func newParallelManager(resultCh chan URLs) (*ParallelManager, chan func() URLs)
// Start monitoring tasks progress
p.monitorProgress()
return p, p.queueCh
return p
}