Browse Source

En cours de développement

master
losyme 3 months ago
parent
commit
a0f76f3848
  1. 1
      Taskfile.yml
  2. 1
      go.mod
  3. 2
      go.sum
  4. 88
      workers/dashboard.go
  5. 103
      workers/worker.go
  6. 30
      workers/workers.go

1
Taskfile.yml

@ -8,6 +8,7 @@ tasks:
cmds:
- go get -u forge.chapril.org/dune/jw
- go get -u forge.chapril.org/losyme/errors
- go get -u forge.chapril.org/losyme/uuid
- go mod tidy
lint:
cmds:

1
go.mod

@ -5,6 +5,7 @@ go 1.17
require (
forge.chapril.org/dune/jw v0.0.0-20211114170701-4b66b0d705ea
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40
forge.chapril.org/losyme/uuid v0.0.0-20211007082235-8d502169e034
)
require (

2
go.sum

@ -6,3 +6,5 @@ forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40 h1:EoVVFHoIsW
forge.chapril.org/losyme/errors v0.0.0-20211003204336-ad5510c24b40/go.mod h1:saYfIRBW9zjiCik+/+5ZaajMEnTPBNI00lF9EnO+2kM=
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae h1:RdYWXKy+OQNJueb4Qqqo7sL4OY3/TpfUvmoIoH8ua9E=
forge.chapril.org/losyme/kvfmt v0.0.0-20211003204256-34ac32764dae/go.mod h1:wnVjwnCO2vFLLZ1EgFuFws1n8XLluufsGsdozGYSA7c=
forge.chapril.org/losyme/uuid v0.0.0-20211007082235-8d502169e034 h1:4NMOPXxwRiXS7wltj2ZZmMO1nFRMPmG1dXK4Ta4MtiM=
forge.chapril.org/losyme/uuid v0.0.0-20211007082235-8d502169e034/go.mod h1:AgvbvGzTkQkbiM+s9eVhvLUSHbA0V6N+q5oBFI2v0LA=

88
workers/dashboard.go

@ -0,0 +1,88 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"sync"
"time"
"forge.chapril.org/dune/jw"
)
type Worker struct {
Jobs int
Busy bool
JobID string
JobNamespace string
JobType string
StartedAt time.Time
}
type dashboard struct {
workers map[string]*Worker
mutex sync.Mutex
}
func newDashboard() *dashboard {
return &dashboard{
workers: make(map[string]*Worker),
}
}
func (d *dashboard) addJob(wID string, job *jw.Job) {
d.mutex.Lock()
defer d.mutex.Unlock()
w := d.workers[wID]
w.Jobs++
w.Busy = true
w.JobID = job.ID
w.JobNamespace = job.Namespace
w.JobType = job.Type
w.StartedAt = time.Now()
}
func (d *dashboard) removeJob(wID string) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.workers[wID].Busy = false
}
func (d *dashboard) addWorker(wID string) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.workers[wID] = new(Worker)
}
func (d *dashboard) removeWorker(wID string) {
d.mutex.Lock()
defer d.mutex.Unlock()
delete(d.workers, wID)
}
func (d *dashboard) data() map[string]*Worker {
d.mutex.Lock()
defer d.mutex.Unlock()
msw := make(map[string]*Worker)
for id, w := range d.workers {
clone := new(Worker)
*clone = *w
msw[id] = clone
}
return msw
}
/*
######################################################################################################## @(°_°)@ #######
*/

103
workers/worker.go

@ -0,0 +1,103 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### workers ####### Copyright (c) 2021 losyme ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"sync"
"time"
"forge.chapril.org/losyme/uuid"
)
type worker struct {
id string
hostname string
runner Runner
dashboard *dashboard
stopCh chan bool
logger Logger
}
func newWorker(hostname string, runner Runner, dashboard *dashboard, logger Logger) (*worker, error) {
id := uuid.New()
logger, err := logger.Clone(id, "worker")
if err != nil {
return nil, err
}
w := &worker{
id: id,
hostname: hostname,
runner: runner,
dashboard: dashboard,
stopCh: make(chan bool),
logger: logger,
}
return w, nil
}
func (w *worker) maybeRunJob() time.Duration {
return 1 * time.Second
}
func (w *worker) loop() {
defer func() {
if data := recover(); data != nil {
w.logger.Fatal("PANIC ERROR RECOVERED", "data", data) //::::::::::::::::::::::::::::::::::::::::::::::::::::
}
}()
timer := time.NewTimer(0)
defer func() {
if !timer.Stop() {
<-timer.C
}
close(w.stopCh)
}()
for {
select {
case <-timer.C:
timer.Reset(w.maybeRunJob())
case <-w.stopCh:
return
}
}
}
func (w *worker) run(swg *sync.WaitGroup, endCh chan *worker) {
swg.Add(1)
go func() { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
defer func() {
endCh <- w
swg.Done()
}()
w.logger.Info("Started", "id", w.id) //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
w.dashboard.addWorker(w.id)
w.loop()
w.dashboard.removeWorker(w.id)
w.logger.Info("Stopped") //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
w.logger.Remove()
}()
}
func (w *worker) stop() {
w.stopCh <- true
}
/*
######################################################################################################## @(°_°)@ #######
*/

30
workers/workers.go

@ -0,0 +1,30 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"forge.chapril.org/dune/jw"
"forge.chapril.org/dune/sdk"
"forge.chapril.org/dune/sdk/runner"
)
type Logger interface {
sdk.Logger
Clone(id, name string) (Logger, error)
Remove()
}
type Runner interface {
Namespace() string
Model() runner.Model
RunJob(job *jw.Job, logger sdk.Logger)
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save