Enabling default runtime to run multiple versions (#1545)

* Enabling default runtime to run multiple versions

* Trigger build

* Fix

* Sprintf
This commit is contained in:
Janos Dobronszki 2020-04-20 15:54:29 +02:00 committed by GitHub
parent c4acf3c2cb
commit 7c31edd5f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -142,7 +142,7 @@ func (r *runtime) run(events <-chan Event) {
case Update: case Update:
if len(event.Service) > 0 { if len(event.Service) > 0 {
r.RLock() r.RLock()
service, ok := r.services[event.Service] service, ok := r.services[fmt.Sprintf("%v:%v", event.Service, event.Version)]
r.RUnlock() r.RUnlock()
if !ok { if !ok {
if logger.V(logger.DebugLevel, logger.DefaultLogger) { if logger.V(logger.DebugLevel, logger.DefaultLogger) {
@ -187,12 +187,16 @@ func logFile(serviceName string) string {
return filepath.Join(path, fmt.Sprintf("%v.log", name)) return filepath.Join(path, fmt.Sprintf("%v.log", name))
} }
func serviceKey(s *Service) string {
return fmt.Sprintf("%v:%v", s.Name, s.Version)
}
// Create creates a new service which is then started by runtime // Create creates a new service which is then started by runtime
func (r *runtime) Create(s *Service, opts ...CreateOption) error { func (r *runtime) Create(s *Service, opts ...CreateOption) error {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
if _, ok := r.services[s.Name]; ok { if _, ok := r.services[serviceKey(s)]; ok {
return errors.New("service already running") return errors.New("service already running")
} }
@ -225,7 +229,7 @@ func (r *runtime) Create(s *Service, opts ...CreateOption) error {
} }
// save service // save service
r.services[s.Name] = service r.services[serviceKey(s)] = service
return nil return nil
} }
@ -333,7 +337,7 @@ func (r *runtime) Read(opts ...ReadOption) ([]*Service, error) {
// Update attemps to update the service // Update attemps to update the service
func (r *runtime) Update(s *Service) error { func (r *runtime) Update(s *Service) error {
r.Lock() r.Lock()
service, ok := r.services[s.Name] service, ok := r.services[serviceKey(s)]
r.Unlock() r.Unlock()
if !ok { if !ok {
return errors.New("Service not found") return errors.New("Service not found")
@ -353,10 +357,10 @@ func (r *runtime) Delete(s *Service) error {
if logger.V(logger.DebugLevel, logger.DefaultLogger) { if logger.V(logger.DebugLevel, logger.DefaultLogger) {
logger.Debugf("Runtime deleting service %s", s.Name) logger.Debugf("Runtime deleting service %s", s.Name)
} }
if s, ok := r.services[s.Name]; ok { if s, ok := r.services[serviceKey(s)]; ok {
// check if running // check if running
if s.Running() { if s.Running() {
delete(r.services, s.Name) delete(r.services, s.key())
return nil return nil
} }
// otherwise stop it // otherwise stop it
@ -364,7 +368,7 @@ func (r *runtime) Delete(s *Service) error {
return err return err
} }
// delete it // delete it
delete(r.services, s.Name) delete(r.services, s.key())
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package runtime package runtime
import ( import (
"fmt"
"io" "io"
"strconv" "strconv"
"strings" "strings"
@ -76,6 +77,10 @@ func (s *service) shouldStart() bool {
return s.retries <= s.maxRetries return s.retries <= s.maxRetries
} }
func (s *service) key() string {
return fmt.Sprintf("%v:%v", s.Name, s.Version)
}
func (s *service) ShouldStart() bool { func (s *service) ShouldStart() bool {
s.RLock() s.RLock()
defer s.RUnlock() defer s.RUnlock()