2019-11-02 16:25:10 +03:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2020-02-07 15:02:41 +03:00
|
|
|
"strconv"
|
2019-11-02 16:25:10 +03:00
|
|
|
"sync"
|
2019-11-26 20:33:41 +03:00
|
|
|
"time"
|
2019-11-02 16:25:10 +03:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/runtime/local/build"
|
|
|
|
"github.com/micro/go-micro/v2/runtime/local/process"
|
|
|
|
proc "github.com/micro/go-micro/v2/runtime/local/process/os"
|
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2019-11-02 16:25:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
running bool
|
|
|
|
closed chan bool
|
|
|
|
err error
|
2019-11-26 20:33:41 +03:00
|
|
|
updated time.Time
|
2019-11-02 16:25:10 +03:00
|
|
|
|
2020-02-07 15:02:41 +03:00
|
|
|
retries int
|
|
|
|
maxRetries int
|
|
|
|
|
2019-11-02 16:25:10 +03: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func newService(s *Service, c CreateOptions) *service {
|
|
|
|
var exec string
|
|
|
|
var args []string
|
|
|
|
|
2019-11-29 14:35:00 +03:00
|
|
|
// set command
|
|
|
|
exec = c.Command[0]
|
|
|
|
// set args
|
|
|
|
if len(c.Command) > 1 {
|
|
|
|
args = c.Command[1:]
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &service{
|
|
|
|
Service: s,
|
|
|
|
Process: new(proc.Process),
|
|
|
|
Exec: &process.Executable{
|
2019-11-19 19:09:43 +03:00
|
|
|
Package: &build.Package{
|
2019-11-02 16:25:10 +03:00
|
|
|
Name: s.Name,
|
|
|
|
Path: exec,
|
|
|
|
},
|
|
|
|
Env: c.Env,
|
|
|
|
Args: args,
|
|
|
|
},
|
2020-02-07 15:02:41 +03:00
|
|
|
closed: make(chan bool),
|
|
|
|
output: c.Output,
|
|
|
|
updated: time.Now(),
|
|
|
|
maxRetries: c.Retries,
|
2019-11-02 16:25:10 +03: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:02:41 +03:00
|
|
|
func (s *service) ShouldStart() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
if s.running {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.maxRetries < s.retries
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
func (s *service) Running() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
return s.running
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start stars the service
|
|
|
|
func (s *service) Start() error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2020-02-07 15:02:41 +03:00
|
|
|
if !s.ShouldStart() {
|
2019-11-02 16:25:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset
|
|
|
|
s.err = nil
|
|
|
|
s.closed = make(chan bool)
|
|
|
|
|
2020-01-11 00:54:28 +03:00
|
|
|
if s.Metadata == nil {
|
|
|
|
s.Metadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Metadata["status"] = "starting"
|
|
|
|
// delete any existing error
|
|
|
|
delete(s.Metadata, "error")
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// TODO: pull source & build binary
|
|
|
|
log.Debugf("Runtime service %s forking new process", s.Service.Name)
|
|
|
|
p, err := s.Process.Fork(s.Exec)
|
|
|
|
if err != nil {
|
2020-01-11 00:54:28 +03:00
|
|
|
s.Metadata["status"] = "error"
|
|
|
|
s.Metadata["error"] = err.Error()
|
2019-11-02 16:25:10 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the pid
|
|
|
|
s.PID = p
|
|
|
|
// set to running
|
|
|
|
s.running = true
|
2020-01-11 00:54:28 +03:00
|
|
|
// set status
|
|
|
|
s.Metadata["status"] = "running"
|
2019-11-02 16:25:10 +03:00
|
|
|
|
|
|
|
if s.output != nil {
|
|
|
|
s.streamOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait and watch
|
|
|
|
go s.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if s.PID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-11 00:54:28 +03:00
|
|
|
|
|
|
|
// set status
|
|
|
|
s.Metadata["status"] = "stopping"
|
|
|
|
|
2019-11-14 17:26:21 +03:00
|
|
|
// kill the process
|
|
|
|
err := s.Process.Kill(s.PID)
|
|
|
|
// wait for it to exit
|
|
|
|
s.Process.Wait(s.PID)
|
2020-01-11 00:54:28 +03:00
|
|
|
|
|
|
|
// set status
|
|
|
|
s.Metadata["status"] = "stopped"
|
|
|
|
|
2019-11-14 17:26:21 +03:00
|
|
|
// return the kill error
|
|
|
|
return err
|
2019-11-02 16:25:10 +03: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
|
|
|
|
err := s.Process.Wait(s.PID)
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
// save the error
|
|
|
|
if err != nil {
|
2020-02-07 15:02:41 +03:00
|
|
|
s.retries++
|
2020-01-11 00:54:28 +03:00
|
|
|
s.Metadata["status"] = "error"
|
|
|
|
s.Metadata["error"] = err.Error()
|
2020-02-07 15:02:41 +03:00
|
|
|
s.Metadata["retries"] = strconv.Itoa(s.retries)
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
s.err = err
|
2020-02-03 18:56:16 +03:00
|
|
|
} else {
|
|
|
|
s.Metadata["status"] = "done"
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// no longer running
|
|
|
|
s.running = false
|
|
|
|
}
|