Add verification
This commit is contained in:
68
controllers/execution_verification.go
Normal file
68
controllers/execution_verification.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"oc-scheduler/infrastructure"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
"cloud.o-forge.io/core/oc-lib/models/execution_verification"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/native_tools"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// Operations about workspace
|
||||
type ExecutionVerificationController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// @Title GetAll
|
||||
// @Description find verification by id
|
||||
// @Param is_draft query string false "draft wished"
|
||||
// @Success 200 {booking} models.booking
|
||||
// @router / [get]
|
||||
func (o *ExecutionVerificationController) GetAll() {
|
||||
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
||||
isDraft := o.Ctx.Input.Query("is_draft")
|
||||
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.EXECUTION_VERIFICATION), user, peerID, groups, nil).LoadAll(isDraft == "true")
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Get
|
||||
// @Description find verification by id
|
||||
// @Param id path string true "the id you want to get"
|
||||
// @Success 200 {booking} models.booking
|
||||
// @router /:id [get]
|
||||
func (o *ExecutionVerificationController) Get() {
|
||||
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
||||
id := o.Ctx.Input.Param(":id")
|
||||
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.EXECUTION_VERIFICATION), user, peerID, groups, nil).LoadOne(id)
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Update
|
||||
// @Description create computes
|
||||
// @Param id path string true "the compute id you want to get"
|
||||
// @Param body body models.compute true "The compute content"
|
||||
// @Success 200 {compute} models.compute
|
||||
// @router /:id [put]
|
||||
func (o *ExecutionVerificationController) Put() {
|
||||
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
||||
// store and return Id or post with UUID
|
||||
var res map[string]interface{}
|
||||
id := o.Ctx.Input.Param(":id")
|
||||
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
|
||||
data := oclib.NewRequest(oclib.LibDataEnum(oclib.EXECUTION_VERIFICATION), user, peerID, groups, nil).UpdateOne(res, id)
|
||||
if data.Err == "" && data.Data != nil && data.Data.(*execution_verification.ExecutionVerification).Validate {
|
||||
data, _ := json.Marshal(&native_tools.WorkflowEventParams{
|
||||
WorkflowResourceID: data.Data.(*execution_verification.ExecutionVerification).WorkflowID,
|
||||
})
|
||||
infrastructure.EmitNATS(peerID, tools.PropalgationMessage{
|
||||
Action: tools.PubSubAction(tools.WORKFLOW_EVENT),
|
||||
DataType: tools.EXECUTION_VERIFICATION.EnumIndex(),
|
||||
Payload: data,
|
||||
})
|
||||
}
|
||||
o.Data["json"] = data
|
||||
o.ServeJSON()
|
||||
}
|
||||
@@ -39,18 +39,8 @@ func (o *WorkflowSchedulerController) Schedule() {
|
||||
var resp *infrastructure.WorkflowSchedule
|
||||
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &resp)
|
||||
|
||||
caller := tools.NewHTTPCaller(map[tools.DataType]map[tools.METHOD]string{ // paths to call other OC services
|
||||
tools.PEER: {
|
||||
tools.POST: "/status/",
|
||||
},
|
||||
tools.BOOKING: {
|
||||
tools.GET: "/booking/check/:id/:start_date/:end_date",
|
||||
tools.POST: "/booking/",
|
||||
},
|
||||
})
|
||||
|
||||
logger.Info().Msg("Booking for " + wfId)
|
||||
req := oclib.NewRequestAdmin(collection, caller)
|
||||
req := oclib.NewRequestAdmin(collection, nil)
|
||||
// req := oclib.NewRequest(collection, user, peerID, groups, caller)
|
||||
resp.UUID = uuid.New().String()
|
||||
fmt.Println(user, peerID, groups)
|
||||
@@ -58,7 +48,7 @@ func (o *WorkflowSchedulerController) Schedule() {
|
||||
Username: user,
|
||||
PeerID: peerID,
|
||||
Groups: groups,
|
||||
Caller: caller,
|
||||
Caller: nil,
|
||||
Admin: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -87,13 +77,24 @@ var wsUpgrader = gorillaws.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
// CheckStreamHandler is a plain http.HandlerFunc (registered via beego.Handler
|
||||
// to avoid Beego's WriteHeader interference with the WebSocket upgrade).
|
||||
// Path: /oc/:id/check → parts = ["", "oc", "<id>", "check"]
|
||||
// @Title CheckStream
|
||||
// @Description WebSocket stream for slot availability checking.
|
||||
// @Param id path string true "workflow id"
|
||||
// @Param as_possible query bool false "search from now"
|
||||
// @Param preemption query bool false "force-valid, surface warnings"
|
||||
// @router /:id/check [get]
|
||||
func (o *WorkflowSchedulerController) CheckStream() {
|
||||
CheckStreamHandler(o.Ctx.ResponseWriter, o.Ctx.Request)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
parts := strings.Split(strings.TrimSuffix(r.URL.Path, "/"), "/")
|
||||
wfID := parts[len(parts)-2] // second-to-last segment
|
||||
wfID := strings.TrimSuffix(
|
||||
strings.TrimPrefix(r.URL.Path, "/oc/"),
|
||||
"/check",
|
||||
)
|
||||
|
||||
q := r.URL.Query()
|
||||
asap := q.Get("as_possible") == "true"
|
||||
@@ -136,20 +137,23 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
|
||||
plannerCh, plannerUnsub := infrastructure.SubscribePlannerUpdates(watchedPeers)
|
||||
wfCh, wfUnsub := infrastructure.SubscribeWorkflowUpdates(wfID)
|
||||
|
||||
// Cleanup on exit: cancel subscriptions, evict planner cache entries,
|
||||
// signal PB_CLOSE_PLANNER on NATS for each peer that was being watched.
|
||||
// Unique ID for this check session — used to track refresh ownership.
|
||||
sessionID := uuid.New().String()
|
||||
|
||||
// Request a fresh planner snapshot for every concerned peer.
|
||||
// The first session to claim a peer becomes its refresh owner; others skip
|
||||
// the duplicate PB_PLANNER emission. ownedPeers grows if the workflow's
|
||||
// peer list changes (wfCh).
|
||||
ownedPeers := infrastructure.RequestPlannerRefresh(watchedPeers, sessionID)
|
||||
|
||||
// Cleanup on exit (clean or forced): release refresh ownership for the
|
||||
// peers this session claimed, which resets Refreshing state and emits
|
||||
// PB_CLOSE_PLANNER so oc-discovery stops the planner stream.
|
||||
defer func() {
|
||||
conn.Close()
|
||||
plannerUnsub()
|
||||
wfUnsub()
|
||||
for _, peer := range watchedPeers {
|
||||
if b, err := json.Marshal(map[string]interface{}{"peer_id": peer}); err == nil {
|
||||
infrastructure.EmitNATS(peer, tools.PropalgationMessage{
|
||||
Action: tools.PB_CLOSE_PLANNER,
|
||||
Payload: b,
|
||||
})
|
||||
}
|
||||
}
|
||||
infrastructure.ReleaseRefreshOwnership(ownedPeers, sessionID)
|
||||
}()
|
||||
|
||||
push := func() error {
|
||||
@@ -166,20 +170,38 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Detect client-side close in a separate goroutine.
|
||||
// Read loop: detect client-side close and parse schedule parameter
|
||||
// updates (date changes, booking mode changes, …) sent by the client.
|
||||
updateCh := make(chan infrastructure.WorkflowSchedule, 1)
|
||||
closeCh := make(chan struct{})
|
||||
go func() {
|
||||
defer close(closeCh)
|
||||
for {
|
||||
if _, _, err := conn.ReadMessage(); err != nil {
|
||||
var updated infrastructure.WorkflowSchedule
|
||||
if err := conn.ReadJSON(&updated); err != nil {
|
||||
// Connection closed or unrecoverable read error.
|
||||
return
|
||||
}
|
||||
// Drop the oldest pending update if the consumer hasn't caught up.
|
||||
select {
|
||||
case updateCh <- updated:
|
||||
default:
|
||||
<-updateCh
|
||||
updateCh <- updated
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Stream loop.
|
||||
for {
|
||||
select {
|
||||
case updated := <-updateCh:
|
||||
// The client changed the requested date/params: adopt the new
|
||||
// schedule and re-run the check immediately.
|
||||
ws = updated
|
||||
if err := push(); err != nil {
|
||||
return
|
||||
}
|
||||
case <-wfCh:
|
||||
// The workflow was modified: refresh the peer list and re-subscribe
|
||||
// so the stream watches the correct set of planners going forward.
|
||||
@@ -187,11 +209,15 @@ func CheckStreamHandler(w http.ResponseWriter, r *http.Request) {
|
||||
plannerUnsub()
|
||||
watchedPeers = newPeers
|
||||
plannerCh, plannerUnsub = infrastructure.SubscribePlannerUpdates(newPeers)
|
||||
// Claim refresh ownership for any newly added peers.
|
||||
newOwned := infrastructure.RequestPlannerRefresh(newPeers, sessionID)
|
||||
ownedPeers = append(ownedPeers, newOwned...)
|
||||
}
|
||||
if err := push(); err != nil {
|
||||
return
|
||||
}
|
||||
case <-plannerCh:
|
||||
// A planner snapshot arrived (or was evicted): re-evaluate.
|
||||
if err := push(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user