Browse Source

En cours de développement

master
losyme 7 months ago
parent
commit
9998faa048
  1. 72
      workers/worker.go

72
workers/worker.go

@ -1,15 +1,17 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### workers ####### Copyright (c) 2021 losyme ################################################## MIT License #######
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"fmt"
"sync"
"time"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/uuid"
)
@ -43,8 +45,74 @@ func newWorker(hostname string, runner Runner, dashboard *dashboard, logger Logg
return w, nil
}
func (w *worker) cloneLogger(job *jw.Job) (Logger, error) {
id := job.ID
name := "job"
if job.Workflow != nil {
id = *job.Workflow
name = "workflow"
}
return w.logger.Clone(id, name)
}
func (w *worker) maybeRunJob() time.Duration {
return 10 * time.Second
job, err := w.runner.Model().NextJob(w.runner.Namespace())
if err != nil {
return 5 * time.Second
}
if job == nil {
return 500 * time.Millisecond
}
w.dashboard.addJob(w.id, job)
defer w.dashboard.removeJob(w.id)
logger, err := w.cloneLogger(job)
if err == nil {
defer logger.Remove()
} else {
w.logger.Warning("Unable to clone the logger", "reason", err) //::::::::::::::::::::::::::::::::::::::::::::::::
logger = w.logger
}
logger.Info("JOB", job.Fields()...) //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/* AFINIR
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Worker",
"id", w.id,
"jobs", w.jobs,
)
*/
w.runner.RunJob(job, logger)
if job.Result == nil {
job.Succeeded()
}
job.Result.Host = w.hostname // AFINIR
job.Result.Worker = w.id
if job, err := w.runner.Model().UpdateJob(job); err == nil {
if job.Status == jw.StatusPending {
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"END",
"status", job.Status,
"run_after", job.RunAfter.Round(time.Second).String(),
"retries", fmt.Sprintf("%d/%d", job.Attempt, job.MaxRetries),
)
} else {
logger.Info("END", "status", job.Status) //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
}
} else {
logger.Info("END", "model", err) //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
}
return 0
}
func (w *worker) loop() {

Loading…
Cancel
Save