improved graph/workflow retrieval

This commit is contained in:
pb
2024-07-23 12:16:20 +02:00
parent 0856c90930
commit 559bac5eb9
5 changed files with 143 additions and 57 deletions

View File

@@ -1,12 +1,68 @@
package daemons
import "oc-scheduler/models"
import (
"oc-scheduler/logger"
"oc-scheduler/models"
"oc-scheduler/workflow_builder"
"time"
)
type ExecutionManager struct {
Bookings models.ScheduledBooking
bookings *models.ScheduledBooking
executions []models.Booking
}
func (em *ExecutionManager) test(){
em.Bookings.Mu.Lock()
defer em.Bookings.Mu.Unlock()
func (em *ExecutionManager) SetBookings(b *models.ScheduledBooking){
em.bookings = b
}
// Loop every second on the booking's list and move the booking that must start to a new list
// that will be looped over to start them
func (em *ExecutionManager) RetrieveNextExecutions(){
if(em.bookings == nil){
logger.Logger.Fatal().Msg("booking has not been set in the exection manager")
}
for(true){
logger.Logger.Debug().Msg("New loop")
em.bookings.Mu.Lock()
bookings := em.bookings.Bookings
if (len(bookings) > 0){
for i := len( bookings) - 1 ; i >= 0 ; i--{
logger.Logger.Debug().Msg("It should start at " + bookings[i].Start.String() + " and it is now " + time.Now().UTC() .String())
if (bookings[i].Start.Before(time.Now().UTC())){
logger.Logger.Info().Msg("Will execute " + bookings[i].Workflow + " soon")
go em.executeBooking(bookings[i])
bookings = append(bookings[:i], bookings[i+1:]...)
em.bookings.Bookings = bookings
}
}
}
em.bookings.Mu.Unlock()
time.Sleep(time.Second)
}
}
func (em *ExecutionManager) executeBooking(booking models.Booking){
// create argo
new_graph := workflow_builder.Graph{}
err := new_graph.LoadFrom(booking.Workflow)
if err != nil {
logger.Logger.Error().Msg("Could not retrieve workflow " + booking.Workflow + " from oc-catalog API")
}
argo_file, err := new_graph.ExportToArgo()
if err != nil {
logger.Logger.Error().Msg("Could not create the Argo file for " + booking.Workflow )
logger.Logger.Error().Msg(err.Error())
}
_ = argo_file
// start execution
// locally launch a pod that contains oc-monitor, give it the name of the workflow
// create the yaml that describes the pod : filename, path/url to Loki
// locally launch an argo workflow with the filename `argo submit PATH_TO_YAML --watch --serviceaccount=argo -n argo`
}

View File

@@ -1,53 +0,0 @@
package daemons
import (
"io"
"net/http"
"net/http/cookiejar"
"net/url"
)
type HttpQuery struct {
baseurl string
jar http.CookieJar
Cookies map[string]string
}
func (h *HttpQuery) Init(url string) {
h.baseurl = url
h.jar, _ = cookiejar.New(nil)
h.Cookies = make(map[string]string)
}
func (h *HttpQuery) Get(url string) ([]byte, error) {
client := &http.Client{Jar: h.jar}
resp, err := client.Get(h.baseurl + url)
if err != nil {
return nil, err
}
// store received cookies
for _, cookie := range h.jar.Cookies(resp.Request.URL) {
h.Cookies[cookie.Name] = cookie.Value
}
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (h *HttpQuery) Post(url string, data url.Values) (*http.Response, error) {
client := &http.Client{Jar: h.jar}
resp, err := client.PostForm(h.baseurl+url, data)
if err != nil {
return nil, err
}
// store received cookies
for _, cookie := range h.jar.Cookies(resp.Request.URL) {
h.Cookies[cookie.Name] = cookie.Value
}
return resp, err
}

View File

@@ -17,13 +17,15 @@ import (
type ScheduleManager struct {
Api_url string
list models.ScheduledBooking
ws HttpQuery
Api_url string
bookings *models.ScheduledBooking
ws models.HttpQuery
}
func (s *ScheduleManager) SetBookings(b *models.ScheduledBooking){
s.bookings = b
}
// Goroutine listening to a NATS server for updates
// on workflows' scheduling. Messages must contain
@@ -31,6 +33,10 @@ type ScheduleManager struct {
// is no way to get scheduling infos for a specific workflow
func (s *ScheduleManager) ListenWorkflowSubmissions(){
if(s.bookings == nil){
logger.Logger.Fatal().Msg("booking has not been set in the schedule manager")
}
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
@@ -48,7 +54,7 @@ func (s *ScheduleManager) ListenWorkflowSubmissions(){
map_mess := retrieveMapFromSub(msg.Data)
s.list.Mu.Lock()
s.bookings.Mu.Lock()
start, err := time.Parse(time.RFC3339,map_mess["start_date"])
if err != nil{
@@ -59,8 +65,8 @@ func (s *ScheduleManager) ListenWorkflowSubmissions(){
logger.Logger.Error().Msg(err.Error())
}
s.list.AddSchedule(models.Booking{Workflow: map_mess["workflow"], Start: start, Stop: stop })
s.list.Mu.Unlock()
s.bookings.AddSchedule(models.Booking{Workflow: map_mess["workflow"], Start: start, Stop: stop })
s.bookings.Mu.Unlock()
}
}
@@ -81,7 +87,7 @@ func (s *ScheduleManager) RetrieveScheduling (){
}
logger.Logger.Info().Msg("Current list of schedules")
fmt.Println(s.list.Bookings)
fmt.Println(s.bookings.Bookings)
time.Sleep(time.Minute * 5)
}
@@ -102,14 +108,14 @@ func (s *ScheduleManager) getNextScheduledWorkflows(apiurl string, hours float64
var workflows []map[string]string
json.Unmarshal(body,&workflows)
s.list.Mu.Lock()
defer s.list.Mu.Unlock()
s.bookings.Mu.Lock()
defer s.bookings.Mu.Unlock()
for _, workflow := range(workflows){
start, _ := time.Parse(time.RFC3339,workflow["start_date"])
stop, _ := time.Parse(time.RFC3339,workflow["stop_date"])
s.list.AddSchedule(models.Booking{Workflow: workflow["Workflow"], Start: start, Stop: stop})
s.bookings.AddSchedule(models.Booking{Workflow: workflow["Workflow"], Start: start, Stop: stop})
}
return nil