88 lines
2.9 KiB
Go
88 lines
2.9 KiB
Go
|
|
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|