2023-08-18 23:49:33 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2024-04-29 13:11:42 +00:00
|
|
|
"errors"
|
2023-08-18 23:49:33 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-08-23 22:27:14 +00:00
|
|
|
"path/filepath"
|
2023-08-18 23:49:33 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/hpcloud/tail"
|
|
|
|
process "github.com/mudler/go-processmanager"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ml *ModelLoader) deleteProcess(s string) error {
|
2024-10-02 06:55:58 +00:00
|
|
|
defer delete(ml.models, s)
|
|
|
|
|
2024-10-02 18:37:40 +00:00
|
|
|
log.Debug().Msgf("Deleting process %s", s)
|
|
|
|
|
2024-10-02 06:55:58 +00:00
|
|
|
m, exists := ml.models[s]
|
|
|
|
if !exists {
|
2024-10-02 18:37:40 +00:00
|
|
|
log.Error().Msgf("Model does not exist %s", s)
|
2024-10-02 06:55:58 +00:00
|
|
|
// Nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
process := m.Process()
|
|
|
|
if process == nil {
|
2024-10-02 18:37:40 +00:00
|
|
|
log.Error().Msgf("No process for %s", s)
|
2024-10-02 06:55:58 +00:00
|
|
|
// Nothing to do as there is no process
|
|
|
|
return nil
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
2024-10-02 06:55:58 +00:00
|
|
|
|
|
|
|
err := process.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("(deleteProcess) error while deleting process %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 13:11:42 +00:00
|
|
|
func (ml *ModelLoader) StopGRPC(filter GRPCProcessFilter) error {
|
|
|
|
var err error = nil
|
2024-09-26 10:44:55 +00:00
|
|
|
for k, m := range ml.models {
|
|
|
|
if filter(k, m.Process()) {
|
2024-09-17 14:51:40 +00:00
|
|
|
e := ml.ShutdownModel(k)
|
2024-04-29 13:11:42 +00:00
|
|
|
err = errors.Join(err, e)
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-29 13:11:42 +00:00
|
|
|
return err
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 13:11:42 +00:00
|
|
|
func (ml *ModelLoader) StopAllGRPC() error {
|
2024-09-17 14:51:40 +00:00
|
|
|
return ml.StopGRPC(all)
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *ModelLoader) GetGRPCPID(id string) (int, error) {
|
2024-09-17 14:51:40 +00:00
|
|
|
ml.mu.Lock()
|
|
|
|
defer ml.mu.Unlock()
|
2024-09-26 10:44:55 +00:00
|
|
|
p, exists := ml.models[id]
|
2023-08-18 23:49:33 +00:00
|
|
|
if !exists {
|
|
|
|
return -1, fmt.Errorf("no grpc backend found for %s", id)
|
|
|
|
}
|
2024-09-26 10:44:55 +00:00
|
|
|
if p.Process() == nil {
|
|
|
|
return -1, fmt.Errorf("no grpc backend found for %s", id)
|
|
|
|
}
|
|
|
|
return strconv.Atoi(p.Process().PID)
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 10:44:55 +00:00
|
|
|
func (ml *ModelLoader) startProcess(grpcProcess, id string, serverAddress string, args ...string) (*process.Process, error) {
|
2023-08-18 23:49:33 +00:00
|
|
|
// Make sure the process is executable
|
2024-04-25 22:47:06 +00:00
|
|
|
if err := os.Chmod(grpcProcess, 0700); err != nil {
|
2024-09-26 10:44:55 +00:00
|
|
|
return nil, err
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("Loading GRPC Process: %s", grpcProcess)
|
|
|
|
|
|
|
|
log.Debug().Msgf("GRPC Service for %s will be running at: '%s'", id, serverAddress)
|
|
|
|
|
2024-08-23 22:27:14 +00:00
|
|
|
workDir, err := filepath.Abs(filepath.Dir(grpcProcess))
|
|
|
|
if err != nil {
|
2024-09-26 10:44:55 +00:00
|
|
|
return nil, err
|
2024-08-23 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
grpcControlProcess := process.New(
|
|
|
|
process.WithTemporaryStateDir(),
|
2024-08-23 22:27:14 +00:00
|
|
|
process.WithName(filepath.Base(grpcProcess)),
|
2024-06-18 20:43:43 +00:00
|
|
|
process.WithArgs(append(args, []string{"--addr", serverAddress}...)...),
|
2023-08-18 23:49:33 +00:00
|
|
|
process.WithEnvironment(os.Environ()...),
|
2024-08-23 22:27:14 +00:00
|
|
|
process.WithWorkDir(workDir),
|
2023-08-18 23:49:33 +00:00
|
|
|
)
|
|
|
|
|
2023-11-26 17:36:23 +00:00
|
|
|
if ml.wd != nil {
|
|
|
|
ml.wd.Add(serverAddress, grpcControlProcess)
|
|
|
|
ml.wd.AddAddressModelMap(serverAddress, id)
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
if err := grpcControlProcess.Run(); err != nil {
|
2024-09-26 10:44:55 +00:00
|
|
|
return grpcControlProcess, err
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("GRPC Service state dir: %s", grpcControlProcess.StateDir())
|
|
|
|
// clean up process
|
|
|
|
go func() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-c
|
2024-06-24 06:34:36 +00:00
|
|
|
err := grpcControlProcess.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error while shutting down grpc process")
|
|
|
|
}
|
2023-08-18 23:49:33 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
t, err := tail.TailFile(grpcControlProcess.StderrPath(), tail.Config{Follow: true})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("Could not tail stderr")
|
|
|
|
}
|
|
|
|
for line := range t.Lines {
|
|
|
|
log.Debug().Msgf("GRPC(%s): stderr %s", strings.Join([]string{id, serverAddress}, "-"), line.Text)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
t, err := tail.TailFile(grpcControlProcess.StdoutPath(), tail.Config{Follow: true})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("Could not tail stdout")
|
|
|
|
}
|
|
|
|
for line := range t.Lines {
|
|
|
|
log.Debug().Msgf("GRPC(%s): stdout %s", strings.Join([]string{id, serverAddress}, "-"), line.Text)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-09-26 10:44:55 +00:00
|
|
|
return grpcControlProcess, nil
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|