Scheduler + Observe
This commit is contained in:
@@ -6,9 +6,13 @@ import (
|
||||
"oc-scheduler/infrastructure/planner"
|
||||
"oc-scheduler/infrastructure/scheduling_resources"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||
libutils "cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
@@ -49,6 +53,13 @@ func handleRemoveResource(resp tools.NATSResponse) {
|
||||
return
|
||||
}
|
||||
scheduling_resources.GetService().HandleRemovePurchase(p, adminReq)
|
||||
case tools.WORKFLOW_EXECUTION:
|
||||
var p scheduling_resources.RemoveResourcePayload
|
||||
if err := json.Unmarshal(resp.Payload, &p); err != nil || p.ID == "" {
|
||||
return
|
||||
}
|
||||
// DeleteOne calls GenericDeleteOne internally which fires NotifyChange.
|
||||
workflow_execution.NewAccessor(adminReq).DeleteOne(p.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +79,21 @@ func handleCreateResource(resp tools.NATSResponse) {
|
||||
if err := json.Unmarshal(resp.Payload, &bk); err != nil {
|
||||
return
|
||||
}
|
||||
if bk.FromNano != "" {
|
||||
access := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
|
||||
pp := access.LoadOne(bk.FromNano)
|
||||
if p := pp.ToPeer(); p == nil || p.Relation == peer.NANO {
|
||||
return
|
||||
}
|
||||
access = oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.BOOKING), nil)
|
||||
d := access.LoadOne(bk.GetID())
|
||||
if d.Data == nil {
|
||||
access.StoreOne(bk.Serialize(&bk))
|
||||
} else {
|
||||
access.UpdateOne(bk.Serialize(&bk), bk.GetID())
|
||||
}
|
||||
return
|
||||
}
|
||||
needsConsiders := scheduling_resources.GetService().HandleCreateBooking(&bk, adminReq)
|
||||
if needsConsiders {
|
||||
payload, _ := json.Marshal(execution.ConsidersPayload{ID: bk.GetID()})
|
||||
@@ -78,10 +104,43 @@ func handleCreateResource(resp tools.NATSResponse) {
|
||||
if err := json.Unmarshal(resp.Payload, &pr); err != nil {
|
||||
return
|
||||
}
|
||||
if pr.FromNano != "" {
|
||||
access := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
|
||||
pp := access.LoadOne(pr.FromNano)
|
||||
if p := pp.ToPeer(); p == nil || p.Relation == peer.NANO {
|
||||
return
|
||||
}
|
||||
access = oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PURCHASE_RESOURCE), nil)
|
||||
d := access.LoadOne(pr.GetID())
|
||||
if d.Data == nil {
|
||||
access.StoreOne(pr.Serialize(&pr))
|
||||
} else {
|
||||
access.UpdateOne(pr.Serialize(&pr), pr.GetID())
|
||||
}
|
||||
return
|
||||
}
|
||||
needsConsiders := scheduling_resources.GetService().HandleCreatePurchase(&pr, adminReq)
|
||||
if needsConsiders {
|
||||
payload, _ := json.Marshal(execution.ConsidersPayload{ID: pr.GetID()})
|
||||
execution.UpdateExecutionState(payload, tools.PURCHASE_RESOURCE)
|
||||
}
|
||||
case tools.WORKFLOW_EXECUTION:
|
||||
// Only propagate the state change onto an execution that oc-scheduler
|
||||
// already owns. Never create executions from an external NATS event:
|
||||
// creation is strictly oc-scheduler's responsibility (via the session
|
||||
// flow), and blindly calling StoreOne here would trigger
|
||||
// StoreDraftDefault (IsDraft=true, State=DRAFT), polluting the name-
|
||||
// uniqueness index and breaking the check stream's first draft creation.
|
||||
var update workflow_execution.WorkflowExecution
|
||||
if err := json.Unmarshal(resp.Payload, &update); err != nil || update.GetID() == "" {
|
||||
return
|
||||
}
|
||||
res, _, loadErr := workflow_execution.NewAccessor(adminReq).LoadOne(update.GetID())
|
||||
if loadErr != nil || res == nil {
|
||||
return
|
||||
}
|
||||
exec := res.(*workflow_execution.WorkflowExecution)
|
||||
exec.State = update.State
|
||||
libutils.GenericRawUpdateOne(exec, exec.GetID(), workflow_execution.NewAccessor(adminReq))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user