Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
ensure we will use the same objects and generate only once during run
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rad committed Dec 11, 2015
1 parent 5394a4e commit fc76b25
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 18 deletions.
23 changes: 18 additions & 5 deletions work/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io/ioutil"
"os"
"strings"
"sync"
"time"
)

Expand All @@ -37,6 +38,7 @@ type Env struct {
log logrus.Entry
attributes map[string]interface{}
config Config
services map[string]*env.Service
}

func NewEnvironment(root string, name string) *Env {
Expand All @@ -48,10 +50,11 @@ func NewEnvironment(root string, name string) *Env {
}

env := &Env{
path: path,
name: name,
log: log,
config: Config{},
services: map[string]*env.Service{},
path: path,
name: name,
log: log,
config: Config{},
}
env.loadAttributes()
env.loadConfig()
Expand All @@ -71,7 +74,17 @@ func (e Env) GetAttributes() map[string]interface{} {
}

func (e Env) LoadService(name string) *env.Service {
return env.NewService(e.path+"/services", name, e)
var mutex = &sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()

if val, ok := e.services[name]; ok {
return val
}

service := env.NewService(e.path+"/services", name, e)
e.services[name] = service
return service
}

func (e Env) attributesDir() string {
Expand Down
24 changes: 22 additions & 2 deletions work/env/service-generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ import (
"io/ioutil"
"net/http"
"strings"
"sync"
)

func (s Service) Generate() {
func (s *Service) Generate() {
var mutex = &sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()

if s.generated {
return
}

s.log.Debug("Generating units")

serviceTmpl, err := s.loadUnitTemplate(spec.PATH_UNIT_SERVICE_TEMPLATE)
Expand Down Expand Up @@ -44,6 +53,7 @@ func (s Service) Generate() {
unit.Log.WithField("type", unit.GetType()).Fatal("Unknown unit type")
}
}
s.generated = true
}

func (s Service) NodeAttributes(hostname string) map[string]interface{} {
Expand Down Expand Up @@ -164,11 +174,20 @@ func (s Service) discoverPod(name cntspec.ACFullname) []cntspec.ACFullname {
}
}

func (s Service) PrepareAciList() string {
func (s *Service) PrepareAciList() string {

if len(s.manifest.Containers) == 0 {
return ""
}

var mutex = &sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()

if s.aciList != "" {
return s.aciList
}

override := s.sources(builder.BuildFlags.GenerateManifests)
s.log.WithField("data", override).Debug("Local resolved sources")

Expand Down Expand Up @@ -208,5 +227,6 @@ func (s Service) PrepareAciList() string {
if acis == "" {
s.log.Error("Aci list is empty after discovery")
}
s.aciList = acis
return acis
}
21 changes: 19 additions & 2 deletions work/env/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io/ioutil"
"os"
"strings"
"sync"
"time"
)

Expand All @@ -29,6 +30,9 @@ type Service struct {
log log.Entry
lockPath string
attributes map[string]interface{}
generated bool
units map[string]*service.Unit
aciList string
}

func NewService(path string, name string, env spec.Env) *Service {
Expand All @@ -40,6 +44,7 @@ func NewService(path string, name string, env spec.Env) *Service {
}

service := &Service{
units: map[string]*service.Unit{},
hasTimer: hasTimer,
log: *l.WithField("service", name),
path: path + "/" + name,
Expand All @@ -48,6 +53,8 @@ func NewService(path string, name string, env spec.Env) *Service {
lockPath: "/ggn-lock/" + name + "/lock",
}

service.log.Debug("New Service")

service.loadManifest()
service.loadAttributes()
service.prepareNodesAsJsonMap()
Expand Down Expand Up @@ -102,10 +109,20 @@ func (s *Service) GetLog() logrus.Entry {
}

func (s *Service) LoadUnit(name string) *service.Unit {
var mutex = &sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()

if val, ok := s.units[name]; ok {
return val
}
var unit *service.Unit
if strings.HasSuffix(name, spec.TYPE_TIMER.String()) {
return service.NewUnit(s.path+"/units", name[:len(name)-len(spec.TYPE_TIMER.String())], spec.TYPE_TIMER, s)
unit = service.NewUnit(s.path+"/units", name[:len(name)-len(spec.TYPE_TIMER.String())], spec.TYPE_TIMER, s)
}
return service.NewUnit(s.path+"/units", name, spec.TYPE_SERVICE, s)
unit = service.NewUnit(s.path+"/units", name, spec.TYPE_SERVICE, s)
s.units[name] = unit
return unit
}

func (s *Service) Diff() {
Expand Down
13 changes: 12 additions & 1 deletion work/env/service/unit-generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import (
"os"
"strconv"
"strings"
"sync"
)

func (u Unit) Generate(tmpl *utils.Templating) {
func (u *Unit) Generate(tmpl *utils.Templating) {
var mutex = &sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()

if u.generated {
return
}

u.Log.Debug("Generate")
data := u.GenerateAttributes()
data["acis"] = u.Service.PrepareAciList()
Expand All @@ -37,6 +46,8 @@ func (u Unit) Generate(tmpl *utils.Templating) {
if err != nil {
u.Log.WithError(err).WithField("path", u.path+"/"+u.Filename).Error("Cannot writer unit")
}

u.generated = true
}

func (u Unit) GenerateAttributes() map[string]interface{} {
Expand Down
19 changes: 11 additions & 8 deletions work/env/service/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
)

type Unit struct {
Log logrus.Entry
Type spec.UnitType
path string
Name string
hostname string
Filename string
unitPath string
Service spec.Service
Log logrus.Entry
Type spec.UnitType
path string
Name string
hostname string
Filename string
unitPath string
Service spec.Service
generated bool
}

func NewUnit(path string, hostname string, utype spec.UnitType, service spec.Service) *Unit {
Expand All @@ -44,6 +45,8 @@ func NewUnit(path string, hostname string, utype spec.UnitType, service spec.Ser
Filename: filename,
unitPath: path + "/" + filename,
}
unit.Log.Debug("New unit")

return unit
}

Expand Down

0 comments on commit fc76b25

Please sign in to comment.