github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/services/stop.go (about)

     1  package services
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Sirupsen/logrus"
     7  	"github.com/daticahealth/cli/lib/jobs"
     8  	"github.com/daticahealth/cli/lib/prompts"
     9  	"github.com/daticahealth/cli/lib/volumes"
    10  )
    11  
    12  // CmdStop stops all instances of a given service. All workers and rake tasks will also be stopped
    13  // if applicable.
    14  func CmdStop(svcName, pod string, is IServices, ij jobs.IJobs, iv volumes.IVolumes, ip prompts.IPrompts) error {
    15  	err := ip.YesNo(fmt.Sprintf("Stopping %s will stop all instances of the service, all workers, all rake tasks, and all currently open consoles.", svcName), fmt.Sprintf("Are you sure you want to stop %s? (y/n) ", svcName))
    16  	if err != nil {
    17  		return err
    18  	}
    19  	service, err := is.RetrieveByLabel(svcName)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	if service == nil {
    24  		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"datica services list\" command.", svcName)
    25  	}
    26  	if !service.Redeployable {
    27  		return fmt.Errorf("This service cannot be stopped. Please contact Datica Support at https://datica.com/support if you need the \"%s\" service stopped.", svcName)
    28  	}
    29  	if pod == "csb01" && service.Name != "code" {
    30  		return fmt.Errorf("Only code services can be stopped for this environment, not %s services. Please contact Datica Support at https://datica.com/support if you need the \"%s\" service stopped.", service.Name, svcName)
    31  	}
    32  	volumes, err := iv.List(service.ID)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	if volumes != nil && len(*volumes) > 0 {
    37  		return fmt.Errorf("This service has a storage volume and cannot be stopped. Please contact Datica Support at https://datica.com/support if you need the \"%s\" service stopped.", svcName)
    38  	}
    39  
    40  	page := 0
    41  	pageSize := 100
    42  	for {
    43  		jobs, err := ij.List(service.ID, page, pageSize)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		for _, job := range *jobs {
    49  			if job.Status != "scheduled" && job.Status != "queued" && job.Status != "started" && job.Status != "running" && job.Status != "waiting" {
    50  				logrus.Debugf("Skipping %s job (%s)", job.Status, job.ID)
    51  				continue
    52  			}
    53  			logrus.Debugf("Deleting %s job (%s) on service %s", job.Type, job.ID, service.ID)
    54  			err = ij.Delete(job.ID, service.ID)
    55  			if err != nil {
    56  				return err
    57  			}
    58  		}
    59  		if len(*jobs) < pageSize {
    60  			break
    61  		}
    62  		page++
    63  	}
    64  
    65  	logrus.Printf("Successfully stopped %s. Run \"datica redeploy %s\" to start this service again.", svcName, svcName)
    66  	return nil
    67  }