Browse Source

En cours de développement

master
mls-361 3 months ago
parent
commit
f90fb3e690
  1. 3
      Taskfile.yml
  2. 9
      cmd/main.go
  3. 3
      go.mod
  4. 2
      go.sum
  5. 52
      internal/config/config.go
  6. 16
      internal/config/data.go
  7. 15
      internal/config/load.go
  8. 21
      internal/engine/engine.go
  9. 18
      internal/supervisor/logger.go
  10. 75
      internal/supervisor/supervisor.go

3
Taskfile.yml

@ -14,6 +14,9 @@ tasks:
cmds:
- go get -u forge.chapril.org/mls-361/application
- go get -u forge.chapril.org/mls-361/crypto
- go get -u forge.chapril.org/mls-361/failure
- go get -u forge.chapril.org/mls-361/logger
- go get -u forge.chapril.org/mls-361/uuid
- go mod tidy
lint:
cmds:

9
cmd/main.go

@ -10,8 +10,6 @@ import (
"os"
"forge.chapril.org/armen/armen/internal/cmdline"
"forge.chapril.org/armen/armen/internal/config"
"forge.chapril.org/armen/armen/internal/engine"
"forge.chapril.org/armen/armen/internal/supervisor"
"forge.chapril.org/mls-361/application"
)
@ -28,12 +26,7 @@ func appRun() error {
return app.OnError(err)
}
config := config.New(app)
engine := engine.New(app, config)
supervisor := supervisor.New(config, engine)
return app.OnError(supervisor.Run())
return app.OnError(supervisor.New(app).Run())
}
func main() {

3
go.mod

@ -5,4 +5,7 @@ go 1.15
require (
forge.chapril.org/mls-361/application v0.0.0-20210414132623-3cc12ee64b10
forge.chapril.org/mls-361/crypto v0.0.0-20210412210122-fd3f4aac66eb
forge.chapril.org/mls-361/failure v0.0.0-20210412202827-a8d100489f9d
forge.chapril.org/mls-361/logger v0.0.0-20210412202904-dadda1a823de
forge.chapril.org/mls-361/uuid v0.0.0-20210411123640-49d47af267ff
)

2
go.sum

@ -10,5 +10,7 @@ forge.chapril.org/mls-361/fqdn v0.0.0-20210412202921-28941d70bb76 h1:P5+SUAuD9pu
forge.chapril.org/mls-361/fqdn v0.0.0-20210412202921-28941d70bb76/go.mod h1:Z0AMpAr8UFXBp6Qkl66KKlw/eb2tbliAMAWPRgC/5t4=
forge.chapril.org/mls-361/kvfmt v0.0.0-20210411202715-0e7c3e922d37 h1:6e1Y7hTJ0B8BYStiZSCdW4+v686rOpstQh2B5SWOhZU=
forge.chapril.org/mls-361/kvfmt v0.0.0-20210411202715-0e7c3e922d37/go.mod h1:bv44R0CAd8lQV4ub1hjLE3kWYwpfsW4Ro9zRnD4YOMU=
forge.chapril.org/mls-361/logger v0.0.0-20210412202904-dadda1a823de h1:6SpigDheCwoqrjwxzFLt6pSlqdKYiALt5xRkVwGFpC0=
forge.chapril.org/mls-361/logger v0.0.0-20210412202904-dadda1a823de/go.mod h1:kQ2uGfvLdRl47OxE/GI8LThroYe6RnodiqfRUbJMst0=
forge.chapril.org/mls-361/uuid v0.0.0-20210411123640-49d47af267ff h1:N7L3nN/9Wlck9qtIxH6gbQ9sbWtsZ048+eDEdpvD09g=
forge.chapril.org/mls-361/uuid v0.0.0-20210411123640-49d47af267ff/go.mod h1:OAYM+f6ErfC4jbruuG2crA5W7YSpr7+wEs2MrxIzR3Q=

52
internal/config/config.go

@ -6,11 +6,19 @@
package config
import "forge.chapril.org/mls-361/application"
import (
"sync"
"forge.chapril.org/mls-361/application"
)
// Config AFAIRE.
type Config struct {
app *application.Application
app *application.Application
sm sync.Mutex
available bool
config *Data
version string
}
// New AFAIRE.
@ -20,6 +28,46 @@ func New(app *application.Application) *Config {
}
}
// Get() AFAIRE.
func (c *Config) Get() *Data {
defer func() {
c.sm.Lock()
c.available = false
c.sm.Unlock()
}()
return c.config
}
func (c *Config) notYetUsed() bool {
c.sm.Lock()
defer c.sm.Unlock()
return c.available
}
// HasChanged AFAIRE.
func (c *Config) HasChanged() (string, error) {
if c.notYetUsed() {
return "", nil
}
config, err := c.load()
if err != nil {
return "", err
}
if config.Version == c.version {
return "", nil
}
c.config = config
c.version = config.Version
c.available = true
return config.Version, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

16
internal/config/data.go

@ -0,0 +1,16 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
// Data AFAIRE.
type Data struct {
Version string
}
/*
######################################################################################################## @(°_°)@ #######
*/

15
internal/config/load.go

@ -0,0 +1,15 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
func (c *Config) load() (*Data, error) {
return &Data{Version: "0"}, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

21
internal/engine/engine.go

@ -11,6 +11,8 @@ import (
"forge.chapril.org/armen/armen/internal/config"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/mls-361/uuid"
)
// Engine AFAIRE.
@ -20,6 +22,7 @@ type Engine struct {
stopCh chan struct{}
endCh chan error
swg sync.WaitGroup
logger logger.Logger
}
// New AFAIRE.
@ -32,6 +35,9 @@ func New(app *application.Application, cfg *config.Config) *Engine {
}
}
// ConfigHasChanged AFAIRE.
func (e *Engine) ConfigHasChanged() {}
// EndCh AFAIRE.
func (e *Engine) EndCh() <-chan error {
return e.endCh
@ -43,8 +49,6 @@ func (e *Engine) Wait() {
}
func (e *Engine) loop() {
defer e.swg.Done()
loop:
for {
select {
@ -56,10 +60,19 @@ loop:
e.endCh <- nil
}
func (e *Engine) main(logger logger.Logger) {
defer e.swg.Done()
defer logger.Remove()
e.logger = logger
e.loop()
}
// Start AFAIRE.
func (e *Engine) Start() {
func (e *Engine) Start(logger logger.Logger) {
e.swg.Add(1)
go e.loop() //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
go e.main(logger.NewLogger(uuid.New(), "engine")) //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
}
// Stop AFAIRE.

18
internal/supervisor/logger.go

@ -0,0 +1,18 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### supervisor ####### Copyright (c) 2021 mls-361 ############################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package supervisor
import "forge.chapril.org/mls-361/logger"
func (s *Supervisor) createLogger() (logger.Logger, error) {
app := s.app
return logger.New(app.ID(), app.Name(), "trace", logger.NewStderrOutput())
}
/*
######################################################################################################## @(°_°)@ #######
*/

75
internal/supervisor/supervisor.go

@ -10,23 +10,58 @@ import (
"os"
"os/signal"
"syscall"
"time"
"forge.chapril.org/armen/armen/internal/config"
"forge.chapril.org/armen/armen/internal/engine"
"forge.chapril.org/mls-361/application"
"forge.chapril.org/mls-361/failure"
"forge.chapril.org/mls-361/logger"
)
// Supervisor AFAIRE.
type Supervisor struct {
config *config.Config
engine *engine.Engine
app *application.Application
cfg *config.Config
ege *engine.Engine
logger logger.Logger
}
// New AFAIRE.
func New(c *config.Config, e *engine.Engine) *Supervisor {
func New(app *application.Application) *Supervisor {
cfg := config.New(app)
ege := engine.New(app, cfg)
return &Supervisor{
config: c,
engine: e,
app: app,
cfg: cfg,
ege: ege,
}
}
func (s *Supervisor) supervise() time.Duration {
cfgVersion, err := s.cfg.HasChanged()
if err != nil {
d := 10 * time.Minute
s.logger.Error( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"impossible to load the configuration",
"reason", err.Error(),
"retry", time.Now().Add(d).String(),
)
return d
}
if cfgVersion == "" {
return 1 * time.Minute
}
s.logger.Notice("The configuration has changed", "version", cfgVersion) //::::::::::::::::::::::::::::::::::::::::::
s.ege.ConfigHasChanged()
return 5 * time.Minute
}
func (s *Supervisor) loop() error {
@ -38,25 +73,45 @@ func (s *Supervisor) loop() error {
end := false
timer := time.NewTimer(0)
for {
select {
case err := <-s.engine.EndCh():
s.engine.Wait()
return err
case <-timer.C:
timer.Reset(s.supervise())
case <-endCh:
if end {
break
}
s.engine.Stop()
if !timer.Stop() {
<-timer.C
}
end = true
s.ege.Stop()
case err := <-s.ege.EndCh():
s.ege.Wait()
return err
}
}
}
// Run AFAIRE.
func (s *Supervisor) Run() error {
s.engine.Start()
logger, err := s.createLogger()
if err != nil {
return failure.Wrap(err, "impossible to create the logger") ////////////////////////////////////////////////////
}
defer logger.Remove()
s.logger = logger
s.ege.Start(logger)
return s.loop()
}

Loading…
Cancel
Save