Browse Source

En cours de développement

master
mls-361 8 months ago
parent
commit
90c5a3ab16
  1. 68
      internal/config/config.go
  2. 41
      internal/config/data.go
  3. 25
      internal/config/engine.go
  4. 33
      internal/config/logger.go
  5. 8
      internal/config/mongodb.go
  6. 19
      internal/config/postgresql.go
  7. 0
      internal/engine/engine.go.afac
  8. 102
      internal/supervisor/supervisor.go

68
internal/config/config.go

@ -7,65 +7,67 @@
package config
import (
"sync"
"encoding/json"
"io/ioutil"
"forge.chapril.org/mls-361/application"
)
const (
_sourceMongoDB = "mongodb"
_sourcePostgreSQL = "postgresql"
)
// Config AFAIRE.
type Config struct {
app *application.Application
sm sync.Mutex
available bool
config *Data
version string
app *application.Application
data *Data
source string
}
// New AFAIRE.
func New(app *application.Application) *Config {
return &Config{
app: app,
app: app,
data: new(Data),
}
}
// Get() AFAIRE.
func (c *Config) Get() *Data {
defer func() {
c.sm.Lock()
c.available = false
c.sm.Unlock()
}()
// LoadFromSource AFAIRE.
func (c *Config) LoadFromSource() error {
switch c.source {
case _sourceMongoDB:
case _sourcePostgreSQL:
}
return c.config
return nil
}
func (c *Config) notYetUsed() bool {
c.sm.Lock()
defer c.sm.Unlock()
// Load AFAIRE.
func (c *Config) Load() (bool, error) {
file, ok := c.app.LookupEnv("CONFIG")
if !ok {
c.data.Logger = defaultLogger()
c.data.Engine = defaultEngine()
return c.available
}
// HasChanged AFAIRE.
func (c *Config) HasChanged() (string, error) {
if c.notYetUsed() {
return "", nil
return true, nil
}
config, err := c.load()
buf, err := ioutil.ReadFile(file)
if err != nil {
return "", err
return false, err
}
if config.Version == c.version {
return "", nil
if err := json.Unmarshal(buf, c.data); err != nil {
return false, err
}
c.config = config
c.version = config.Version
c.available = true
c.source, err = c.data.validate()
if err != nil {
return false, err
}
return config.Version, nil
return c.source == "", nil
}
/*

41
internal/config/data.go

@ -8,7 +8,46 @@ package config
// Data AFAIRE.
type Data struct {
Version string
Logger *Logger `json:"logger"`
MongoDB *MongoDB `json:"mongodb"`
PostgreSQL *PostgreSQL `json:"postgresql"`
Engine *Engine `json:"engine"`
}
func (d *Data) validate() (string, error) {
if d.Logger == nil {
d.Logger = defaultLogger()
} else if err := d.Logger.validate(); err != nil {
return "", err
}
if d.Engine != nil {
if err := d.Engine.validate(); err != nil {
return "", err
}
return "", nil
}
if d.MongoDB != nil {
if err := d.MongoDB.validate(); err != nil {
return "", err
}
return _sourceMongoDB, nil
}
if d.PostgreSQL != nil {
if err := d.PostgreSQL.validate(); err != nil {
return "", err
}
return _sourcePostgreSQL, nil
}
d.Engine = defaultEngine()
return "", nil
}
/*

25
internal/config/engine.go

@ -0,0 +1,25 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
// Engine AFAIRE.
type Engine struct {
Version string `json:"version"`
UseAfter string `json:"use_after"`
}
func defaultEngine() *Engine {
return &Engine{}
}
func (e *Engine) validate() error {
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

33
internal/config/logger.go

@ -0,0 +1,33 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
const (
_defaultLoggerLevel = "trace"
_defaultLoggerOutput = "stderr"
)
// Logger AFAIRE.
type Logger struct {
Level string `json:"level"`
Output string `json:"output"`
}
func defaultLogger() *Logger {
return &Logger{
Level: _defaultLoggerLevel,
Output: _defaultLoggerOutput,
}
}
func (l *Logger) validate() error {
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

8
internal/config/load.go → internal/config/mongodb.go

@ -6,8 +6,12 @@
package config
func (c *Config) load() (*Data, error) {
return &Data{Version: "0"}, nil
// MongoDB AFAIRE.
type MongoDB struct {
}
func (m *MongoDB) validate() error {
return nil
}
/*

19
internal/config/postgresql.go

@ -0,0 +1,19 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### config ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package config
// PostgreSQL AFAIRE.
type PostgreSQL struct {
}
func (p *PostgreSQL) validate() error {
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

0
internal/engine/engine.go → internal/engine/engine.go.afac

102
internal/supervisor/supervisor.go

@ -8,126 +8,90 @@ package supervisor
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"
"forge.chapril.org/armen/armen/internal/config"
)
// Supervisor AFAIRE.
type Supervisor struct {
app *application.Application
config *config.Config
engine *engine.Engine
logger logger.Logger
}
// New AFAIRE.
func New(app *application.Application) *Supervisor {
config := config.New(app)
engine := engine.New(app, config)
return &Supervisor{
app: app,
config: config,
engine: engine,
config: config.New(app),
}
}
func (s *Supervisor) supervise() time.Duration {
cfgVersion, err := s.config.HasChanged()
func (s *Supervisor) loadConfig() error {
complete, err := s.config.Load()
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
return failure.Wrap(err, "unable to load configuration") ///////////////////////////////////////////////////////
}
if cfgVersion == "" {
return 1 * time.Minute
if complete {
return nil
}
s.logger.Notice("The configuration has changed", "version", cfgVersion) //::::::::::::::::::::::::::::::::::::::::::
s.engine.ConfigHasChanged()
if err := s.config.LoadFromSource(); err != nil {
return failure.Wrap(err, "impossible to load the entire configuration") ////////////////////////////////////////
}
return 5 * time.Minute
return nil
}
func (s *Supervisor) loop() error {
endCh := make(chan os.Signal, 1)
defer close(endCh)
signal.Notify(endCh, os.Interrupt, syscall.SIGABRT, syscall.SIGQUIT, syscall.SIGTERM)
end := false
timer := time.NewTimer(0)
for {
select {
case <-timer.C:
timer.Reset(s.supervise())
case <-endCh:
if end {
break
}
if !timer.Stop() {
<-timer.C
}
s.engine.Stop()
end = true
func (s *Supervisor) createComponents() error {
return nil
}
case err := <-s.engine.EndCh():
s.engine.Wait()
return err
}
}
func (s *Supervisor) loop() {
}
// Run AFAIRE.
func (s *Supervisor) Run() error {
if err := s.loadConfig(); err != nil {
return failure.Wrap(err, "unable to load configuration") ///////////////////////////////////////////////////////
}
logger, err := s.createLogger()
if err != nil {
return failure.Wrap(err, "impossible to create the logger") ////////////////////////////////////////////////////
}
defer logger.Remove()
s.logger = logger
app := s.app
defer func() {
logger.Info("END", "uptime", time.Since(app.StartedAt()).Round(time.Second).String()) //::::::::::::::::::::::::
logger.Remove()
}()
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"BEGIN",
"id", app.ID(),
"name", app.Name(),
"version", app.Version(),
"builtAt", app.BuiltAt().String(),
"builtAt", app.BuiltAt().Format("2006-01-02 15:04:05"),
"pid", os.Getpid(),
)
defer func() {
logger.Info("END", "uptime", time.Since(app.StartedAt()).String()) //:::::::::::::::::::::::::::::::::::::::::::
}()
s.logger = logger
if err := s.createComponents(); err != nil {
return failure.Wrap(err, "impossible to create the components") //::::::::::::::::::::::::::::::::::::::::::::::
}
s.engine.Start(logger)
s.loop()
return s.loop()
return nil
}
/*

Loading…
Cancel
Save