2024-07-11 18:25:40 +02:00
|
|
|
package daemons
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-08-20 09:23:05 +02:00
|
|
|
"oc-schedulerd/conf"
|
2024-08-19 11:42:26 +02:00
|
|
|
"sync"
|
2024-07-11 18:25:40 +02:00
|
|
|
"time"
|
|
|
|
|
2024-08-09 18:44:33 +02:00
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
2024-07-11 18:25:40 +02:00
|
|
|
"github.com/nats-io/nats.go"
|
2024-08-19 11:42:26 +02:00
|
|
|
"github.com/rs/zerolog"
|
2024-08-09 18:44:33 +02:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2024-07-11 18:25:40 +02:00
|
|
|
)
|
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
type Booking struct {
|
|
|
|
Start time.Time
|
|
|
|
Stop time.Time
|
|
|
|
Duration uint
|
|
|
|
Workflow string
|
|
|
|
}
|
2024-07-18 10:23:22 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
func (s Booking) Equals(other Booking) bool {
|
|
|
|
return s.Workflow == other.Workflow && s.Start == other.Start && s.Stop == other.Stop
|
|
|
|
}
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
func (b *Booking) String() string {
|
|
|
|
return fmt.Sprintf("{workflow : %s , start_date : %s , stop_date : %s }", b.Workflow, b.Start.Format(time.RFC3339), b.Stop.Format(time.RFC3339))
|
2024-07-11 18:25:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
type ScheduledBooking struct {
|
|
|
|
Bookings []Booking
|
|
|
|
Mu sync.Mutex
|
2024-07-23 12:16:20 +02:00
|
|
|
}
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
func (sb *ScheduledBooking) AddSchedule(new_booking Booking, logger zerolog.Logger) {
|
|
|
|
if !sb.scheduleAlreadyExists(new_booking) {
|
|
|
|
sb.Bookings = append(sb.Bookings, new_booking)
|
|
|
|
logger.Info().Msg("Updated list schedules : \n " + sb.String())
|
2024-07-23 12:16:20 +02:00
|
|
|
}
|
2024-08-19 11:42:26 +02:00
|
|
|
logger.Debug().Msg("Workflow received not added")
|
|
|
|
}
|
2024-07-23 12:16:20 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
func (sb *ScheduledBooking) scheduleAlreadyExists(new_booking Booking) bool {
|
|
|
|
for _, booking := range sb.Bookings {
|
|
|
|
if booking.Equals(new_booking) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *ScheduledBooking) String() string {
|
|
|
|
var str string
|
|
|
|
for _, booking := range sb.Bookings {
|
|
|
|
str += fmt.Sprintf("%s\n", booking.String())
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
// NATS daemon listens to subject " workflowsUpdate "
|
|
|
|
// workflowsUpdate messages must be formatted following this pattern '{"workflow" : "", "start_date" : "", "stop_date" : "" }'
|
|
|
|
|
|
|
|
type ScheduleManager struct {
|
|
|
|
Logger zerolog.Logger
|
|
|
|
bookings ScheduledBooking
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goroutine listening to a NATS server for updates
|
|
|
|
// on workflows' scheduling. Messages must contain
|
|
|
|
// workflow execution ID, to allow retrieval of execution infos
|
|
|
|
func (s *ScheduleManager) ListenForWorkflowSubmissions() {
|
2024-08-20 09:23:05 +02:00
|
|
|
nc, err := nats.Connect(conf.GetConfig().NatsUrl)
|
2024-08-09 18:44:33 +02:00
|
|
|
if err != nil {
|
2024-08-19 11:42:26 +02:00
|
|
|
s.Logger.Error().Msg("Could not connect to NATS")
|
2024-08-09 18:44:33 +02:00
|
|
|
return
|
|
|
|
}
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-09 18:44:33 +02:00
|
|
|
defer nc.Close()
|
2024-07-11 18:25:40 +02:00
|
|
|
|
|
|
|
ch := make(chan *nats.Msg, 64)
|
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
subs, err := nc.ChanSubscribe("workflowsUpdate", ch)
|
2024-07-11 18:25:40 +02:00
|
|
|
if err != nil {
|
2024-08-19 11:42:26 +02:00
|
|
|
s.Logger.Error().Msg("Error listening to NATS")
|
2024-07-11 18:25:40 +02:00
|
|
|
}
|
|
|
|
defer subs.Unsubscribe()
|
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
for msg := range ch {
|
2024-07-11 18:25:40 +02:00
|
|
|
fmt.Println("Waiting...")
|
|
|
|
|
|
|
|
map_mess := retrieveMapFromSub(msg.Data)
|
2024-08-19 11:42:26 +02:00
|
|
|
|
2024-07-23 12:16:20 +02:00
|
|
|
s.bookings.Mu.Lock()
|
2024-08-09 18:44:33 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
wf_exec := s.getWorkflowExecution(map_mess["workflow"])
|
|
|
|
|
|
|
|
s.bookings.AddSchedule(Booking{Workflow: map_mess["workflow"], Start: *wf_exec.ExecDate, Stop: *wf_exec.EndDate}, s.Logger)
|
2024-07-23 12:16:20 +02:00
|
|
|
s.bookings.Mu.Unlock()
|
2024-08-19 11:42:26 +02:00
|
|
|
|
2024-07-11 18:25:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
func (s *ScheduleManager) getWorkflowExecution(exec_id string) *workflow_execution.WorkflowExecution {
|
|
|
|
res := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), exec_id)
|
2024-08-09 18:44:33 +02:00
|
|
|
if res.Code != 200 {
|
2024-08-19 11:42:26 +02:00
|
|
|
s.Logger.Error().Msg("Could not retrieve workflow ID from execution ID " + exec_id)
|
2024-08-09 18:44:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
wf_exec := res.ToWorkflowExecution()
|
|
|
|
|
|
|
|
return wf_exec
|
|
|
|
}
|
|
|
|
|
2024-07-18 10:23:22 +02:00
|
|
|
// At the moment very simplistic, but could be useful if we send bigger messages
|
2024-07-11 18:25:40 +02:00
|
|
|
func retrieveMapFromSub(message []byte) (result_map map[string]string) {
|
|
|
|
json.Unmarshal(message, &result_map)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used at launch of the component to retrieve the next scheduled workflows
|
|
|
|
// and then every X minutes in case some workflows were scheduled before launch
|
2024-08-19 11:42:26 +02:00
|
|
|
func (s *ScheduleManager) SchedulePolling() {
|
2024-08-09 18:44:33 +02:00
|
|
|
var sleep_time float64 = 1
|
2024-08-19 11:42:26 +02:00
|
|
|
for {
|
2024-08-12 16:09:30 +02:00
|
|
|
s.getNextScheduledWorkflows(3)
|
2024-08-19 11:42:26 +02:00
|
|
|
|
|
|
|
s.Logger.Info().Msg("Current list of schedules")
|
2024-07-23 12:16:20 +02:00
|
|
|
fmt.Println(s.bookings.Bookings)
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-09 18:44:33 +02:00
|
|
|
time.Sleep(time.Minute * time.Duration(sleep_time))
|
2024-07-11 18:25:40 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 18:44:33 +02:00
|
|
|
func (s *ScheduleManager) getWorfklowExecution(from time.Time, to time.Time) (exec_list []workflow_execution.WorkflowExecution, err error) {
|
|
|
|
f := dbs.Filters{
|
|
|
|
And: map[string][]dbs.Filter{
|
2024-08-19 11:42:26 +02:00
|
|
|
"execution_date": {{Operator: dbs.GTE.String(), Value: primitive.NewDateTimeFromTime(from)}, {Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(to)}},
|
|
|
|
"state": {{Operator: dbs.EQUAL.String(), Value: 1}},
|
2024-08-09 18:44:33 +02:00
|
|
|
},
|
|
|
|
}
|
2024-08-19 11:42:26 +02:00
|
|
|
res := oclib.Search(&f, "", oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION))
|
2024-08-09 18:44:33 +02:00
|
|
|
if res.Code != 200 {
|
2024-08-19 11:42:26 +02:00
|
|
|
s.Logger.Error().Msg("Error loading")
|
2024-08-09 18:44:33 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
for _, exec := range res.Data {
|
|
|
|
lib_data := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), exec.GetID())
|
2024-08-09 18:44:33 +02:00
|
|
|
exec_obj := lib_data.ToWorkflowExecution()
|
|
|
|
exec_list = append(exec_list, *exec_obj)
|
|
|
|
}
|
|
|
|
return exec_list, nil
|
|
|
|
}
|
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
func (s *ScheduleManager) getNextScheduledWorkflows(minutes float64) {
|
2024-07-11 18:25:40 +02:00
|
|
|
start := time.Now().UTC()
|
2024-08-09 18:44:33 +02:00
|
|
|
end := start.Add(time.Minute * time.Duration(minutes)).UTC()
|
2024-08-19 11:42:26 +02:00
|
|
|
|
2024-08-09 18:44:33 +02:00
|
|
|
fmt.Printf("Getting workflows execution from %s to %s \n", start.String(), end.String())
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
next_wf_exec, err := s.getWorfklowExecution(start, end)
|
2024-07-11 18:25:40 +02:00
|
|
|
if err != nil {
|
2024-08-19 11:42:26 +02:00
|
|
|
s.Logger.Error().Msg("Could not retrieve next schedules")
|
2024-08-09 18:44:33 +02:00
|
|
|
return
|
2024-07-11 18:25:40 +02:00
|
|
|
}
|
2024-08-09 18:44:33 +02:00
|
|
|
|
2024-07-23 12:16:20 +02:00
|
|
|
s.bookings.Mu.Lock()
|
|
|
|
defer s.bookings.Mu.Unlock()
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
for _, exec := range next_wf_exec {
|
2024-08-09 18:44:33 +02:00
|
|
|
exec_start := exec.ExecDate
|
|
|
|
exec_stop := exec.EndDate
|
2024-07-11 18:25:40 +02:00
|
|
|
|
2024-08-19 11:42:26 +02:00
|
|
|
s.bookings.AddSchedule(Booking{Workflow: exec.UUID, Start: *exec_start, Stop: *exec_stop}, s.Logger)
|
2024-07-11 18:25:40 +02:00
|
|
|
}
|
|
|
|
}
|