Refactor Oc-Sheduler

This commit is contained in:
mr
2026-03-25 11:11:37 +01:00
parent 7cbe08f4ea
commit 12eba65a01
24 changed files with 3498 additions and 2047 deletions

View File

@@ -28,7 +28,6 @@ var wsUpgrader = gorillaws.Upgrader{
}
// CheckStreamHandler is the WebSocket handler for slot availability checking.
// It is invoked via the CheckStream controller method.
// Query params: as_possible=true, preemption=true
func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
wfID := strings.TrimSuffix(
@@ -50,7 +49,7 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
}
watchedPeers, err := infrastructure.GetWorkflowPeerIDs(wfID, req)
fmt.Println("Here my watched peers involved in workflow", watchedPeers)
fmt.Println("Watched peers for workflow", wfID, ":", watchedPeers)
if err != nil {
http.Error(w, `{"code":404,"error":"`+err.Error()+`"}`, http.StatusNotFound)
return
@@ -73,17 +72,14 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
executionsID := uuid.New().String()
ownedPeers := infrastructure.RequestPlannerRefresh(watchedPeers, executionsID)
selfID, err := oclib.GetMySelf()
if err != nil || selfID == nil {
logger.Err(err).Msg(err.Error())
self, err := oclib.GetMySelf()
if err != nil || self == nil {
logger.Err(err).Msg("could not resolve self peer")
conn.Close()
return
}
selfPeerID := ""
if selfID != nil {
selfPeerID = selfID.PeerID
}
selfPeerID := self.PeerID
// scheduled=true once bookings/purchases/exec have been created for this session.
scheduled := false
confirmed := false
@@ -91,37 +87,33 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
conn.Close()
plannerUnsub()
wfUnsub()
infrastructure.ReleaseRefreshOwnership(ownedPeers, executionsID)
if !confirmed {
infrastructure.CleanupSession(selfID, executionsID, selfID, req)
infrastructure.CleanupSession(executionsID, req)
}
}()
// pushCheck runs an availability check and sends the result to the client.
// If reschedule=true and the slot is available, it also creates/updates
// bookings, purchases and the execution draft for this session.
pushCheck := func(reschedule bool) error {
result, checkErr := ws.Check(wfID, asap, preemption, req)
if checkErr != nil {
return checkErr
}
if result.Available && reschedule {
// Sync the resolved start/end back to ws so that UpsertSessionDrafts
// creates bookings/purchases with the actual scheduled dates (not the
// raw client value which may be zero or pre-asapBuffer).
ws.Start = result.Start
if result.End != nil {
ws.End = result.End
}
ws.UpsertSessionDrafts(wfID, executionsID, selfID, req)
_, _, execs, purchases, bookings, err := ws.GetBuyAndBook(wfID, req)
if err != nil {
return err
}
infrastructure.UpsertSessionDrafts(executionsID, execs, purchases, bookings, req)
scheduled = true
}
result.SchedulingID = executionsID
return conn.WriteJSON(result)
}
// Initial check + schedule.
if err := pushCheck(true); err != nil {
return
}
@@ -148,10 +140,8 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
select {
case updated := <-updateCh:
if updated.Confirm {
// Confirm: flip bookings/purchases to IsDraft=false, then let
// the considers mechanism transition exec to IsDraft=false.
ws.UUID = executionsID
_, _, _, schedErr := ws.Schedules(wfID, req)
_, _, _, schedErr := infrastructure.Schedule(&ws, wfID, req)
if schedErr != nil {
_ = conn.WriteJSON(map[string]interface{}{
"error": schedErr.Error(),
@@ -172,7 +162,7 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
!reflect.DeepEqual(updated.SelectedPartnerships, ws.SelectedPartnerships) ||
!reflect.DeepEqual(updated.SelectedBuyings, ws.SelectedBuyings) ||
!reflect.DeepEqual(updated.SelectedStrategies, ws.SelectedStrategies)
infrastructure.CleanupSession(selfID, executionsID, selfID, req)
infrastructure.CleanupSession(executionsID, req)
ws = updated
if err := pushCheck(changed || !scheduled); err != nil {
return
@@ -180,9 +170,9 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
case remotePeerID := <-plannerCh:
if remotePeerID == selfPeerID {
// Our own planner updated (caused by our local booking store).
// Just resend the current availability result without rescheduling
// to avoid an infinite loop.
if scheduled {
continue
}
result, checkErr := ws.Check(wfID, asap, preemption, req)
if checkErr == nil {
result.SchedulingID = executionsID
@@ -190,24 +180,9 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
}
continue
}
// A remote peer's planner changed. Re-check; if our slot is now
// taken and we were already scheduled, reschedule at the new slot.
result, checkErr := ws.Check(wfID, asap, preemption, req)
if checkErr != nil {
if err := pushCheck(scheduled); err != nil {
return
}
if !result.Available && scheduled {
// Move to the next free slot and reschedule.
if result.NextSlot != nil {
ws.Start = *result.NextSlot
}
if err := pushCheck(true); err != nil {
return
}
} else {
result.SchedulingID = executionsID
_ = conn.WriteJSON(result)
}
case <-wfCh:
if newPeers, err := infrastructure.GetWorkflowPeerIDs(wfID, req); err == nil {
@@ -241,8 +216,7 @@ func (o *WorkflowSchedulerController) UnSchedule() {
Groups: groups,
Admin: true,
}
selfID, _ := oclib.GetMySelf()
if err := infrastructure.UnscheduleExecution(executionID, selfID, req); err != nil {
if err := infrastructure.UnscheduleExecution(executionID, req); err != nil {
o.Data["json"] = map[string]interface{}{"code": 404, "error": err.Error()}
} else {
o.Data["json"] = map[string]interface{}{"code": 200, "error": ""}
@@ -251,7 +225,7 @@ func (o *WorkflowSchedulerController) UnSchedule() {
}
// @Title SearchScheduledDraftOrder
// @Description schedule workflow
// @Description search draft order for a workflow
// @Param id path string true "id execution"
// @Success 200 {workspace} models.workspace
// @router /:id/order [get]
@@ -265,7 +239,5 @@ func (o *WorkflowSchedulerController) SearchScheduledDraftOrder() {
},
}
o.Data["json"] = oclib.NewRequestAdmin(orderCollection, nil).Search(filter, "", true)
//o.Data["json"] = oclib.NewRequest(orderCollection, user, peerID, groups, nil).Search(filter, "", true)
o.ServeJSON()
}