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.

134 lines
3.4 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package memory
import (
"time"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/dune/dune/internal/storage"
)
func (ms *memoryStorage) findJob(id string) (int, *jw.Job) {
for i, job := range ms.jobs {
if job.ID == id {
return i, job
}
}
return 0, nil
}
func (ms *memoryStorage) ValidateJob(_ *jw.Job) error {
return nil
}
func (ms *memoryStorage) insertJob(job *jw.Job) (bool, error) {
if _, j := ms.findJob(job.ID); j != nil {
return false, errors.Wrap( /////////////////////////////////////////////////////////////////////////////////////
storage.ErrStorage,
errors.New("this job identifier already exists", "id", job.ID),
)
}
if job.Category != nil && job.Workflow == nil {
for _, j := range ms.jobs {
if j.Category != nil && j.Workflow == nil && *j.Category == *job.Category &&
j.Namespace == job.Namespace &&
j.Type == job.Type &&
(j.Status == jw.StatusTodo || j.Status == jw.StatusPending || j.Status == jw.StatusRunning) {
return false, nil
}
}
}
ms.jobs = append(ms.jobs, job)
return true, nil
}
func (ms *memoryStorage) InsertJob(job *jw.Job) (bool, error) {
ms.sm.Lock()
defer ms.sm.Unlock()
return ms.insertJob(job)
}
func (ms *memoryStorage) updateJob(job *jw.Job) error {
if i, j := ms.findJob(job.ID); j != nil {
ms.jobs[i] = job
return nil
}
return errors.Wrap( ////////////////////////////////////////////////////////////////////////////////////////////////
storage.ErrStorage,
errors.New("this job does not exist", "job", job.ID),
)
}
func (ms *memoryStorage) UpdateJob(job *jw.Job) error {
ms.sm.Lock()
defer ms.sm.Unlock()
return ms.updateJob(job)
}
func (ms *memoryStorage) NotifyJob(id string, data interface{}) error {
ms.sm.Lock()
defer ms.sm.Unlock()
_, job := ms.findJob(id)
if job == nil {
return errors.New("this job does not exist", "job", id) ////////////////////////////////////////////////////////
}
// AFINIR
return errors.NotImplemented()
}
func (ms *memoryStorage) SetJobPriority(id string, priority jw.Priority) error {
ms.sm.Lock()
defer ms.sm.Unlock()
_, job := ms.findJob(id)
if job == nil {
return errors.New("this job does not exist", "job", id) ////////////////////////////////////////////////////////
}
if job.FinishedAt != nil {
return errors.New("this job is finished", "job", id) ///////////////////////////////////////////////////////////
}
job.Priority = priority
return nil
}
func (ms *memoryStorage) SetJobRunAfter(id string, duration time.Duration) error {
ms.sm.Lock()
defer ms.sm.Unlock()
_, job := ms.findJob(id)
if job == nil {
return errors.New("this job does not exist", "job", id) ////////////////////////////////////////////////////////
}
if job.FinishedAt != nil {
return errors.New("this job is finished", "job", id) ///////////////////////////////////////////////////////////
}
job.RunAfter = time.Now().Add(duration)
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/