From d991f1abb5fa949922bf3a344d709519d761f7f0 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 11 Feb 2015 20:57:25 +0700 Subject: [PATCH] App --- app.go | 31 ------------------------------- chain/bank.go | 2 ++ core/app.go | 30 ++++++++++++++++++++++++++++++ core/config.go | 19 ++++++------------- core/supervisor.go | 8 +++++--- yeast.go | 22 ++++++++++++++++++++++ 6 files changed, 65 insertions(+), 47 deletions(-) delete mode 100644 app.go create mode 100644 core/app.go create mode 100644 yeast.go diff --git a/app.go b/app.go deleted file mode 100644 index 9406fb0..0000000 --- a/app.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "flag" - - "github.com/kr/pretty" - "github.com/localhots/confection" - "github.com/localhots/yeast/chain" - "github.com/localhots/yeast/core" - "github.com/localhots/yeast/unit" -) - -func init() { - confection.SetupFlags() - flag.Parse() -} - -func main() { - core.InitConfig() - - ub := unit.NewBank(core.Conf().UnitsConfig) - ub.Reload() - - cb := chain.NewBank(core.Conf().ChainsConfig, ub) - cb.Reload() - - pretty.Println(core.Conf()) - - println("Waiting") - select {} -} diff --git a/chain/bank.go b/chain/bank.go index 52834c6..adad146 100644 --- a/chain/bank.go +++ b/chain/bank.go @@ -32,6 +32,8 @@ func (b *Bank) Chain(name string) *Chain { } func (b *Bank) Reload() { + b.units.Reload() + f, err := os.Open(b.config) if err != nil { panic("Failed to open chains config: " + b.config) diff --git a/core/app.go b/core/app.go new file mode 100644 index 0000000..bbddaa6 --- /dev/null +++ b/core/app.go @@ -0,0 +1,30 @@ +package core + +import ( + "github.com/localhots/yeast/chain" + "github.com/localhots/yeast/unit" +) + +type ( + App struct { + config *Config + chains *chain.Bank + } +) + +func NewApp() *App { + a := &App{ + config: &Config{}, + } + a.config.Init() + + ub := unit.NewBank(a.Conf().UnitsConfig) + a.chains = chain.NewBank(a.Conf().ChainsConfig, ub) + a.chains.Reload() + + return a +} + +func (a *App) Conf() Config { + return a.config.conf.Config().(Config) +} diff --git a/core/config.go b/core/config.go index c2b9743..19cda47 100644 --- a/core/config.go +++ b/core/config.go @@ -8,6 +8,7 @@ import ( type ( Config struct { + conf *confection.Manager ChainsConfig string `json:"chains_config_path" attrs:"required" title:"Chains config path"` UnitsConfig string `json:"units_config_path" attrs:"required" title:"Units config path"` Python Python `json:"python" title:"Python"` @@ -18,21 +19,13 @@ type ( } ) -var ( - conf *confection.Manager -) - -func Conf() Config { - return conf.Config().(Config) +func (c *Config) Init() { + c.conf = confection.New(*c, c.decoder) + go c.conf.StartServer() + c.conf.RequireConfig() } -func InitConfig() { - conf = confection.New(Config{}, ConfigDecoder) - go conf.StartServer() - conf.RequireConfig() -} - -func ConfigDecoder(b []byte) interface{} { +func (c *Config) decoder(b []byte) interface{} { var newConf Config if err := json.Unmarshal(b, &newConf); err != nil { panic(err) diff --git a/core/supervisor.go b/core/supervisor.go index 034915e..ae02271 100644 --- a/core/supervisor.go +++ b/core/supervisor.go @@ -8,7 +8,10 @@ import ( ) type ( - Supervisor struct{} + Supervisor struct { + pythonBin string + pythonWrapper string + } ) // XXX: We're about to spawn hundreds of Python processes @@ -21,8 +24,7 @@ func (s *Supervisor) StartAll(units []string) { func (s *Supervisor) Start(name string) { fmt.Println("Starting unit: " + name) - conf := Conf().Python - cmd := exec.Command(conf.BinPath, conf.WrapperPath, name) + cmd := exec.Command(s.pythonBin, s.pythonWrapper, name) cmd.Stdout = os.Stdout // Sorry if err := cmd.Start(); err != nil { fmt.Println("Failed to start unit: ", name) diff --git a/yeast.go b/yeast.go new file mode 100644 index 0000000..65c7d0d --- /dev/null +++ b/yeast.go @@ -0,0 +1,22 @@ +package main + +import ( + "flag" + + "github.com/kr/pretty" + "github.com/localhots/confection" + "github.com/localhots/yeast/core" +) + +func init() { + confection.SetupFlags() + flag.Parse() +} + +func main() { + app := core.NewApp() + pretty.Println(app.Conf()) + + println("Waiting") + select {} +}