diff --git a/examples/client.go b/examples/client.go index 2bed4ff..6d511b5 100644 --- a/examples/client.go +++ b/examples/client.go @@ -272,7 +272,7 @@ func main() { fmt.Println(err) os.Exit(1) } - updateJob := realis.NewUpdateJob(taskConfig) + updateJob := realis.NewDefaultUpdateJob(taskConfig) updateJob.InstanceCount(5).RAM(128) resp, err := r.StartJobUpdate(updateJob, "") diff --git a/monitors.go b/monitors.go index f1acb52..5719c96 100644 --- a/monitors.go +++ b/monitors.go @@ -20,7 +20,6 @@ import ( "github.com/pkg/errors" "github.com/rdelval/gorealis/gen-go/apache/aurora" "github.com/rdelval/gorealis/response" - "os" "time" ) @@ -29,7 +28,7 @@ type Monitor struct { } // Polls the scheduler every certain amount of time to see if the update has succeeded -func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout int) bool { +func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout int) (bool, error) { updateQ := aurora.JobUpdateQuery{ Key: &updateKey, @@ -40,14 +39,14 @@ func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout respDetail, err := m.Client.JobUpdateDetails(updateQ) if err != nil { fmt.Println(err) - os.Exit(1) + return false, err } updateDetail := response.JobUpdateDetails(respDetail) if len(updateDetail) == 0 { fmt.Println("No update found") - return false + return false, errors.New("No update found for "+updateKey.String()) } status := updateDetail[0].Update.Summary.State.Status @@ -57,10 +56,10 @@ func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout // if we encounter an inactive state and it is not at rolled forward, update failed if status == aurora.JobUpdateStatus_ROLLED_FORWARD { fmt.Println("Update succeded") - return true + return true, nil } else { fmt.Println("Update failed") - return false + return false, nil } } @@ -69,7 +68,7 @@ func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout } fmt.Println("Timed out") - return false + return false, nil } func (m *Monitor) Instances(key *aurora.JobKey, instances int32, interval int, timeout int) (bool, error) { diff --git a/updatejob.go b/updatejob.go index cb2d8c6..6ef6c57 100644 --- a/updatejob.go +++ b/updatejob.go @@ -25,7 +25,7 @@ type UpdateJob struct { } // Create a default UpdateJob object. -func NewUpdateJob(config *aurora.TaskConfig) *UpdateJob { +func NewDefaultUpdateJob(config *aurora.TaskConfig) *UpdateJob { req := aurora.NewJobUpdateRequest() req.TaskConfig = config @@ -66,6 +66,37 @@ func NewUpdateJob(config *aurora.TaskConfig) *UpdateJob { return &UpdateJob{job, req} } +func NewUpdateJob(config *aurora.TaskConfig, settings *aurora.JobUpdateSettings) *UpdateJob { + + req := aurora.NewJobUpdateRequest() + req.TaskConfig = config + req.Settings = settings + + job := NewJob().(AuroraJob) + job.jobConfig.TaskConfig = config + + // Rebuild resource map from TaskConfig + for ptr := range config.Resources { + if ptr.NumCpus != nil { + job.resources["cpu"].NumCpus = ptr.NumCpus + continue // Guard against Union violations that Go won't enforce + } + + if ptr.RamMb != nil { + job.resources["ram"].RamMb = ptr.RamMb + continue + } + + if ptr.DiskMb != nil { + job.resources["disk"].DiskMb = ptr.DiskMb + continue + } + } + + //TODO(rdelvalle): Deep copy job struct to avoid unexpected behavior + return &UpdateJob{job, req} +} + // Set instance count the job will have after the update. func (u *UpdateJob) InstanceCount(inst int32) *UpdateJob { u.req.InstanceCount = inst