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.

107 lines
2.5 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package memory
import (
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
)
func (m *memory) findWorkflow(id string) (int, *jw.Workflow) {
for i, wf := range m.workflows {
if wf.ID == id {
return i, wf
}
}
return 0, nil
}
func (m *memory) ValidateWorkflow(_ *jw.Workflow) error {
return nil
}
func (m *memory) InsertWorkflow(wf *jw.Workflow, job *jw.Job) error {
m.sm.Lock()
defer m.sm.Unlock()
if _, w := m.findWorkflow(wf.ID); w != nil {
return errors.New( /////////////////////////////////////////////////////////////////////////////////////////////
"this workflow identifier already exists",
"id", wf.ID,
)
}
if _, err := m.insertJob(job); err != nil {
return err
}
m.workflows = append(m.workflows, wf)
return nil
}
func (m *memory) Workflow(id string, mustExist bool) (*jw.Workflow, error) {
m.sm.Lock()
defer m.sm.Unlock()
if _, wf := m.findWorkflow(id); wf != nil {
return wf, nil
}
if mustExist {
return nil, errors.New("this workflow does not exist") /////////////////////////////////////////////////////////
}
return nil, nil
}
func (m *memory) UpdateWorkflow(wf *jw.Workflow, job, nextJob *jw.Job) error {
m.sm.Lock()
defer m.sm.Unlock()
i, w := m.findWorkflow(wf.ID)
if w == nil {
return errors.New("this workflow does not exist") //////////////////////////////////////////////////////////////
}
m.workflows[i] = wf
if err := m.updateJob(job); err != nil {
return err
}
if nextJob == nil {
return nil
}
_, err := m.insertJob(nextJob)
return err
}
func (m *memory) SetWorkflowPriority(id string, priority jw.Priority) error {
m.sm.Lock()
defer m.sm.Unlock()
_, wf := m.findWorkflow(id)
if wf == nil {
return errors.New("this workflow does not exist", "workflow", id) //////////////////////////////////////////////
}
if wf.Status != jw.StatusRunning {
return errors.New("this workflow is no longer running", "workflow", id) ////////////////////////////////////////
}
wf.Priority = priority
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/