2019-09-14 07:33:14 +03:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-11-02 16:25:10 +03:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2019-09-14 07:33:14 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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
|
2019-11-02 16:25:10 +03:00
|
|
|
log.Debugf("Runtime starting service %s", service.Name)
|
2019-09-24 20:05:51 +03:00
|
|
|
if err := service.Start(); err != nil {
|
2019-11-02 16:25:10 +03:00
|
|
|
log.Debugf("Runtime error starting service %s: %v", service.Name, err)
|
|
|
|
}
|
|
|
|
case event := <-events:
|
|
|
|
log.Debugf("Runtime received notification event: %v", event)
|
|
|
|
// NOTE: we only handle Update events for now
|
|
|
|
switch event.Type {
|
|
|
|
case Update:
|
|
|
|
// parse returned response to timestamp
|
|
|
|
updateTimeStamp, err := strconv.ParseInt(event.Version, 10, 64)
|
|
|
|
if err != nil {
|
2019-11-08 18:57:07 +03:00
|
|
|
log.Debugf("Runtime error parsing build time for %s: %v", event.Service, err)
|
2019-11-02 16:25:10 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
buildTime := time.Unix(updateTimeStamp, 0)
|
|
|
|
processEvent := func(event Event, service *Service) error {
|
2019-11-14 17:26:21 +03:00
|
|
|
r.RLock()
|
|
|
|
name := service.Name
|
|
|
|
version := service.Version
|
|
|
|
r.RUnlock()
|
|
|
|
|
|
|
|
buildTimeStamp, err := strconv.ParseInt(version, 10, 64)
|
2019-11-02 16:25:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
muBuild := time.Unix(buildTimeStamp, 0)
|
|
|
|
if buildTime.After(muBuild) {
|
2019-11-14 17:26:21 +03:00
|
|
|
log.Debugf("Runtime updating service %s", name)
|
2019-11-02 16:25:10 +03:00
|
|
|
if err := r.Update(service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-14 17:26:21 +03:00
|
|
|
r.Lock()
|
2019-11-02 16:25:10 +03:00
|
|
|
service.Version = fmt.Sprintf("%d", buildTime.Unix())
|
2019-11-14 17:26:21 +03:00
|
|
|
r.Unlock()
|
2019-11-02 16:25:10 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-14 17:26:21 +03:00
|
|
|
|
2019-11-02 16:25:10 +03:00
|
|
|
if len(event.Service) > 0 {
|
2019-11-14 17:26:21 +03:00
|
|
|
r.RLock()
|
2019-11-02 16:25:10 +03:00
|
|
|
service, ok := r.services[event.Service]
|
2019-11-14 17:26:21 +03:00
|
|
|
r.RUnlock()
|
2019-11-02 16:25:10 +03:00
|
|
|
if !ok {
|
|
|
|
log.Debugf("Runtime unknown service: %s", event.Service)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := processEvent(event, service.Service); err != nil {
|
|
|
|
log.Debugf("Runtime error updating service %s: %v", event.Service, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if blank service was received we update all services
|
|
|
|
for _, service := range r.services {
|
|
|
|
if err := processEvent(event, service.Service); err != nil {
|
|
|
|
log.Debugf("Runtime error updating service %s: %v", service.Name, err)
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 20:05:51 +03:00
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
case <-r.closed:
|
|
|
|
log.Debugf("Runtime stopped.")
|
2019-09-24 20:05:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-09-14 07:33:14 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
if _, ok := r.services[s.Name]; ok {
|
|
|
|
return errors.New("service already registered")
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:32:35 +03: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 07:33:14 +03:00
|
|
|
// save service
|
2019-09-24 20:32:35 +03:00
|
|
|
r.services[s.Name] = newService(s, options)
|
2019-09-14 07:33:14 +03:00
|
|
|
|
2019-09-14 18:07:36 +03:00
|
|
|
// push into start queue
|
2019-11-14 17:26:21 +03:00
|
|
|
log.Debugf("Runtime creating service %s", s.Name)
|
2019-09-14 18:07:36 +03:00
|
|
|
r.start <- r.services[s.Name]
|
|
|
|
|
2019-09-14 07:33:14 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
// Get returns all instances of requested service
|
|
|
|
// If no service name is provided we return all the track services.
|
|
|
|
func (r *runtime) Get(name string, opts ...GetOption) ([]*Service, error) {
|
2019-09-14 07:58:03 +03:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, errors.New("missing service name")
|
2019-09-14 07:58:03 +03:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:41:40 +03:00
|
|
|
gopts := GetOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&gopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
var services []*Service
|
|
|
|
// if we track the service check if the version is provided
|
|
|
|
if s, ok := r.services[name]; ok {
|
|
|
|
if len(gopts.Version) > 0 {
|
|
|
|
if s.Version == gopts.Version {
|
|
|
|
services = append(services, s.Service)
|
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
// no version has sbeen requested, just append the service
|
|
|
|
services = append(services, s.Service)
|
|
|
|
}
|
|
|
|
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
|
2019-10-29 15:29:21 +03:00
|
|
|
func (r *runtime) Update(s *Service) error {
|
2019-11-14 17:26:21 +03:00
|
|
|
var opts []CreateOption
|
|
|
|
|
|
|
|
// check if the service already exists
|
|
|
|
r.RLock()
|
|
|
|
if service, ok := r.services[s.Name]; ok {
|
|
|
|
opts = append(opts, WithOutput(service.output))
|
|
|
|
}
|
|
|
|
r.RUnlock()
|
|
|
|
|
2019-10-29 15:29:21 +03:00
|
|
|
// delete the service
|
|
|
|
if err := r.Delete(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new service
|
2019-11-14 17:26:21 +03:00
|
|
|
return r.Create(s, opts...)
|
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
|
|
|
|
func (r *runtime) Delete(s *Service) error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
log.Debugf("Runtime deleting service %s", s.Name)
|
|
|
|
if s, ok := r.services[s.Name]; ok {
|
|
|
|
// check if running
|
|
|
|
if !s.Running() {
|
|
|
|
delete(r.services, s.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// otherwise stop it
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// delete it
|
|
|
|
delete(r.services, s.Name)
|
|
|
|
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) {
|
|
|
|
var services []*Service
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
|
|
|
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
|
|
|
|
if r.options.Notifier != nil {
|
|
|
|
var err error
|
|
|
|
events, err = r.options.Notifier.Notify()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
|
|
|
log.Debugf("Runtime failed to start update notifier")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-09-24 20:05:51 +03:00
|
|
|
log.Debugf("Runtime stopping %s", service.Name)
|
2019-09-14 07:33:14 +03:00
|
|
|
service.Stop()
|
|
|
|
}
|
2019-11-02 16:25:10 +03:00
|
|
|
// stop the notifier too
|
|
|
|
if r.options.Notifier != nil {
|
|
|
|
return r.options.Notifier.Close()
|
|
|
|
}
|
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"
|
|
|
|
}
|