github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/autoscale.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/drycc/controller-sdk-go/api"
     7  	"github.com/drycc/controller-sdk-go/appsettings"
     8  )
     9  
    10  // AutoscaleList tells the informations about app's autoscale status
    11  func (d *DryccCmd) AutoscaleList(appID string) error {
    12  	s, appID, err := load(d.ConfigFile, appID)
    13  
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	appSettings, err := appsettings.List(s.Client, appID)
    19  	if d.checkAPICompatibility(s.Client, err) != nil {
    20  		return err
    21  	}
    22  	if appSettings.Autoscale == nil {
    23  		d.Println("No autoscale rules found.")
    24  	} else {
    25  		table := d.getDefaultFormatTable([]string{"UUID", "PTYPE", "PERCENT", "MIN", "MAX"})
    26  		for process, kv := range appSettings.Autoscale {
    27  			table.Append([]string{
    28  				appSettings.UUID,
    29  				process,
    30  				fmt.Sprintf("%d", (*kv).CPUPercent),
    31  				fmt.Sprintf("%d", (*kv).Min),
    32  				fmt.Sprintf("%d", (*kv).Max),
    33  			})
    34  		}
    35  		table.Render()
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  // AutoscaleSet sets autoscale options for the app.
    42  func (d *DryccCmd) AutoscaleSet(appID string, processType string, min int, max int, CPUPercent int) error {
    43  	s, appID, err := load(d.ConfigFile, appID)
    44  
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	d.Printf("Applying autoscale settings for process type %s on %s... ", processType, appID)
    50  
    51  	quit := progress(d.WOut)
    52  	data := map[string]*api.Autoscale{
    53  		processType: {
    54  			Min:        min,
    55  			Max:        max,
    56  			CPUPercent: CPUPercent,
    57  		},
    58  	}
    59  	_, err = appsettings.Set(s.Client, appID, api.AppSettings{Autoscale: data})
    60  
    61  	quit <- true
    62  	<-quit
    63  
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	d.Println("done")
    69  	return nil
    70  }
    71  
    72  // AutoscaleUnset removes autoscale for the app.
    73  func (d *DryccCmd) AutoscaleUnset(appID string, processType string) error {
    74  	s, appID, err := load(d.ConfigFile, appID)
    75  
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	d.Printf("Removing autoscale for process type %s on %s... ", processType, appID)
    81  
    82  	quit := progress(d.WOut)
    83  	data := map[string]*api.Autoscale{
    84  		processType: nil,
    85  	}
    86  	_, err = appsettings.Set(s.Client, appID, api.AppSettings{Autoscale: data})
    87  
    88  	quit <- true
    89  	<-quit
    90  
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	d.Println("done")
    96  	return nil
    97  }