github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/svcctl/svcmsg/heart.go (about)

     1  // Package svcmsg models the Heartbeat data that the executor must communicate
     2  // to the service.
     3  //
     4  // IMPORTANT: This package should have minimal dependencies as it will be
     5  // imported by cmd/state-exec. The resulting compiled executable must remain as
     6  // small as possible.
     7  package svcmsg
     8  
     9  import (
    10  	"fmt"
    11  	"strings"
    12  )
    13  
    14  type Heartbeat struct {
    15  	ProcessID string
    16  	ExecPath  string
    17  }
    18  
    19  func NewHeartbeatFromSvcMsg(data string) *Heartbeat {
    20  	var pid, execPath string
    21  
    22  	ss := strings.SplitN(data, "<", 2)
    23  	if len(ss) > 0 {
    24  		pid = ss[0]
    25  	}
    26  	if len(ss) > 1 {
    27  		execPath = ss[1]
    28  	}
    29  
    30  	return NewHeartbeat(pid, execPath)
    31  }
    32  
    33  func NewHeartbeat(pid, execPath string) *Heartbeat {
    34  	return &Heartbeat{
    35  		ProcessID: pid,
    36  		ExecPath:  execPath,
    37  	}
    38  }
    39  
    40  func (h *Heartbeat) SvcMsg() string {
    41  	return fmt.Sprintf("heart<%s<%s", h.ProcessID, h.ExecPath)
    42  }