Refactor a bit

This commit is contained in:
Richard Patel
2018-10-28 13:43:45 +01:00
parent 7c4ed9d41e
commit ddfdce9d0f
6 changed files with 280 additions and 284 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"net/url"
"sync/atomic"
)
type Job struct {
@@ -13,6 +14,8 @@ type Job struct {
LastError error
}
var activeTasks int32
func Schedule(c context.Context, remotes <-chan *RemoteDir) {
in, out := makeJobBuffer()
wCtx := WorkerContext{
@@ -32,6 +35,7 @@ func Schedule(c context.Context, remotes <-chan *RemoteDir) {
case remote := <-remotes:
// Enqueue initial job
atomic.AddInt32(&activeTasks, 1)
wCtx.queueJob(Job{
Remote: remote,
Uri: remote.BaseUri,
@@ -48,6 +52,7 @@ func Schedule(c context.Context, remotes <-chan *RemoteDir) {
func (r *RemoteDir) Watch() {
// Wait for all jobs on remote to finish
r.Wait.Wait()
atomic.AddInt32(&activeTasks, -1)
}
func makeJobBuffer() (chan<- Job, <-chan Job) {