Refactor Oc-Sheduler
This commit is contained in:
23
infrastructure/nats/nats.go
Normal file
23
infrastructure/nats/nats.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"oc-scheduler/infrastructure/execution"
|
||||
"oc-scheduler/infrastructure/planner"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
// ListenNATS registers all NATS event handlers and starts listening.
|
||||
// Each handler is a thin router that delegates to the appropriate service.
|
||||
func ListenNATS() {
|
||||
tools.NewNATSCaller().ListenNats(map[tools.NATSMethod]func(tools.NATSResponse){
|
||||
tools.PLANNER_EXECUTION: planner.GetPlannerService().HandleStore,
|
||||
tools.CONSIDERS_EVENT: handleConsidersEvent,
|
||||
tools.REMOVE_RESOURCE: handleRemoveResource,
|
||||
tools.CREATE_RESOURCE: handleCreateResource,
|
||||
tools.CONFIRM_EVENT: handleConfirm,
|
||||
tools.WORKFLOW_STARTED_EVENT: execution.HandleWorkflowStarted,
|
||||
tools.WORKFLOW_STEP_DONE_EVENT: execution.HandleWorkflowStepDone,
|
||||
tools.WORKFLOW_DONE_EVENT: execution.HandleWorkflowDone,
|
||||
})
|
||||
}
|
||||
87
infrastructure/nats/nats_handlers.go
Normal file
87
infrastructure/nats/nats_handlers.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"oc-scheduler/infrastructure/execution"
|
||||
"oc-scheduler/infrastructure/planner"
|
||||
"oc-scheduler/infrastructure/scheduling_resources"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
// handleConfirm processes a CONFIRM_EVENT: sets IsDraft=false on the resource.
|
||||
func handleConfirm(resp tools.NATSResponse) {
|
||||
scheduling_resources.Confirm(string(resp.Payload), resp.Datatype)
|
||||
}
|
||||
|
||||
// handleConsidersEvent routes CONSIDERS_EVENT to the execution service.
|
||||
func handleConsidersEvent(resp tools.NATSResponse) {
|
||||
switch resp.Datatype {
|
||||
case tools.BOOKING, tools.PURCHASE_RESOURCE:
|
||||
execution.UpdateExecutionState(resp.Payload, resp.Datatype)
|
||||
case tools.WORKFLOW_EXECUTION:
|
||||
execution.ConfirmExecutionDrafts(resp.Payload)
|
||||
}
|
||||
}
|
||||
|
||||
// handleRemoveResource routes REMOVE_RESOURCE to the appropriate service.
|
||||
func handleRemoveResource(resp tools.NATSResponse) {
|
||||
adminReq := &tools.APIRequest{Admin: true}
|
||||
switch resp.Datatype {
|
||||
case tools.WORKFLOW:
|
||||
var wf workflow.Workflow
|
||||
if err := json.Unmarshal(resp.Payload, &wf); err != nil {
|
||||
return
|
||||
}
|
||||
planner.GetPlannerService().NotifyWorkflow(wf.GetID())
|
||||
case tools.BOOKING:
|
||||
var p scheduling_resources.RemoveResourcePayload
|
||||
if err := json.Unmarshal(resp.Payload, &p); err != nil {
|
||||
return
|
||||
}
|
||||
scheduling_resources.GetService().HandleRemoveBooking(p, adminReq)
|
||||
case tools.PURCHASE_RESOURCE:
|
||||
var p scheduling_resources.RemoveResourcePayload
|
||||
if err := json.Unmarshal(resp.Payload, &p); err != nil {
|
||||
return
|
||||
}
|
||||
scheduling_resources.GetService().HandleRemovePurchase(p, adminReq)
|
||||
}
|
||||
}
|
||||
|
||||
// handleCreateResource routes CREATE_RESOURCE to the appropriate service.
|
||||
func handleCreateResource(resp tools.NATSResponse) {
|
||||
adminReq := &tools.APIRequest{Admin: true}
|
||||
switch resp.Datatype {
|
||||
case tools.WORKFLOW:
|
||||
var wf workflow.Workflow
|
||||
if err := json.Unmarshal(resp.Payload, &wf); err != nil {
|
||||
return
|
||||
}
|
||||
planner.GetPlannerService().Broadcast(&wf)
|
||||
planner.GetPlannerService().NotifyWorkflow(wf.GetID())
|
||||
case tools.BOOKING:
|
||||
var bk booking.Booking
|
||||
if err := json.Unmarshal(resp.Payload, &bk); err != nil {
|
||||
return
|
||||
}
|
||||
needsConsiders := scheduling_resources.GetService().HandleCreateBooking(&bk, adminReq)
|
||||
if needsConsiders {
|
||||
payload, _ := json.Marshal(execution.ConsidersPayload{ID: bk.GetID()})
|
||||
execution.UpdateExecutionState(payload, tools.BOOKING)
|
||||
}
|
||||
case tools.PURCHASE_RESOURCE:
|
||||
var pr purchase_resource.PurchaseResource
|
||||
if err := json.Unmarshal(resp.Payload, &pr); err != nil {
|
||||
return
|
||||
}
|
||||
needsConsiders := scheduling_resources.GetService().HandleCreatePurchase(&pr, adminReq)
|
||||
if needsConsiders {
|
||||
payload, _ := json.Marshal(execution.ConsidersPayload{ID: pr.GetID()})
|
||||
execution.UpdateExecutionState(payload, tools.PURCHASE_RESOURCE)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user