Files
oc-schedulerd/daemons/execution_manager.go

116 lines
3.4 KiB
Go
Raw Normal View History

2024-07-11 18:25:40 +02:00
package daemons
2024-07-23 12:16:20 +02:00
import (
"oc-schedulerd/conf"
2024-07-23 12:16:20 +02:00
"time"
oclib "cloud.o-forge.io/core/oc-lib"
2026-01-14 15:16:19 +01:00
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/common/enum"
2024-08-21 14:20:13 +02:00
workflow_execution "cloud.o-forge.io/core/oc-lib/models/workflow_execution"
2026-01-14 15:16:19 +01:00
"go.mongodb.org/mongo-driver/bson/primitive"
2024-07-23 12:16:20 +02:00
)
2024-07-11 18:25:40 +02:00
var Executions = ScheduledExecution{Execs: map[string]workflow_execution.WorkflowExecution{}}
2024-08-20 15:24:46 +02:00
type ExecutionManager struct{}
2024-07-23 12:16:20 +02:00
2025-02-14 11:59:32 +01:00
// Loop every second on the Execution's list and move the Execution that must start to a new list
2024-07-23 12:16:20 +02:00
// that will be looped over to start them
func (em *ExecutionManager) RetrieveNextExecutions() {
logger := oclib.GetLogger()
for {
2025-02-14 11:59:32 +01:00
Executions.Mu.Lock()
if len(Executions.Execs) > 0 {
executions := Executions.Execs
2026-01-14 15:16:19 +01:00
orderedExec := map[int]map[string]workflow_execution.WorkflowExecution{}
for execId, exec := range executions {
2026-01-14 15:16:19 +01:00
if orderedExec[exec.Priority] == nil {
orderedExec[exec.Priority] = map[string]workflow_execution.WorkflowExecution{}
2024-07-23 12:16:20 +02:00
}
2026-01-14 15:16:19 +01:00
orderedExec[exec.Priority][execId] = exec
2024-07-23 12:16:20 +02:00
}
2026-01-14 15:16:19 +01:00
for i := range []int{7, 6, 5, 4, 3, 2, 1, 0} { // priority in reversed
if orderedExec[i] == nil {
continue
}
for execId, exec := range orderedExec[i] {
if i == 0 && em.isAStartingExecutionBeforeEnd(&exec) { // BEST EFFORT exception
continue
}
if exec.ExecDate.Before(time.Now().UTC()) {
logger.Info().Msg("Will execute " + execId + " soon")
go em.executeExecution(&exec)
delete(executions, execId)
}
}
}
}
2025-02-14 11:59:32 +01:00
Executions.Mu.Unlock()
2024-07-23 12:16:20 +02:00
time.Sleep(time.Second)
}
}
2026-01-14 15:16:19 +01:00
func (em *ExecutionManager) isAStartingExecutionBeforeEnd(execution *workflow_execution.WorkflowExecution) bool {
access := workflow_execution.NewAccessor(nil)
l, _, err := access.Search(&dbs.Filters{
And: map[string][]dbs.Filter{
"execution_date": {{Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(*execution.EndDate)}},
"state": {{Operator: dbs.EQUAL.String(), Value: enum.SCHEDULED}},
}, // TODO later should refine on each endpoint
}, "", false)
if err != nil && len(l) == 0 {
return false
}
return true
}
func (em *ExecutionManager) executeExecution(execution *workflow_execution.WorkflowExecution) {
// start execution
2024-07-29 15:45:32 +02:00
// create the yaml that describes the pod : filename, path/url to Loki
var executor Executor
// exec_method := os.Getenv("MONITOR_METHOD")
logger := oclib.GetLogger()
duration := 0
if execution.EndDate != nil {
duration = int(execution.EndDate.Sub(execution.ExecDate).Seconds())
2024-07-29 15:45:32 +02:00
}
if conf.GetConfig().Mode == "local" {
executor = NewLocalMonitor(execution.UUID, execution.CreatorID, duration)
}
if conf.GetConfig().Mode == "container" {
executor = NewContainerMonitor(execution.UUID, execution.CreatorID, duration)
}
if executor == nil {
logger.Fatal().Msg("Could not create executor")
}
args := executor.PrepareMonitorExec()
2025-11-20 16:30:58 +01:00
executor.LaunchMonitor(args, logger)
// if exec_method == "k8s" {
// logger.Error().Msg("TODO : executing oc-monitor in a k8s")
// } else {
// logger.Debug().Msg("Executing oc-monitor localy")
// duration := 0
// if Execution.EndDate != nil {
// duration = int(Execution.EndDate.Sub(Execution.ExecDate).Seconds())
// }
// monitor := LocalMonitor{
// Logger: logger,
// Duration: duration,
// ExecutionID: Execution.ExecutionsID,
// PeerID: Execution.CreatorID,
// LokiUrl: conf.GetConfig().LokiUrl,
2025-11-20 16:30:58 +01:00
// }
// monitor.LaunchLocalMonitor()
// }
2024-07-29 15:45:32 +02:00
}