From 1ade41aeae84563e8748d26d76abe78dbe8cecf7 Mon Sep 17 00:00:00 2001 From: pb Date: Mon, 26 May 2025 19:05:17 +0200 Subject: [PATCH] moved the code that execute the booking into a separated function so that it can be launched as goroutine and parallelize get booking$ --- .../workflow_execution/workflow_scheduler.go | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/models/workflow_execution/workflow_scheduler.go b/models/workflow_execution/workflow_scheduler.go index 7ec29e2..8b9ca26 100644 --- a/models/workflow_execution/workflow_scheduler.go +++ b/models/workflow_execution/workflow_scheduler.go @@ -15,6 +15,7 @@ import ( "cloud.o-forge.io/core/oc-lib/tools" "github.com/google/uuid" "github.com/robfig/cron" + "github.com/rs/zerolog" ) /* @@ -83,24 +84,39 @@ func (ws *WorkflowSchedule) CheckBooking(wfID string, request *tools.APIRequest) l.Debug().Msg("looping throughs execs : " + string(i)) bookings = append(bookings, exec.Book(ws.UUID, wfID, priceds)...) } + + errCh := make(chan error, len(bookings)) + for _, b := range bookings { - bl := l.With().Str("booking",b.UUID).Logger() - meth := request.Caller.URLS[tools.BOOKING][tools.GET] - meth = strings.ReplaceAll(meth, ":id", b.ResourceID) - meth = strings.ReplaceAll(meth, ":start_date", b.ExpectedStartDate.Format("2006-01-02T15:04:05")) - meth = strings.ReplaceAll(meth, ":end_date", b.ExpectedEndDate.Format("2006-01-02T15:04:05")) - request.Caller.URLS[tools.BOOKING][tools.GET] = meth - bl.Debug().Msg("Get booking on" + b.DestPeerID) - _, err := (&peer.Peer{}).LaunchPeerExecution(b.DestPeerID, b.ResourceID, tools.BOOKING, tools.GET, nil, request.Caller) - bl.Debug().Msg("Received response from Get booking on " + b.DestPeerID) + go getBooking(l, b, request, wf, execs, bookings, errCh) + } + + for err := range(errCh){ if err != nil { return false, wf, execs, bookings, err } - } + return true, wf, execs, bookings, nil } +func getBooking(l zerolog.Logger, b *booking.Booking, request *tools.APIRequest, wf *workflow.Workflow, execs []*WorkflowExecution, bookings []*booking.Booking, errCh chan error) { + bl := l.With().Str("booking", b.UUID).Logger() + meth := request.Caller.URLS[tools.BOOKING][tools.GET] + meth = strings.ReplaceAll(meth, ":id", b.ResourceID) + meth = strings.ReplaceAll(meth, ":start_date", b.ExpectedStartDate.Format("2006-01-02T15:04:05")) + meth = strings.ReplaceAll(meth, ":end_date", b.ExpectedEndDate.Format("2006-01-02T15:04:05")) + request.Caller.URLS[tools.BOOKING][tools.GET] = meth + bl.Debug().Msg("Get booking " + b.UUID + "on" + b.DestPeerID) + _, err := (&peer.Peer{}).LaunchPeerExecution(b.DestPeerID, b.ResourceID, tools.BOOKING, tools.GET, nil, request.Caller) + bl.Debug().Msg("Received response from Get booking on " + b.DestPeerID) + if err != nil { + errCh <- err + } + + errCh <- nil +} + func (ws *WorkflowSchedule) Schedules(wfID string, request *tools.APIRequest) (*WorkflowSchedule, *workflow.Workflow, []*WorkflowExecution, error) { if request == nil { return ws, nil, []*WorkflowExecution{}, errors.New("no request found")