2019-09-14 07:33:14 +03:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-04-01 16:40:15 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-09-14 07:33:14 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-04-01 16:40:15 +03:00
|
|
|
"github.com/hpcloud/tail"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-04-23 17:30:43 +03:00
|
|
|
"github.com/micro/go-micro/v2/runtime/local/git"
|
2019-09-14 07:33:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type runtime struct {
|
|
|
|
sync.RWMutex
|
2019-11-02 16:25:10 +03:00
|
|
|
// options configure runtime
|
|
|
|
options Options
|
2019-09-14 18:07:36 +03: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-11-15 16:41:40 +03:00
|
|
|
// TODO: track different versions of the same service
|
2019-09-14 07:33:14 +03:00
|
|
|
services map[string]*service
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// NewRuntime creates new local runtime and returns it
|
|
|
|
func NewRuntime(opts ...Option) Runtime {
|
|
|
|
// get default options
|
|
|
|
options := Options{}
|
2019-09-14 07:33:14 +03:00
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-09-14 07:33:14 +03:00
|
|
|
|
2020-04-11 13:22:02 +03:00
|
|
|
// make the logs directory
|
|
|
|
path := filepath.Join(os.TempDir(), "micro", "logs")
|
|
|
|
_ = os.MkdirAll(path, 0755)
|
|
|
|
|
2019-09-14 07:33:14 +03:00
|
|
|
return &runtime{
|
2019-11-02 16:25:10 +03:00
|
|
|
options: options,
|
2019-09-14 07:33:14 +03:00
|
|
|
closed: make(chan bool),
|
2019-09-14 18:07:36 +03:00
|
|
|
start: make(chan *service, 128),
|
2019-09-14 07:33:14 +03:00
|
|
|
services: make(map[string]*service),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 17:30:43 +03:00
|
|
|
// @todo move this to runtime default
|
|
|
|
func (r *runtime) checkoutSourceIfNeeded(s *Service) error {
|
|
|
|
source, err := git.ParseSourceLocal("", s.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
source.Ref = s.Version
|
|
|
|
err = git.CheckoutSource(os.TempDir(), source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Source = source.FullPath
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Init initializes runtime options
|
|
|
|
func (r *runtime) Init(opts ...Option) error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2019-09-14 07:33:14 +03:00
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.options)
|
2019-09-14 07:33:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// run runs the runtime management loop
|
|
|
|
func (r *runtime) run(events <-chan Event) {
|
2019-09-24 20:05:51 +03:00
|
|
|
t := time.NewTicker(time.Second * 5)
|
|
|
|
defer t.Stop()
|
|
|
|
|
2019-11-26 20:33:41 +03:00
|
|
|
// process event processes an incoming event
|
|
|
|
processEvent := func(event Event, service *service) error {
|
|
|
|
// get current vals
|
|
|
|
r.RLock()
|
|
|
|
name := service.Name
|
|
|
|
updated := service.updated
|
|
|
|
r.RUnlock()
|
|
|
|
|
|
|
|
// only process if the timestamp is newer
|
|
|
|
if !event.Timestamp.After(updated) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime updating service %s", name)
|
|
|
|
}
|
2019-11-26 20:33:41 +03:00
|
|
|
|
|
|
|
// this will cause a delete followed by created
|
|
|
|
if err := r.Update(service.Service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the local timestamp
|
|
|
|
r.Lock()
|
|
|
|
service.updated = updated
|
|
|
|
r.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:05:51 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
// check running services
|
|
|
|
r.RLock()
|
|
|
|
for _, service := range r.services {
|
2020-02-07 15:02:41 +03:00
|
|
|
if !service.ShouldStart() {
|
2019-09-24 20:05:51 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check service error
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime starting %s", service.Name)
|
|
|
|
}
|
2019-09-24 20:05:51 +03:00
|
|
|
if err := service.Start(); err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error starting %s: %v", service.Name, err)
|
|
|
|
}
|
2019-09-24 20:05:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r.RUnlock()
|
|
|
|
case service := <-r.start:
|
2020-02-07 15:02:41 +03:00
|
|
|
if !service.ShouldStart() {
|
2019-09-24 20:05:51 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO: check service error
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime starting service %s", service.Name)
|
|
|
|
}
|
2019-09-24 20:05:51 +03:00
|
|
|
if err := service.Start(); err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error starting service %s: %v", service.Name, err)
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
case event := <-events:
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime received notification event: %v", event)
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
// NOTE: we only handle Update events for now
|
|
|
|
switch event.Type {
|
|
|
|
case Update:
|
|
|
|
if len(event.Service) > 0 {
|
2019-11-14 17:26:21 +03:00
|
|
|
r.RLock()
|
2020-04-20 16:54:29 +03:00
|
|
|
service, ok := r.services[fmt.Sprintf("%v:%v", event.Service, event.Version)]
|
2019-11-14 17:26:21 +03:00
|
|
|
r.RUnlock()
|
2019-11-02 16:25:10 +03:00
|
|
|
if !ok {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime unknown service: %s", event.Service)
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-11-26 20:33:41 +03:00
|
|
|
if err := processEvent(event, service); err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error updating service %s: %v", event.Service, err)
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-11-26 20:33:41 +03:00
|
|
|
|
|
|
|
r.RLock()
|
|
|
|
services := r.services
|
|
|
|
r.RUnlock()
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// if blank service was received we update all services
|
2019-11-26 20:33:41 +03:00
|
|
|
for _, service := range services {
|
|
|
|
if err := processEvent(event, service); err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error updating service %s: %v", service.Name, err)
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 20:05:51 +03:00
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
case <-r.closed:
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime stopped")
|
|
|
|
}
|
2019-09-24 20:05:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 16:40:15 +03:00
|
|
|
func logFile(serviceName string) string {
|
2020-04-11 13:22:02 +03:00
|
|
|
// make the directory
|
2020-04-01 16:40:15 +03:00
|
|
|
name := strings.Replace(serviceName, "/", "-", -1)
|
2020-04-11 13:22:02 +03:00
|
|
|
path := filepath.Join(os.TempDir(), "micro", "logs")
|
|
|
|
return filepath.Join(path, fmt.Sprintf("%v.log", name))
|
2020-04-01 16:40:15 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 16:54:29 +03:00
|
|
|
func serviceKey(s *Service) string {
|
|
|
|
return fmt.Sprintf("%v:%v", s.Name, s.Version)
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Create creates a new service which is then started by runtime
|
2019-09-24 20:32:35 +03:00
|
|
|
func (r *runtime) Create(s *Service, opts ...CreateOption) error {
|
2020-04-23 17:30:43 +03:00
|
|
|
r.checkoutSourceIfNeeded(s)
|
2019-09-14 07:33:14 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2020-04-20 16:54:29 +03:00
|
|
|
if _, ok := r.services[serviceKey(s)]; ok {
|
2020-01-17 17:14:47 +03:00
|
|
|
return errors.New("service already running")
|
2019-09-14 07:33:14 +03:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:32:35 +03:00
|
|
|
var options CreateOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-29 14:35:00 +03:00
|
|
|
if len(options.Command) == 0 {
|
2020-03-20 01:38:37 +03:00
|
|
|
options.Command = []string{"go"}
|
|
|
|
options.Args = []string{"run", "."}
|
2019-09-24 20:32:35 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:51:30 +03:00
|
|
|
// create new service
|
|
|
|
service := newService(s, options)
|
|
|
|
|
2020-04-01 16:40:15 +03:00
|
|
|
f, err := os.OpenFile(logFile(service.Name), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.output != nil {
|
|
|
|
service.output = io.MultiWriter(service.output, f)
|
|
|
|
} else {
|
|
|
|
service.output = f
|
|
|
|
}
|
2019-12-24 20:51:30 +03:00
|
|
|
// start the service
|
|
|
|
if err := service.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-14 07:33:14 +03:00
|
|
|
|
2019-12-24 20:51:30 +03:00
|
|
|
// save service
|
2020-04-20 16:54:29 +03:00
|
|
|
r.services[serviceKey(s)] = service
|
2019-09-14 18:07:36 +03:00
|
|
|
|
2019-09-14 07:33:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-01 16:40:15 +03:00
|
|
|
// @todo: Getting existing lines is not supported yet.
|
|
|
|
// The reason for this is because it's hard to calculate line offset
|
|
|
|
// as opposed to character offset.
|
|
|
|
// This logger streams by default and only supports the `StreamCount` option.
|
|
|
|
func (r *runtime) Logs(s *Service, options ...LogsOption) (LogStream, error) {
|
|
|
|
lopts := LogsOptions{}
|
|
|
|
for _, o := range options {
|
|
|
|
o(&lopts)
|
|
|
|
}
|
|
|
|
ret := &logStream{
|
|
|
|
service: s.Name,
|
|
|
|
stream: make(chan LogRecord),
|
|
|
|
stop: make(chan bool),
|
|
|
|
}
|
|
|
|
t, err := tail.TailFile(logFile(s.Name), tail.Config{Follow: true, Location: &tail.SeekInfo{
|
|
|
|
Whence: 2,
|
|
|
|
Offset: 0,
|
|
|
|
}, Logger: tail.DiscardingLogger})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.tail = t
|
|
|
|
go func() {
|
|
|
|
for line := range t.Lines {
|
|
|
|
ret.stream <- LogRecord{Message: line.Text}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type logStream struct {
|
|
|
|
tail *tail.Tail
|
|
|
|
service string
|
|
|
|
stream chan LogRecord
|
2020-04-02 14:16:35 +03:00
|
|
|
sync.Mutex
|
|
|
|
stop chan bool
|
|
|
|
err error
|
2020-04-01 16:40:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logStream) Chan() chan LogRecord {
|
|
|
|
return l.stream
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:03:26 +03:00
|
|
|
func (l *logStream) Error() error {
|
|
|
|
return l.err
|
|
|
|
}
|
|
|
|
|
2020-04-01 16:40:15 +03:00
|
|
|
func (l *logStream) Stop() error {
|
2020-04-02 14:16:35 +03:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2020-04-01 16:40:15 +03:00
|
|
|
// @todo seems like this is causing a hangup
|
|
|
|
//err := l.tail.Stop()
|
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
|
|
|
select {
|
|
|
|
case <-l.stop:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(l.stop)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:31:14 +03:00
|
|
|
// Read returns all instances of requested service
|
2019-11-15 16:41:40 +03:00
|
|
|
// If no service name is provided we return all the track services.
|
2019-11-29 14:35:00 +03:00
|
|
|
func (r *runtime) Read(opts ...ReadOption) ([]*Service, error) {
|
2019-09-14 07:58:03 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-11-25 19:31:14 +03:00
|
|
|
gopts := ReadOptions{}
|
2019-11-15 16:41:40 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&gopts)
|
|
|
|
}
|
|
|
|
|
2019-11-29 14:35:00 +03:00
|
|
|
save := func(k, v string) bool {
|
|
|
|
if len(k) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return k == v
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:59:44 +03:00
|
|
|
//nolint:prealloc
|
2019-11-15 16:41:40 +03:00
|
|
|
var services []*Service
|
2019-11-29 14:35:00 +03:00
|
|
|
|
|
|
|
for _, service := range r.services {
|
|
|
|
if !save(gopts.Service, service.Name) {
|
|
|
|
continue
|
2019-11-15 16:41:40 +03:00
|
|
|
}
|
2019-11-29 14:35:00 +03:00
|
|
|
if !save(gopts.Version, service.Version) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO deal with service type
|
2019-11-15 16:41:40 +03:00
|
|
|
// no version has sbeen requested, just append the service
|
2019-11-29 14:35:00 +03:00
|
|
|
services = append(services, service.Service)
|
2019-11-15 16:41:40 +03:00
|
|
|
}
|
2019-11-29 14:35:00 +03:00
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
return services, nil
|
2019-09-14 07:58:03 +03:00
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Update attemps to update the service
|
2020-04-23 15:53:42 +03:00
|
|
|
func (r *runtime) Update(s *Service, opts ...UpdateOption) error {
|
2020-04-23 17:30:43 +03:00
|
|
|
r.checkoutSourceIfNeeded(s)
|
2020-04-16 18:50:24 +03:00
|
|
|
r.Lock()
|
2020-04-20 16:54:29 +03:00
|
|
|
service, ok := r.services[serviceKey(s)]
|
2020-04-16 18:50:24 +03:00
|
|
|
r.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Service not found")
|
2019-11-14 17:26:21 +03:00
|
|
|
}
|
2020-04-16 18:50:24 +03:00
|
|
|
err := service.Stop()
|
|
|
|
if err != nil {
|
2019-10-29 15:29:21 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-04-16 18:50:24 +03:00
|
|
|
return service.Start()
|
2019-10-29 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
// Delete removes the service from the runtime and stops it
|
2020-04-23 15:53:42 +03:00
|
|
|
func (r *runtime) Delete(s *Service, opts ...DeleteOption) error {
|
2019-11-15 16:41:40 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime deleting service %s", s.Name)
|
|
|
|
}
|
2020-04-20 16:54:29 +03:00
|
|
|
if s, ok := r.services[serviceKey(s)]; ok {
|
2019-11-15 16:41:40 +03:00
|
|
|
// check if running
|
2020-02-07 15:02:41 +03:00
|
|
|
if s.Running() {
|
2020-04-20 16:54:29 +03:00
|
|
|
delete(r.services, s.key())
|
2019-11-15 16:41:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// otherwise stop it
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// delete it
|
2020-04-20 16:54:29 +03:00
|
|
|
delete(r.services, s.key())
|
2019-11-15 16:41:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// List returns a slice of all services tracked by the runtime
|
2019-10-29 15:29:21 +03:00
|
|
|
func (r *runtime) List() ([]*Service, error) {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2019-12-03 22:59:44 +03:00
|
|
|
services := make([]*Service, 0, len(r.services))
|
|
|
|
|
2019-10-29 15:29:21 +03:00
|
|
|
for _, service := range r.services {
|
|
|
|
services = append(services, service.Service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Start starts the runtime
|
2019-09-14 07:58:03 +03:00
|
|
|
func (r *runtime) Start() error {
|
2019-09-14 07:33:14 +03:00
|
|
|
r.Lock()
|
2019-09-24 20:05:51 +03:00
|
|
|
defer r.Unlock()
|
2019-09-14 07:33:14 +03:00
|
|
|
|
|
|
|
// already running
|
|
|
|
if r.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// set running
|
|
|
|
r.running = true
|
|
|
|
r.closed = make(chan bool)
|
2019-09-14 18:07:36 +03:00
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
var events <-chan Event
|
2020-01-16 16:34:04 +03:00
|
|
|
if r.options.Scheduler != nil {
|
2019-11-02 16:25:10 +03:00
|
|
|
var err error
|
2020-01-16 16:34:04 +03:00
|
|
|
events, err = r.options.Scheduler.Notify()
|
2019-11-02 16:25:10 +03:00
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime failed to start update notifier")
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go r.run(events)
|
2019-09-14 07:33:14 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
// Stop stops the runtime
|
2019-09-14 07:33:14 +03:00
|
|
|
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 {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime stopping %s", service.Name)
|
|
|
|
}
|
2019-09-14 07:33:14 +03:00
|
|
|
service.Stop()
|
|
|
|
}
|
2020-01-16 16:34:04 +03:00
|
|
|
// stop the scheduler
|
|
|
|
if r.options.Scheduler != nil {
|
|
|
|
return r.options.Scheduler.Close()
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
2019-09-14 07:33:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
|
|
|
|
// String implements stringer interface
|
|
|
|
func (r *runtime) String() string {
|
|
|
|
return "local"
|
|
|
|
}
|