From c83e5d268a0f55033e6231f0550a50d4e099792f Mon Sep 17 00:00:00 2001
From: Renan DelValle <rdelval1@binghamton.edu>
Date: Thu, 20 Oct 2016 14:39:48 -0400
Subject: [PATCH] Monitor Instances now returns a tuple so an error can be
 passed instead of exiting on error. Tuple is: Result of action, error if it
 exists.

---
 examples/client.go |  6 +++---
 monitors.go        | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/examples/client.go b/examples/client.go
index 3bff6e8..252f3b0 100644
--- a/examples/client.go
+++ b/examples/client.go
@@ -124,7 +124,7 @@ func main() {
 		fmt.Println(resp.String())
 
 		if resp.ResponseCode == aurora.ResponseCode_OK {
-			if !monitor.Instances(job.JobKey(), job.GetInstanceCount(), 5, 50) {
+			if ok, err := monitor.Instances(job.JobKey(), job.GetInstanceCount(), 5, 50); !ok || err != nil {
 				_, err := r.KillJob(job.JobKey())
 				if err != nil {
 					fmt.Println(err)
@@ -176,7 +176,7 @@ func main() {
 		}
 
 		if resp.ResponseCode == aurora.ResponseCode_OK {
-			if !monitor.Instances(job.JobKey(), 0, 5, 50) {
+			if ok, err := monitor.Instances(job.JobKey(), 0, 5, 50); !ok || err != nil {
 				fmt.Println("Unable to kill all instances of job")
 				os.Exit(1)
 			}
@@ -226,7 +226,7 @@ func main() {
 		}
 
 		if resp.ResponseCode == aurora.ResponseCode_OK {
-			if !monitor.Instances(job.JobKey(), job.GetInstanceCount()+numOfInstances, 5, 50) {
+			if ok, err := monitor.Instances(job.JobKey(), job.GetInstanceCount()+numOfInstances, 5, 50); !ok || err != nil {
 				fmt.Println("Flexing up failed")
 			}
 		}
diff --git a/monitors.go b/monitors.go
index 00971e5..c7e6895 100644
--- a/monitors.go
+++ b/monitors.go
@@ -21,6 +21,7 @@ import (
 	"github.com/rdelval/gorealis/response"
 	"os"
 	"time"
+	"github.com/pkg/errors"
 )
 
 type Monitor struct {
@@ -44,7 +45,7 @@ func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout
 
 		updateDetail := response.JobUpdateDetails(respDetail)
 
-		if(len(updateDetail) == 0 ) {
+		if len(updateDetail) == 0 {
 			fmt.Println("No update found")
 			return false
 		}
@@ -71,19 +72,18 @@ func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout
 	return false
 }
 
-func (m *Monitor) Instances(key *aurora.JobKey, instances int32, interval int, timeout int) bool {
+func (m *Monitor) Instances(key *aurora.JobKey, instances int32, interval int, timeout int) (bool, error) {
 
 	for i := 0; i*interval < timeout; i++ {
 
 		live, err := m.Client.GetInstanceIds(key, aurora.LIVE_STATES)
 
 		if err != nil {
-			fmt.Println(err)
-			os.Exit(1)
+			return false, errors.Wrap(err, "Unable to communicate with Aurora")
 		}
 
 		if len(live) == int(instances) {
-			return true
+			return true, nil
 		}
 
 		fmt.Println("Polling, instances running: ", len(live))
@@ -91,5 +91,5 @@ func (m *Monitor) Instances(key *aurora.JobKey, instances int32, interval int, t
 	}
 
 	fmt.Println("Timed out")
-	return false
+	return false, nil
 }