launch streaming to update booking

This commit is contained in:
mr 2025-06-18 09:14:30 +02:00
parent f61c9f5df9
commit c0b8ac1eee
2 changed files with 44 additions and 34 deletions

View File

@ -281,6 +281,12 @@ func (o *BookingController) Post() {
o.ServeJSON()
return
}
if b.Data.(*booking.Booking).ResourceType == tools.COMPUTE_RESOURCE {
go func() {
time.Sleep(time.Until(b.Data.(*booking.Booking).ExpectedStartDate))
infrastructure.NewPrometheusService().Stream(b.Data.GetID(), b.Data.(*booking.Booking).ExpectedEndDate, 1*time.Second, nil, nil)
}()
}
if err := o.createNamespace(resp.ExecutionsID); err != nil {
fmt.Println(err.Error())

View File

@ -131,46 +131,50 @@ func (p *PrometheusService) Call(bookingID string) (*booking.Booking, map[string
return book.Data.(*booking.Booking), metrics
}
func (p *PrometheusService) Stream(bookingID string, end *time.Time, interval time.Duration, flusher http.Flusher, encoder *json.Encoder) {
func (p *PrometheusService) Stream(bookingID string, end *time.Time, interval time.Duration, flusher *http.Flusher, encoder *json.Encoder) {
e := time.Now().UTC().Add(time.Hour * 1)
if end != nil {
max := 100
bookIDS := []string{}
mets := map[string][]models.MetricsSnapshot{}
for time.Now().Before(*end) {
go func() {
book, metrics := p.Call(bookingID)
for k, v := range metrics {
if me, ok := mets[k]; !ok {
mets[k] = []models.MetricsSnapshot{v}
} else {
me = append(me, v)
mets[k] = me
}
e = (*end).UTC()
}
max := 100
bookIDS := []string{}
mets := map[string][]models.MetricsSnapshot{}
for time.Now().Before(e) {
go func() {
book, metrics := p.Call(bookingID)
for k, v := range metrics {
if me, ok := mets[k]; !ok {
mets[k] = []models.MetricsSnapshot{v}
} else {
me = append(me, v)
mets[k] = me
}
bookIDS = append(bookIDS, bookingID)
}
bookIDS = append(bookIDS, bookingID)
if flusher != nil {
encoder.Encode(metrics)
flusher.Flush()
if len(bookIDS) == max {
if book.ExecutionMetrics == nil {
book.ExecutionMetrics = mets
(*flusher).Flush()
}
if len(bookIDS) != max {
return
}
if book.ExecutionMetrics == nil {
book.ExecutionMetrics = mets
} else {
for kk, vv := range mets {
if em, ok := book.ExecutionMetrics[kk]; !ok {
book.ExecutionMetrics[kk] = vv
} else {
for kk, vv := range mets {
if em, ok := book.ExecutionMetrics[kk]; !ok {
book.ExecutionMetrics[kk] = vv
} else {
em = append(em, vv...)
book.ExecutionMetrics[kk] = em
}
}
em = append(em, vv...)
book.ExecutionMetrics[kk] = em
}
book.GetAccessor(nil).UpdateOne(book, bookingID)
bookIDS = []string{}
}
}()
time.Sleep(interval)
}
} else {
// todo an anchor... detecting the end of a task.
}
book.GetAccessor(nil).UpdateOne(book, bookingID)
bookIDS = []string{}
}()
time.Sleep(interval)
}
}