2019-11-20 14:54:42 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/runtime"
|
|
|
|
pb "github.com/micro/go-micro/v2/runtime/service/proto"
|
2020-04-01 15:40:15 +02:00
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2019-11-20 14:54:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type svc struct {
|
|
|
|
sync.RWMutex
|
|
|
|
options runtime.Options
|
|
|
|
runtime pb.RuntimeService
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes runtime with given options
|
|
|
|
func (s *svc) Init(opts ...runtime.Option) error {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create registers a service in the runtime
|
|
|
|
func (s *svc) Create(svc *runtime.Service, opts ...runtime.CreateOption) error {
|
|
|
|
options := runtime.CreateOptions{}
|
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:47:47 +00:00
|
|
|
// set the default source from MICRO_RUNTIME_SOURCE
|
|
|
|
if len(svc.Source) == 0 {
|
|
|
|
svc.Source = s.options.Source
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:54:42 +00:00
|
|
|
// runtime service create request
|
|
|
|
req := &pb.CreateRequest{
|
|
|
|
Service: &pb.Service{
|
2019-11-22 17:10:00 +00:00
|
|
|
Name: svc.Name,
|
|
|
|
Version: svc.Version,
|
|
|
|
Source: svc.Source,
|
|
|
|
Metadata: svc.Metadata,
|
2019-11-20 14:54:42 +00:00
|
|
|
},
|
|
|
|
Options: &pb.CreateOptions{
|
|
|
|
Command: options.Command,
|
2020-03-13 18:39:59 +00:00
|
|
|
Args: options.Args,
|
2019-11-20 14:54:42 +00:00
|
|
|
Env: options.Env,
|
2020-03-13 18:39:59 +00:00
|
|
|
Type: options.Type,
|
|
|
|
Image: options.Image,
|
2019-11-20 14:54:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.runtime.Create(context.Background(), req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
func (s *svc) Logs(service *runtime.Service, options ...runtime.LogsOption) (runtime.LogStream, error) {
|
|
|
|
ls, err := s.runtime.Logs(context.Background(), &pb.LogsRequest{
|
|
|
|
Service: service.Name,
|
|
|
|
Stream: true,
|
|
|
|
Count: 10, // @todo pass in actual options
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logStream := &serviceLogStream{
|
|
|
|
service: service.Name,
|
|
|
|
stream: make(chan runtime.LogRecord),
|
|
|
|
stop: make(chan bool),
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
record := runtime.LogRecord{}
|
|
|
|
err := ls.RecvMsg(&record)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
logStream.stream <- record
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return logStream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceLogStream struct {
|
|
|
|
service string
|
|
|
|
stream chan runtime.LogRecord
|
2020-04-02 13:16:35 +02:00
|
|
|
sync.Mutex
|
|
|
|
stop chan bool
|
|
|
|
err error
|
2020-04-02 00:03:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *serviceLogStream) Error() error {
|
|
|
|
return l.err
|
2020-04-01 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *serviceLogStream) Chan() chan runtime.LogRecord {
|
|
|
|
return l.stream
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *serviceLogStream) Stop() error {
|
2020-04-02 13:16:35 +02:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2020-04-01 15:40:15 +02:00
|
|
|
select {
|
|
|
|
case <-l.stop:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(l.stop)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
// Read returns the service with the given name from the runtime
|
2019-11-29 11:35:00 +00:00
|
|
|
func (s *svc) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) {
|
2019-11-25 16:31:14 +00:00
|
|
|
options := runtime.ReadOptions{}
|
2019-11-20 14:54:42 +00:00
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtime service create request
|
2019-11-25 16:31:14 +00:00
|
|
|
req := &pb.ReadRequest{
|
|
|
|
Options: &pb.ReadOptions{
|
2019-11-29 11:35:00 +00:00
|
|
|
Service: options.Service,
|
2019-11-20 14:54:42 +00:00
|
|
|
Version: options.Version,
|
2019-11-29 11:35:00 +00:00
|
|
|
Type: options.Type,
|
2019-11-20 14:54:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
resp, err := s.runtime.Read(context.Background(), req)
|
2019-11-20 14:54:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
services := make([]*runtime.Service, 0, len(resp.Services))
|
|
|
|
for _, service := range resp.Services {
|
|
|
|
svc := &runtime.Service{
|
2019-11-22 17:10:00 +00:00
|
|
|
Name: service.Name,
|
|
|
|
Version: service.Version,
|
|
|
|
Source: service.Source,
|
|
|
|
Metadata: service.Metadata,
|
2019-11-20 14:54:42 +00:00
|
|
|
}
|
|
|
|
services = append(services, svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the running service
|
|
|
|
func (s *svc) Update(svc *runtime.Service) error {
|
|
|
|
// runtime service create request
|
|
|
|
req := &pb.UpdateRequest{
|
|
|
|
Service: &pb.Service{
|
2020-03-27 11:37:12 +00:00
|
|
|
Name: svc.Name,
|
|
|
|
Version: svc.Version,
|
|
|
|
Source: svc.Source,
|
|
|
|
Metadata: svc.Metadata,
|
2019-11-20 14:54:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.runtime.Update(context.Background(), req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete stops and removes the service from the runtime
|
|
|
|
func (s *svc) Delete(svc *runtime.Service) error {
|
|
|
|
// runtime service create request
|
|
|
|
req := &pb.DeleteRequest{
|
|
|
|
Service: &pb.Service{
|
2020-03-27 11:37:12 +00:00
|
|
|
Name: svc.Name,
|
|
|
|
Version: svc.Version,
|
|
|
|
Source: svc.Source,
|
|
|
|
Metadata: svc.Metadata,
|
2019-11-20 14:54:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.runtime.Delete(context.Background(), req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lists all services managed by the runtime
|
|
|
|
func (s *svc) List() ([]*runtime.Service, error) {
|
|
|
|
// list all services managed by the runtime
|
|
|
|
resp, err := s.runtime.List(context.Background(), &pb.ListRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
services := make([]*runtime.Service, 0, len(resp.Services))
|
|
|
|
for _, service := range resp.Services {
|
|
|
|
svc := &runtime.Service{
|
2019-11-22 17:10:00 +00:00
|
|
|
Name: service.Name,
|
|
|
|
Version: service.Version,
|
|
|
|
Source: service.Source,
|
|
|
|
Metadata: service.Metadata,
|
2019-11-20 14:54:42 +00:00
|
|
|
}
|
|
|
|
services = append(services, svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the runtime
|
|
|
|
func (s *svc) Start() error {
|
|
|
|
// NOTE: nothing to be done here
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the runtime
|
|
|
|
func (s *svc) Stop() error {
|
|
|
|
// NOTE: nothing to be done here
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the runtime service implementation
|
|
|
|
func (s *svc) String() string {
|
|
|
|
return "service"
|
|
|
|
}
|
2020-01-19 00:55:01 +00:00
|
|
|
|
|
|
|
// NewRuntime creates new service runtime and returns it
|
|
|
|
func NewRuntime(opts ...runtime.Option) runtime.Runtime {
|
|
|
|
// get default options
|
|
|
|
options := runtime.Options{}
|
|
|
|
|
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create default client
|
|
|
|
cli := client.DefaultClient
|
|
|
|
|
|
|
|
return &svc{
|
|
|
|
options: options,
|
|
|
|
runtime: pb.NewRuntimeService(runtime.DefaultName, cli),
|
|
|
|
}
|
|
|
|
}
|