Gestionnaire de jobs et workflows
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.3 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package memory
import "forge.chapril.org/dune/jw"
type jobsToRun struct { // AFINIR: sync.Pool ?
jobs []*jw.Job
}
func (jtr *jobsToRun) Next() (*jw.Job, error) {
for i, job := range jtr.jobs {
if job.Status == jw.StatusTodo {
jtr.jobs = jtr.jobs[i+1:]
return job, nil
}
}
return nil, nil
}
func (m *memory) jobsRunning() jw.JobsRunning {
jr := make(jw.JobsRunning)
for _, job := range m.jobs {
if job.Status != jw.StatusRunning {
continue
}
jr[job.Namespace+job.Type] += 1
}
return jr
}
func (m *memory) NextJob(fn jw.SelectNextJob) (*jw.Job, error) {
m.sm.Lock()
defer m.sm.Unlock()
job, err := fn(m.jobsRunning(), &jobsToRun{jobs: m.jobs})
if job == nil || err != nil {
return nil, err
}
// Sufffisant ?
clone := new(jw.Job)
*clone = *job
job.Status = jw.StatusRunning
job.Weight++
return clone, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/