2019-11-02 15:25:10 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2020-04-20 15:54:29 +02:00
|
|
|
"fmt"
|
2019-11-02 15:25:10 +02:00
|
|
|
"io"
|
2020-02-07 14:02:41 +02:00
|
|
|
"strconv"
|
2020-03-13 20:39:59 +02:00
|
|
|
"strings"
|
2019-11-02 15:25:10 +02:00
|
|
|
"sync"
|
2019-11-26 19:33:41 +02:00
|
|
|
"time"
|
2019-11-02 15:25:10 +02:00
|
|
|
|
2022-09-29 16:44:53 +02:00
|
|
|
log "go-micro.dev/v4/logger"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/runtime/local/build"
|
|
|
|
"go-micro.dev/v4/runtime/local/process"
|
|
|
|
proc "go-micro.dev/v4/runtime/local/process/os"
|
2019-11-02 15:25:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
running bool
|
|
|
|
closed chan bool
|
|
|
|
err error
|
2019-11-26 19:33:41 +02:00
|
|
|
updated time.Time
|
2019-11-02 15:25:10 +02:00
|
|
|
|
2020-02-07 14:02:41 +02:00
|
|
|
retries int
|
|
|
|
maxRetries int
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// output for logs
|
|
|
|
output io.Writer
|
|
|
|
|
|
|
|
// service to manage
|
|
|
|
*Service
|
|
|
|
// process creator
|
|
|
|
Process *proc.Process
|
|
|
|
// Exec
|
|
|
|
Exec *process.Executable
|
|
|
|
// process pid
|
|
|
|
PID *process.PID
|
2022-09-29 16:44:53 +02:00
|
|
|
// to be used logger
|
|
|
|
Logger log.Logger
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 16:44:53 +02:00
|
|
|
func newService(s *Service, c CreateOptions, l log.Logger) *service {
|
2019-11-02 15:25:10 +02:00
|
|
|
var exec string
|
|
|
|
var args []string
|
|
|
|
|
2019-11-29 13:35:00 +02:00
|
|
|
// set command
|
2020-03-13 20:39:59 +02:00
|
|
|
exec = strings.Join(c.Command, " ")
|
|
|
|
args = c.Args
|
2019-11-02 15:25:10 +02:00
|
|
|
|
|
|
|
return &service{
|
|
|
|
Service: s,
|
|
|
|
Process: new(proc.Process),
|
|
|
|
Exec: &process.Executable{
|
2019-11-19 18:09:43 +02:00
|
|
|
Package: &build.Package{
|
2019-11-02 15:25:10 +02:00
|
|
|
Name: s.Name,
|
|
|
|
Path: exec,
|
|
|
|
},
|
|
|
|
Env: c.Env,
|
|
|
|
Args: args,
|
2020-04-09 16:44:39 +02:00
|
|
|
Dir: s.Source,
|
2019-11-02 15:25:10 +02:00
|
|
|
},
|
2022-09-29 16:44:53 +02:00
|
|
|
Logger: log.LoggerOrDefault(l),
|
2020-02-07 14:02:41 +02:00
|
|
|
closed: make(chan bool),
|
|
|
|
output: c.Output,
|
|
|
|
updated: time.Now(),
|
|
|
|
maxRetries: c.Retries,
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) streamOutput() {
|
|
|
|
go io.Copy(s.output, s.PID.Output)
|
|
|
|
go io.Copy(s.output, s.PID.Error)
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:55:55 +02:00
|
|
|
func (s *service) shouldStart() bool {
|
2020-02-07 14:02:41 +02:00
|
|
|
if s.running {
|
|
|
|
return false
|
|
|
|
}
|
2020-03-20 00:38:37 +02:00
|
|
|
return s.retries <= s.maxRetries
|
2020-02-07 14:02:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:54:29 +02:00
|
|
|
func (s *service) key() string {
|
|
|
|
return fmt.Sprintf("%v:%v", s.Name, s.Version)
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:55:55 +02:00
|
|
|
func (s *service) ShouldStart() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
return s.shouldStart()
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
func (s *service) Running() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
return s.running
|
|
|
|
}
|
|
|
|
|
2020-06-08 11:47:25 +02:00
|
|
|
// Start starts the service
|
2019-11-02 15:25:10 +02:00
|
|
|
func (s *service) Start() error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2020-02-07 15:55:55 +02:00
|
|
|
if !s.shouldStart() {
|
2019-11-02 15:25:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// reset
|
|
|
|
s.err = nil
|
|
|
|
s.closed = make(chan bool)
|
2020-04-16 17:50:24 +02:00
|
|
|
s.retries = 0
|
2019-11-02 15:25:10 +02:00
|
|
|
|
2020-01-10 23:54:28 +02:00
|
|
|
if s.Metadata == nil {
|
|
|
|
s.Metadata = make(map[string]string)
|
|
|
|
}
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("starting", nil)
|
2020-01-10 23:54:28 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// TODO: pull source & build binary
|
2022-09-29 16:44:53 +02:00
|
|
|
s.Logger.Log(log.DebugLevel, "Runtime service %s forking new process", s.Service.Name)
|
2020-04-16 17:50:24 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
p, err := s.Process.Fork(s.Exec)
|
|
|
|
if err != nil {
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("error", err)
|
2019-11-02 15:25:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set the pid
|
|
|
|
s.PID = p
|
|
|
|
// set to running
|
|
|
|
s.running = true
|
2020-01-10 23:54:28 +02:00
|
|
|
// set status
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("running", nil)
|
2020-03-20 14:48:12 +02:00
|
|
|
// set started
|
|
|
|
s.Metadata["started"] = time.Now().Format(time.RFC3339)
|
2019-11-02 15:25:10 +02:00
|
|
|
|
|
|
|
if s.output != nil {
|
|
|
|
s.streamOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait and watch
|
|
|
|
go s.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-08 11:47:25 +02:00
|
|
|
// Status updates the status of the service. Assumes it's called under a lock as it mutates state
|
|
|
|
func (s *service) Status(status string, err error) {
|
|
|
|
s.Metadata["lastStatusUpdate"] = time.Now().Format(time.RFC3339)
|
|
|
|
s.Metadata["status"] = status
|
|
|
|
if err == nil {
|
|
|
|
delete(s.Metadata, "error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.Metadata["error"] = err.Error()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Stop stops the service
|
|
|
|
func (s *service) Stop() error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
|
|
|
s.running = false
|
2020-04-16 17:50:24 +02:00
|
|
|
s.retries = 0
|
2019-11-02 15:25:10 +02:00
|
|
|
if s.PID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-10 23:54:28 +02:00
|
|
|
|
|
|
|
// set status
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("stopping", nil)
|
2020-01-10 23:54:28 +02:00
|
|
|
|
2019-11-14 16:26:21 +02:00
|
|
|
// kill the process
|
|
|
|
err := s.Process.Kill(s.PID)
|
2020-06-08 11:47:25 +02:00
|
|
|
if err == nil {
|
|
|
|
// wait for it to exit
|
|
|
|
s.Process.Wait(s.PID)
|
2020-04-16 17:50:24 +02:00
|
|
|
}
|
2020-01-10 23:54:28 +02:00
|
|
|
|
|
|
|
// set status
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("stopped", err)
|
2020-01-10 23:54:28 +02:00
|
|
|
|
2019-11-14 16:26:21 +02:00
|
|
|
// return the kill error
|
|
|
|
return err
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the last error service has returned
|
|
|
|
func (s *service) Error() error {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
return s.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for the service to finish running
|
|
|
|
func (s *service) Wait() {
|
|
|
|
// wait for process to exit
|
2020-06-10 10:36:41 +02:00
|
|
|
s.RLock()
|
|
|
|
thisPID := s.PID
|
|
|
|
s.RUnlock()
|
|
|
|
err := s.Process.Wait(thisPID)
|
2019-11-02 15:25:10 +02:00
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2020-06-10 10:36:41 +02:00
|
|
|
if s.PID.ID != thisPID.ID {
|
|
|
|
// trying to update when it's already been switched out, ignore
|
2022-09-29 16:44:53 +02:00
|
|
|
s.Logger.Logf(log.WarnLevel, "Trying to update a process status but PID doesn't match. Old %s, New %s. Skipping update.", thisPID.ID, s.PID.ID)
|
2020-06-10 10:36:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// save the error
|
|
|
|
if err != nil {
|
2022-09-29 16:44:53 +02:00
|
|
|
s.Logger.Logf(log.ErrorLevel, "Service %s terminated with error %s", s.Name, err)
|
2020-02-07 14:02:41 +02:00
|
|
|
s.retries++
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("error", err)
|
2020-02-07 14:02:41 +02:00
|
|
|
s.Metadata["retries"] = strconv.Itoa(s.retries)
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
s.err = err
|
2020-02-03 17:56:16 +02:00
|
|
|
} else {
|
2020-06-08 11:47:25 +02:00
|
|
|
s.Status("done", nil)
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// no longer running
|
|
|
|
s.running = false
|
|
|
|
}
|