func CmdRm(svcName, target string, iw IWorker, is services.IServices, ip prompts.IPrompts, ij jobs.IJobs) error { service, err := is.RetrieveByLabel(svcName) if err != nil { return err } if service == nil { return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName) } err = ip.YesNo(fmt.Sprintf("Removing the worker target %s for service %s will automatically stop all existing worker jobs with that target, would you like to proceed? (y/n) ", target, svcName)) if err != nil { return err } jobs, err := ij.RetrieveByTarget(service.ID, target, 1, 1000) if err != nil { return err } for _, j := range *jobs { err = ij.Delete(j.ID, service.ID) if err != nil { return err } } workers, err := iw.Retrieve(service.ID) if err != nil { return err } delete(workers.Workers, target) err = iw.Update(service.ID, workers) if err != nil { return err } logrus.Printf("Successfully removed all workers with target %s for service %s", target, svcName) return nil }
// CmdStop stops all instances of a given service. All workers and rake tasks will also be stopped // if applicable. func CmdStop(svcName string, is IServices, ij jobs.IJobs, ip prompts.IPrompts) error { err := ip.YesNo(fmt.Sprintf("Are you sure you want to stop %s? This will stop all instances of the service, all workers, all rake tasks, and all currently open consoles. (y/n) ", svcName)) if err != nil { return err } service, err := is.RetrieveByLabel(svcName) if err != nil { return err } if service == nil { return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName) } if !service.Redeployable { return fmt.Errorf("This service cannot be stopped. Please contact Catalyze Support at [email protected] if you need the \"%s\" service stopped.", svcName) } page := 0 pageSize := 100 for { jobs, err := ij.List(service.ID, page, pageSize) if err != nil { return err } for _, job := range *jobs { if job.Status != "scheduled" && job.Status != "queued" && job.Status != "started" && job.Status != "running" && job.Status != "waiting" { logrus.Debugf("Skipping %s job (%s)", job.Status, job.ID) continue } logrus.Debugf("Deleting %s job (%s) on service %s", job.Type, job.ID, service.ID) err = ij.Delete(job.ID, service.ID) if err != nil { return err } } if len(*jobs) < pageSize { break } page++ } logrus.Printf("Successfully stopped %s. Run \"catalyze redeploy %s\" to start this service again.", svcName, svcName) return nil }
func CmdScale(svcName, target, scaleString string, iw IWorker, is services.IServices, ip prompts.IPrompts, ij jobs.IJobs) error { service, err := is.RetrieveByLabel(svcName) if err != nil { return err } if service == nil { return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName) } scaleFunc, changeInScale, err := iw.ParseScale(scaleString) if err != nil { return err } workers, err := iw.Retrieve(service.ID) if err != nil { return err } scale := scaleFunc(workers.Workers[target], changeInScale) if existingScale, ok := workers.Workers[target]; !ok || scale > existingScale { logrus.Printf("Deploying %d new workers with target %s for service %s", scale-existingScale, target, svcName) workers.Workers[target] = scale err = iw.Update(service.ID, workers) if err != nil { return err } err = ij.DeployTarget(target, service.ID) if err != nil { return err } logrus.Printf("Successfully deployed %d new workers with target %s for service %s and set the scale to %d", scale-existingScale, target, svcName, scale) } else if scale < existingScale { err = ip.YesNo(fmt.Sprintf("Scaling down the %s target from %d to %d for service %s will automatically stop %d jobs, would you like to proceed? (y/n) ", target, existingScale, scale, svcName, existingScale-scale)) if err != nil { return err } jobs, err := ij.RetrieveByTarget(service.ID, target, 1, 1000) if err != nil { return err } deleteLimit := existingScale - scale deleted := 0 for _, j := range *jobs { err = ij.Delete(j.ID, service.ID) if err != nil { return err } deleted++ if deleted == deleteLimit { break } } workers.Workers[target] = scale err = iw.Update(service.ID, workers) if err != nil { return err } logrus.Printf("Successfully removed %d existing workers with target %s for service %s and set the scale to %d", existingScale-scale, target, svcName, scale) } else { logrus.Printf("Worker target %s for service %s is already at a scale of %d", target, svcName, scale) } return nil }