Browse Source

En cours de développement

master
losyme 2 months ago
parent
commit
064433d4a0
  1. 25
      internal/api/api.go
  2. 41
      internal/api/config.go
  3. 16
      internal/application/api.go
  4. 11
      internal/application/model.go
  5. 34
      internal/model/config.go
  6. 92
      internal/model/jobs.go
  7. 19
      internal/model/model.go
  8. 74
      internal/model/storage.go

25
internal/api/api.go

@ -9,29 +9,24 @@ package api
import (
"net/http"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/kong/context"
"forge.chapril.org/losyme/kong/middleware"
"forge.chapril.org/losyme/kong/middleware/json"
"forge.chapril.org/losyme/kong/router"
"forge.chapril.org/losyme/logger"
"forge.chapril.org/dune/dune/internal/model"
)
type API struct {
*Config
}
func New(cfg *Config) (*API, error) {
if err := cfg.validate(); err != nil {
return nil, err
}
api := &API{cfg}
api.setup()
return api, nil
Router *router.Router
Model *model.Model
Storage jw.Storage
Logger *logger.Component
}
func (api *API) setup() {
func (api *API) Build() {
api.Router.MethodNotAllowed = json.MethodNotAllowed(api.Logger)
api.Router.NotFound = json.NotFound(api.Logger)
api.Router.OnRecover = json.OnRecover(api.Logger)
@ -57,7 +52,7 @@ func (api *API) status(c *context.Context) error {
}
func (api *API) dashboard(c *context.Context) error {
stats, err := api.Model.Storage.Dashboard()
stats, err := api.Storage.Dashboard()
if err != nil {
return err
}

41
internal/api/config.go

@ -1,41 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package api
import (
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/kong/router"
"forge.chapril.org/losyme/logger"
"forge.chapril.org/dune/dune/internal/model"
)
type Config struct {
Logger *logger.Component
Model *model.Model
Router *router.Router
}
func (cfg *Config) validate() error {
if cfg.Logger == nil {
return errors.New("[api] logger cannot be nil") ////////////////////////////////////////////////////////////////
}
if cfg.Model == nil {
return errors.New("[api] model cannot be nil") /////////////////////////////////////////////////////////////////
}
if cfg.Router == nil {
return errors.New("[api] router cannot be nil") ////////////////////////////////////////////////////////////////
}
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

16
internal/application/api.go

@ -20,17 +20,15 @@ func (app *Application) API() error {
return err
}
c, err := api.New(
&api.Config{
Logger: logger,
Model: cs.Model,
Router: cs.Router,
},
)
if err != nil {
return err
c := &api.API{
Router: cs.Router,
Model: cs.Model,
Storage: cs.Storage,
Logger: logger,
}
c.Build()
app.components.API = c
return nil

11
internal/application/model.go

@ -20,14 +20,9 @@ func (app *Application) model() error {
return err
}
c, err := model.New(
&model.Config{
Logger: logger,
Storage: cs.Storage,
},
)
if err != nil {
return err
c := &model.Model{
Storage: cs.Storage,
Logger: logger,
}
cs.Model = c

34
internal/model/config.go

@ -1,34 +0,0 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package model
import (
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/logger"
)
type Config struct {
Logger *logger.Component
Storage jw.Storage
}
func (cfg *Config) validate() error {
if cfg.Logger == nil {
return errors.New("[model] logger cannot be nil") //////////////////////////////////////////////////////////////
}
if cfg.Storage == nil {
return errors.New("[model] storage cannot be nil") /////////////////////////////////////////////////////////////
}
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

92
internal/model/jobs.go

@ -60,7 +60,99 @@ func (m *Model) CreateJob(job *jw.Job) (*jw.Job, error) {
return nil, nil
}
func (m *Model) updateJob(job *jw.Job) error {
if job.Workflow == nil || job.Status == jw.StatusPending {
err := m.storeJob(job)
if err != nil {
m.Logger.Error( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Impossible to update this job",
append(
job.Fields(),
"reason", err,
)...,
)
}
return err
}
/* AFINIR
wf, err := m.Storage.Workflow(*job.Workflow, true)
if err != nil {
m.Logger.Error( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Cannot retrieve the workflow associated with this job",
append(
job.Fields(),
"reason", err,
)...,
)
return err
}
if err := m.updateWorkflow(wf, job); err != nil {
m.Logger.Error( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Impossible to update this workflow",
append(
wf.Fields(),
"reason", err,
)...,
)
return err
}
*/
return nil
}
func (m *Model) UpdateJob(job *jw.Job) (*jw.Job, error) {
if job.Result == nil {
job.Succeeded()
}
jr := job.Result
m.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Job result",
"id", job.ID,
"status", jr.Status,
"next_step", jr.NextStep,
"value", jr.Value,
"duration", jr.Duration.String(),
"error", jr.Error,
)
switch jr.Status {
case jw.StatusSucceeded:
job.Finished(jw.StatusSucceeded)
case jw.StatusPending:
if jr.Error == "" {
job.RunAfter = time.Now().Add(jr.Duration)
job.Status = jw.StatusPending
break
}
if job.Attempt < job.MaxRetries {
job.RunAfter = time.Now().Add(jr.Duration)
job.Status = jw.StatusPending
job.Attempt++
break
}
fallthrough
default:
job.Finished(jw.StatusFailed)
}
if err := m.updateJob(job); err != nil {
return nil, err
}
return job, nil
}

19
internal/model/model.go

@ -6,7 +6,11 @@
package model
import "forge.chapril.org/losyme/errors"
import (
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/logger"
)
const (
ErrInternal = errors.Sentinel("internal error")
@ -14,17 +18,8 @@ const (
)
type Model struct {
*Config
}
func New(cfg *Config) (*Model, error) {
if err := cfg.validate(); err != nil {
return nil, err
}
m := &Model{cfg}
return m, nil
Storage jw.Storage
Logger *logger.Component
}
/*

74
internal/model/storage.go

@ -0,0 +1,74 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package model
import (
"fmt"
"time"
"forge.chapril.org/dune/jw"
)
func (m *Model) logJob(job *jw.Job) {
if job.Status == jw.StatusPending {
m.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Job to continue",
"id", job.ID,
"run_after", job.RunAfter.Round(time.Second).String(),
"attempt", fmt.Sprintf("%d/%d", job.Attempt, job.MaxRetries),
)
} else {
m.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Job finished",
"id", job.ID,
"status", job.Status,
"duration", (*job.FinishedAt).Sub(job.CreatedAt).String(),
)
}
}
func (m *Model) storeJob(job *jw.Job) error {
if err := m.Storage.UpdateJob(job); err != nil {
// AFINIR
return err
}
m.logJob(job)
return nil
}
func (m *Model) logWorkflow(wf *jw.Workflow, job, nextJob *jw.Job) {
m.logJob(job)
if nextJob != nil {
m.Logger.Info("New job", nextJob.Fields()...) //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
return
}
m.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Workflow finished",
"id", wf.ID,
"status", wf.Status,
"duration", (*wf.FinishedAt).Sub(wf.CreatedAt).String(),
)
}
func (m *Model) storeWorkflow(wf *jw.Workflow, job, nextJob *jw.Job) error {
if err := m.Storage.UpdateWorkflow(wf, job, nextJob); err != nil {
// AFINIR
return err
}
m.logWorkflow(wf, job, nextJob)
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save