Skip to content

Commit

Permalink
improve performance of a couple loops
Browse files Browse the repository at this point in the history
  • Loading branch information
bohendo committed Mar 13, 2024
1 parent 4671849 commit 9dc8c34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/cloudexec/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func ConfirmDeleteDroplets(config config.Config, dropletName string, instanceToJ
if err != nil {
return confirmedToDelete, fmt.Errorf("Failed to get droplets by name: %w", err)
}
if instanceToJobs == nil {
return confirmedToDelete, fmt.Errorf("Given instanceToJobs argument must not be nil")
}
if len(instances) > 0 {
fmt.Printf("Existing %s instance(s) found:\n", dropletName)
for _, instance := range instances {
// get a pretty string describing the jobs associated with this instance
if instanceToJobs == nil {
return confirmedToDelete, fmt.Errorf("Given instanceToJobs argument must not be nil")
}
jobs := instanceToJobs[int64(instance.ID)]
var prettyJobs string
if len(jobs) == 0 {
Expand Down
17 changes: 10 additions & 7 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,19 @@ func (s *State) DeleteJob(jobID int64) {
}
}

func (s *State) CancelRunningJobs(config config.Config, bucketName string, toCancel []int64) error {
func (s *State) CancelRunningJobs(config config.Config, bucketName string, toCancelSlice []int64) error {
// Gather list of ids to mark as cancelled
toCancel := make(map[int64]bool)
for _, id := range toCancelSlice {
toCancel[id] = true
}

// Mark any running jobs as cancelled
for i, job := range s.Jobs {
if job.Status == Running || job.Status == Provisioning {
for _, id := range toCancel {
if id == job.ID {
fmt.Printf("Setting status of job %d to 'Cancelled'\n", job.ID)
s.Jobs[i].Status = Cancelled
break
}
if _, exists := toCancel[job.ID]; exists {
fmt.Printf("Setting status of job %d to 'Cancelled'\n", job.ID)
s.Jobs[i].Status = Cancelled
}
}
}
Expand Down

0 comments on commit 9dc8c34

Please sign in to comment.