Browse Source

En cours de développement

master
losyme 3 months ago
parent
commit
8a5b9aa170
  1. 26
      internal/storage/memory/next.go

26
internal/storage/memory/next.go

@ -6,15 +6,22 @@
package memory
import "forge.chapril.org/dune/jw"
import (
"sort"
"time"
"forge.chapril.org/dune/jw"
)
type jobsToRun struct { // AFINIR: sync.Pool ?
jobs []*jw.Job
}
func (jtr *jobsToRun) Next() (*jw.Job, error) {
now := time.Now()
for i, job := range jtr.jobs {
if job.Status == jw.StatusTodo {
if (job.Status == jw.StatusTodo || job.Status == jw.StatusPending) && job.RunAfter.Before(now) {
jtr.jobs = jtr.jobs[i+1:]
return job, nil
}
@ -41,7 +48,20 @@ 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})
jobs := make([]*jw.Job, len(m.jobs))
copy(jobs, m.jobs)
sort.Slice(
jobs,
func(i, j int) bool {
ji := jobs[i]
jj := jobs[j]
return ji.Priority > jj.Priority || ji.Weight < jj.Weight || ji.TimeReference.Before(jj.TimeReference)
},
)
job, err := fn(m.jobsRunning(), &jobsToRun{jobs})
if job == nil || err != nil {
return nil, err
}

Loading…
Cancel
Save