github.com/openimsdk/tools@v0.0.49/utils/mageutil/bricks.go (about)

     1  package mageutil
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  // StopBinaries iterates over all binary files and terminates their corresponding processes.
    13  func StopBinaries() {
    14  	for binary := range serviceBinaries {
    15  		fullPath := GetBinFullPath(binary)
    16  		KillExistBinary(fullPath)
    17  	}
    18  }
    19  
    20  // StartBinaries Start all binary services.
    21  func StartBinaries() error {
    22  	for binary, count := range serviceBinaries {
    23  		binFullPath := filepath.Join(OpenIMOutputHostBin, binary)
    24  		for i := 0; i < count; i++ {
    25  			args := []string{"-i", strconv.Itoa(i), "-c", OpenIMOutputConfig}
    26  			cmd := exec.Command(binFullPath, args...)
    27  			fmt.Printf("Starting %s\n", cmd.String())
    28  			cmd.Dir = OpenIMOutputHostBin
    29  			cmd.Stdout = os.Stdout
    30  			cmd.Stderr = os.Stderr
    31  			if err := cmd.Start(); err != nil {
    32  				fmt.Fprintf(os.Stderr, "Failed to start %s with args %v: %v\n", binFullPath, args, err)
    33  				return err
    34  			}
    35  		}
    36  	}
    37  	return nil
    38  }
    39  
    40  // StartTools starts all tool binaries.
    41  func StartTools() error {
    42  	for _, tool := range toolBinaries {
    43  		toolFullPath := GetToolFullPath(tool)
    44  		cmd := exec.Command(toolFullPath, "-c", OpenIMOutputConfig)
    45  		fmt.Printf("Starting %s\n", cmd.String())
    46  		cmd.Dir = OpenIMOutputHostBinTools
    47  		cmd.Stdout = os.Stdout
    48  		cmd.Stderr = os.Stderr
    49  
    50  		if err := cmd.Start(); err != nil {
    51  			fmt.Printf("Failed to start %s with error: %v\n", toolFullPath, err)
    52  			return err
    53  		}
    54  
    55  		if err := cmd.Wait(); err != nil {
    56  			fmt.Printf("Failed to execute %s with exit code: %v\n", toolFullPath, err)
    57  			return err
    58  		}
    59  		fmt.Printf("Starting %s successfully \n", cmd.String())
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  // KillExistBinaries iterates over all binary files and kills their corresponding processes.
    66  func KillExistBinaries() {
    67  	for binary := range serviceBinaries {
    68  		fullPath := GetBinFullPath(binary)
    69  		KillExistBinary(fullPath)
    70  	}
    71  }
    72  
    73  // CheckBinariesStop checks if all binary files have stopped and returns an error if there are any binaries still running.
    74  func CheckBinariesStop() error {
    75  	var runningBinaries []string
    76  
    77  	for binary := range serviceBinaries {
    78  		fullPath := GetBinFullPath(binary)
    79  		if CheckProcessNamesExist(fullPath) {
    80  			runningBinaries = append(runningBinaries, binary)
    81  		}
    82  	}
    83  
    84  	if len(runningBinaries) > 0 {
    85  		return fmt.Errorf("the following binaries are still running: %s", strings.Join(runningBinaries, ", "))
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // CheckBinariesRunning checks if all binary files are running as expected and returns any errors encountered.
    92  func CheckBinariesRunning() error {
    93  	var errorMessages []string
    94  
    95  	for binary, expectedCount := range serviceBinaries {
    96  		fullPath := GetBinFullPath(binary)
    97  		err := CheckProcessNames(fullPath, expectedCount)
    98  		if err != nil {
    99  			errorMessages = append(errorMessages, fmt.Sprintf("binary %s is not running as expected: %v", binary, err))
   100  		}
   101  	}
   102  
   103  	if len(errorMessages) > 0 {
   104  		return fmt.Errorf(strings.Join(errorMessages, "\n"))
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  // PrintListenedPortsByBinaries iterates over all binary files and prints the ports they are listening on.
   111  func PrintListenedPortsByBinaries() {
   112  	for binary, _ := range serviceBinaries {
   113  		basePath := GetBinFullPath(binary)
   114  		fullPath := basePath
   115  		PrintBinaryPorts(fullPath)
   116  	}
   117  }