68 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package daemons
 | 
						|
 | 
						|
import (
 | 
						|
	"oc-scheduler/logger"
 | 
						|
	"oc-scheduler/models"
 | 
						|
	"oc-scheduler/workflow_builder"
 | 
						|
	"time"
 | 
						|
)
 | 
						|
 | 
						|
type ExecutionManager struct {
 | 
						|
	bookings	*models.ScheduledBooking
 | 
						|
	executions	[]models.Booking	
 | 
						|
}
 | 
						|
 | 
						|
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`
 | 
						|
} |