Skip to content

Commit

Permalink
fix error handling of poller
Browse files Browse the repository at this point in the history
  • Loading branch information
keyki committed Sep 11, 2017
1 parent aa5f05f commit 9af78e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.1.1
VERSION=0.1.2
OWNER=$(shell glu info| sed -n "s/Owner: //p")

deps:
Expand Down
48 changes: 29 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ import (

"github.com/sethgrid/multibar"

"errors"
"gopkg.in/yaml.v2"
)

func poll(cmd string) (int, int) {
func poll(cmd string) (int, int, error) {
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
if err != nil {
fmt.Printf("[OUTPUT] %s\n", out)
log.Fatal(err)
return 0, 0, errors.New(fmt.Sprintf("ERROR: output: %s, err: %s", out, err.Error()))
}

parts := strings.Split(string(out), "/")
if len(parts) != 2 {
log.Fatalf("Command should have returned <actual>/<total> instead it was: %s", out)
return 0, 0, errors.New(fmt.Sprintf("Command should have returned <actual>/<total> instead it was: %s", out))
}

act, err := strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil {
log.Fatal(err)
return 0, 0, errors.New(fmt.Sprintf("Cannot convert actual copy progress to int: %s", parts[0]))
}

sum, err := strconv.Atoi(strings.TrimSpace(parts[1]))
if err != nil {
log.Fatal(err)
return 0, 0, errors.New(fmt.Sprintf("Cannot convert total blob sizeto int: %s", parts[1]))
}
return act, sum
return act, sum, nil
}

func main() {
Expand All @@ -60,40 +60,50 @@ func main() {
wg.Add(len(obj))

totals := map[string]int{}
log.Println("Getting blob sizes..")
for task, cmd := range obj {
go func(task, cmd string) {
_, sum := poll(cmd)
defer wg.Done()
_, sum, err := poll(cmd)
for err != nil {
time.Sleep(time.Second * 1)
log.Printf("Error finding the total size of the vhd in Storage Account: %s, error: %s", task, err.Error())
_, sum, err = poll(cmd)
}
log.Printf("Blob size of vhd in Storage Account of %s is: %d", task, sum)
totals[task] = sum
wg.Done()
}(task, cmd)
}
wg.Wait()

wg.Add(len(obj))
for task, cmd := range obj {
p := progressBars.MakeBar(totals[task], fmt.Sprintf("%-30s", task))
go func(task, cmd string, progressFn multibar.ProgressFunc) {

act, sum := poll(cmd)
progressFn(act)
for act < sum {
act, _ = poll(cmd)
go func(cmd string, progressFn multibar.ProgressFunc) {
defer wg.Done()
act, sum, err := poll(cmd)
if err == nil {
progressFn(act)
}
for act < sum || err != nil {
time.Sleep(time.Second * 1)
act, _, err = poll(cmd)
if err == nil {
progressFn(act)
}
}
progressFn(act)

wg.Done()
}(task, cmd, p)
}(cmd, p)
}

go progressBars.Listen()
//time.Sleep(time.Second * 3)

for _, b := range progressBars.Bars {
b.Update(0)
}

wg.Wait()
time.Sleep(time.Second * 1)

fmt.Println("DONE")
}

0 comments on commit 9af78e3

Please sign in to comment.