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() o.ServeJSON()
return 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 { if err := o.createNamespace(resp.ExecutionsID); err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())

View File

@ -131,12 +131,15 @@ func (p *PrometheusService) Call(bookingID string) (*booking.Booking, map[string
return book.Data.(*booking.Booking), metrics 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 { if end != nil {
e = (*end).UTC()
}
max := 100 max := 100
bookIDS := []string{} bookIDS := []string{}
mets := map[string][]models.MetricsSnapshot{} mets := map[string][]models.MetricsSnapshot{}
for time.Now().Before(*end) { for time.Now().Before(e) {
go func() { go func() {
book, metrics := p.Call(bookingID) book, metrics := p.Call(bookingID)
for k, v := range metrics { for k, v := range metrics {
@ -148,9 +151,13 @@ func (p *PrometheusService) Stream(bookingID string, end *time.Time, interval ti
} }
} }
bookIDS = append(bookIDS, bookingID) bookIDS = append(bookIDS, bookingID)
if flusher != nil {
encoder.Encode(metrics) encoder.Encode(metrics)
flusher.Flush() (*flusher).Flush()
if len(bookIDS) == max { }
if len(bookIDS) != max {
return
}
if book.ExecutionMetrics == nil { if book.ExecutionMetrics == nil {
book.ExecutionMetrics = mets book.ExecutionMetrics = mets
} else { } else {
@ -165,13 +172,10 @@ func (p *PrometheusService) Stream(bookingID string, end *time.Time, interval ti
} }
book.GetAccessor(nil).UpdateOne(book, bookingID) book.GetAccessor(nil).UpdateOne(book, bookingID)
bookIDS = []string{} bookIDS = []string{}
}
}() }()
time.Sleep(interval) time.Sleep(interval)
} }
} else {
// todo an anchor... detecting the end of a task.
}
} }
// should add a datacenter... under juridiction... of opencloud... // should add a datacenter... under juridiction... of opencloud...