1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-30 08:06:40 +02:00
go-micro/runtime/default.go

325 lines
5.1 KiB
Go
Raw Normal View History

2019-09-14 06:33:14 +02:00
package runtime
import (
"errors"
2019-09-24 20:00:11 +02:00
"io"
2019-09-14 06:33:14 +02:00
"strings"
"sync"
"time"
"github.com/micro/go-micro/runtime/package"
"github.com/micro/go-micro/runtime/process"
proc "github.com/micro/go-micro/runtime/process/os"
"github.com/micro/go-micro/util/log"
)
type runtime struct {
sync.RWMutex
2019-09-14 17:07:36 +02:00
// used to stop the runtime
closed chan bool
// used to start new services
start chan *service
// indicates if we're running
running bool
// the service map
2019-09-14 06:33:14 +02:00
services map[string]*service
}
type service struct {
sync.RWMutex
running bool
closed chan bool
err error
2019-09-24 20:00:11 +02:00
// output for logs
output io.Writer
2019-09-14 06:33:14 +02:00
// service to manage
*Service
// process creator
Process *proc.Process
// Exec
Exec *process.Executable
// process pid
PID *process.PID
}
func newRuntime() *runtime {
return &runtime{
closed: make(chan bool),
2019-09-14 17:07:36 +02:00
start: make(chan *service, 128),
2019-09-14 06:33:14 +02:00
services: make(map[string]*service),
}
}
2019-09-24 19:32:35 +02:00
func newService(s *Service, c CreateOptions) *service {
var exec string
var args []string
2019-09-14 06:33:14 +02:00
2019-09-24 19:32:35 +02:00
if len(s.Exec) > 0 {
parts := strings.Split(s.Exec, " ")
exec = parts[0]
args = []string{}
if len(parts) > 1 {
args = parts[1:]
}
} else {
// set command
exec = c.Command[0]
// set args
if len(c.Command) > 1 {
args = c.Command[1:]
}
2019-09-14 06:33:14 +02:00
}
return &service{
Service: s,
Process: new(proc.Process),
Exec: &process.Executable{
Binary: &packager.Binary{
Name: s.Name,
Path: exec,
},
2019-09-24 19:32:35 +02:00
Env: c.Env,
2019-09-14 06:33:14 +02:00
Args: args,
},
closed: make(chan bool),
2019-09-24 20:00:11 +02:00
output: c.Output,
2019-09-14 06:33:14 +02:00
}
}
2019-09-24 20:00:11 +02:00
func (s *service) streamOutput() {
go io.Copy(s.output, s.PID.Output)
go io.Copy(s.output, s.PID.Error)
}
2019-09-14 06:33:14 +02:00
func (s *service) Running() bool {
s.RLock()
defer s.RUnlock()
return s.running
}
func (s *service) Start() error {
s.Lock()
defer s.Unlock()
if s.running {
return nil
}
// reset
s.err = nil
s.closed = make(chan bool)
// TODO: pull source & build binary
2019-11-01 00:30:21 +02:00
log.Debugf("Runtime service %s forking new process\n", s.Service.Name)
2019-09-14 06:33:14 +02:00
p, err := s.Process.Fork(s.Exec)
if err != nil {
return err
}
// set the pid
s.PID = p
// set to running
s.running = true
2019-09-24 20:00:11 +02:00
if s.output != nil {
s.streamOutput()
}
2019-09-14 06:33:14 +02:00
// wait and watch
go s.Wait()
return nil
}
func (s *service) Stop() error {
s.Lock()
defer s.Unlock()
select {
case <-s.closed:
return nil
default:
close(s.closed)
s.running = false
2019-11-01 00:30:21 +02:00
if s.PID == nil {
return nil
}
2019-09-14 06:33:14 +02:00
return s.Process.Kill(s.PID)
}
return nil
}
func (s *service) Error() error {
s.RLock()
defer s.RUnlock()
return s.err
}
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 {
s.err = err
}
// no longer running
s.running = false
}
2019-09-24 19:05:51 +02:00
func (r *runtime) run() {
r.RLock()
closed := r.closed
r.RUnlock()
t := time.NewTicker(time.Second * 5)
defer t.Stop()
for {
select {
case <-t.C:
// check running services
r.RLock()
for _, service := range r.services {
if service.Running() {
continue
}
// TODO: check service error
log.Debugf("Runtime starting %s", service.Name)
if err := service.Start(); err != nil {
log.Debugf("Runtime error starting %s: %v", service.Name, err)
}
}
r.RUnlock()
case service := <-r.start:
if service.Running() {
continue
}
// TODO: check service error
log.Debugf("Starting %s", service.Name)
if err := service.Start(); err != nil {
log.Debugf("Runtime error starting %s: %v", service.Name, err)
}
case <-closed:
// TODO: stop all the things
return
}
}
}
2019-09-24 19:32:35 +02:00
func (r *runtime) Create(s *Service, opts ...CreateOption) error {
2019-09-14 06:33:14 +02:00
r.Lock()
defer r.Unlock()
if _, ok := r.services[s.Name]; ok {
return errors.New("service already registered")
}
2019-09-24 19:32:35 +02:00
var options CreateOptions
for _, o := range opts {
o(&options)
}
if len(s.Exec) == 0 && len(options.Command) == 0 {
return errors.New("missing exec command")
}
2019-09-14 06:33:14 +02:00
// save service
2019-09-24 19:32:35 +02:00
r.services[s.Name] = newService(s, options)
2019-09-14 06:33:14 +02:00
2019-09-14 17:07:36 +02:00
// push into start queue
r.start <- r.services[s.Name]
2019-09-14 06:33:14 +02:00
return nil
}
func (r *runtime) Delete(s *Service) error {
r.Lock()
defer r.Unlock()
if s, ok := r.services[s.Name]; ok {
delete(r.services, s.Name)
return s.Stop()
}
return nil
}
2019-10-29 14:29:21 +02:00
func (r *runtime) Update(s *Service) error {
// delete the service
if err := r.Delete(s); err != nil {
return err
}
// create new service
return r.Create(s)
}
func (r *runtime) List() ([]*Service, error) {
var services []*Service
r.RLock()
defer r.RUnlock()
for _, service := range r.services {
services = append(services, service.Service)
}
return services, nil
}
func (r *runtime) Start() error {
2019-09-14 06:33:14 +02:00
r.Lock()
2019-09-24 19:05:51 +02:00
defer r.Unlock()
2019-09-14 06:33:14 +02:00
// already running
if r.running {
return nil
}
// set running
r.running = true
r.closed = make(chan bool)
2019-09-14 17:07:36 +02:00
2019-09-24 19:05:51 +02:00
go r.run()
2019-09-14 06:33:14 +02:00
return nil
}
func (r *runtime) Stop() error {
r.Lock()
defer r.Unlock()
if !r.running {
return nil
}
select {
case <-r.closed:
return nil
default:
close(r.closed)
// set not running
r.running = false
// stop all the services
for _, service := range r.services {
2019-09-24 19:05:51 +02:00
log.Debugf("Runtime stopping %s", service.Name)
2019-09-14 06:33:14 +02:00
service.Stop()
}
}
return nil
}