github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/supervisor/daemon/myst.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package daemon
    19  
    20  import (
    21  	"encoding/json"
    22  	"errors"
    23  	"fmt"
    24  	"net/http"
    25  	"os"
    26  	"strconv"
    27  	"time"
    28  
    29  	"github.com/rs/zerolog/log"
    30  )
    31  
    32  const (
    33  	tequilapiHost = "http://localhost"
    34  	defaultPort   = 4050
    35  )
    36  
    37  func (d *Daemon) killMyst() error {
    38  	log.Info().Msg("Trying to stop node process gracefully")
    39  	err := gracefulStop(d.tequilapiURL(), 3*time.Second)
    40  	if err == nil {
    41  		return nil
    42  	}
    43  
    44  	log.Warn().Msgf("Failed to stop node gracefully, will continue with force kill: %v", err)
    45  	pid, err := mystPid(d.tequilapiURL())
    46  	if err != nil {
    47  		return fmt.Errorf("could not get myst pid: %w", err)
    48  	}
    49  	if err := forceKill(pid); err != nil {
    50  		return fmt.Errorf("could not force kill node: %w", err)
    51  	}
    52  	return nil
    53  }
    54  
    55  func (d *Daemon) setTequilapiPort(cmd []string) error {
    56  	if len(cmd) < 2 {
    57  		return fmt.Errorf("expected 2 arguments")
    58  	}
    59  	port, err := strconv.ParseUint(cmd[1], 10, 16)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	log.Info().Msgf("Changing tequilapi port to: %d", port)
    64  	d.tequilapiPort = uint16(port)
    65  	return nil
    66  }
    67  
    68  func (d *Daemon) tequilapiURL() string {
    69  	return fmt.Sprintf("%s:%d", tequilapiHost, d.tequilapiPort)
    70  }
    71  
    72  func gracefulStop(tequilapiURL string, timeout time.Duration) error {
    73  	client := http.Client{Timeout: timeout}
    74  	resp, err := client.Post(fmt.Sprintf("%s/stop", tequilapiURL), "application/json", nil)
    75  	if err != nil {
    76  		return fmt.Errorf("could not call stop: %w", err)
    77  	}
    78  	defer resp.Body.Close()
    79  	if resp.StatusCode != http.StatusAccepted {
    80  		return fmt.Errorf("expected status %d, got %d", http.StatusAccepted, resp.StatusCode)
    81  	}
    82  
    83  	timeoutCh := time.After(timeout)
    84  	for {
    85  		select {
    86  		case <-timeoutCh:
    87  			return errors.New("timeout waiting for myst to exit")
    88  		default:
    89  			_, err := mystPid(tequilapiURL)
    90  			if err != nil {
    91  				return nil
    92  			}
    93  			time.Sleep(500 * time.Millisecond)
    94  		}
    95  	}
    96  }
    97  
    98  func forceKill(pid int) error {
    99  	proc, err := os.FindProcess(pid)
   100  	if err != nil {
   101  		return fmt.Errorf("could not find process %d: %w", pid, err)
   102  	}
   103  	return kill(proc)
   104  }
   105  
   106  func kill(proc *os.Process) error {
   107  	err := proc.Kill()
   108  	if err != nil {
   109  		return fmt.Errorf("could not kill process %d: %w", proc.Pid, err)
   110  	}
   111  	state, err := proc.Wait()
   112  	if err == nil && !state.Exited() {
   113  		return fmt.Errorf("process left in running state: %d: %w", proc.Pid, err)
   114  	}
   115  	return nil
   116  }
   117  
   118  func mystPid(tequilapiURL string) (int, error) {
   119  	client := http.Client{
   120  		Timeout: 3 * time.Second,
   121  	}
   122  	resp, err := client.Get(fmt.Sprintf("%s/healthcheck", tequilapiURL))
   123  	if err != nil {
   124  		return 0, fmt.Errorf("could not call healthcheck: %w", err)
   125  	}
   126  	defer resp.Body.Close()
   127  	if resp.StatusCode != http.StatusOK {
   128  		return 0, fmt.Errorf("expected status %d, got %d", http.StatusOK, resp.StatusCode)
   129  	}
   130  
   131  	type healthcheck struct {
   132  		Process int `json:"process"`
   133  	}
   134  	hz := healthcheck{}
   135  	if err := json.NewDecoder(resp.Body).Decode(&hz); err != nil {
   136  		return 0, fmt.Errorf("could not parse health check JSON: %w", err)
   137  	}
   138  	return hz.Process, nil
   139  }