2023-04-08 13:58:47 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-08 16:51:34 -06:00
|
|
|
"log"
|
2023-04-08 13:58:47 -06:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
graphql "github.com/graph-gophers/graphql-go"
|
|
|
|
"github.com/google/uuid"
|
2023-05-08 21:42:33 -06:00
|
|
|
"reflect"
|
2023-04-08 13:58:47 -06:00
|
|
|
)
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// Generate a random graphql id
|
2023-04-08 13:58:47 -06:00
|
|
|
func gql_randid() graphql.ID{
|
|
|
|
uuid_str := uuid.New().String()
|
|
|
|
return graphql.ID(uuid_str)
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// GraphNode is the interface common to both DAG nodes and Event tree nodes
|
2023-04-08 13:58:47 -06:00
|
|
|
type GraphNode interface {
|
|
|
|
Name() string
|
|
|
|
Description() string
|
|
|
|
ID() graphql.ID
|
|
|
|
UpdateListeners() error
|
|
|
|
UpdateChannel() chan error
|
|
|
|
Update() error
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// BaseNode is the most basic implementation of the GraphNode interface
|
|
|
|
// It is used to implement functions common to Events and Resources
|
2023-04-08 13:58:47 -06:00
|
|
|
type BaseNode struct {
|
|
|
|
name string
|
|
|
|
description string
|
|
|
|
id graphql.ID
|
|
|
|
listeners []chan error
|
|
|
|
listeners_lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node * BaseNode) Name() string {
|
|
|
|
return node.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node * BaseNode) Description() string {
|
|
|
|
return node.description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node * BaseNode) ID() graphql.ID {
|
|
|
|
return node.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new listener channel for the node, add it to the nodes listener list, and return the new channel
|
|
|
|
func (node * BaseNode) UpdateChannel() chan error {
|
|
|
|
new_listener := make(chan error, 1)
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
node.listeners = append(node.listeners, new_listener)
|
|
|
|
node.listeners_lock.Unlock()
|
|
|
|
return new_listener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the update to listener channels
|
|
|
|
func (node * BaseNode) UpdateListeners() error {
|
|
|
|
closed_listeners := []int{}
|
|
|
|
listeners_closed := false
|
|
|
|
|
|
|
|
// Send each listener nil to signal it to check for new content
|
|
|
|
// if the first attempt to send it fails close the listener
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
for i, listener := range node.listeners {
|
|
|
|
select {
|
|
|
|
case listener <- nil:
|
|
|
|
default:
|
|
|
|
close(listener)
|
|
|
|
closed_listeners = append(closed_listeners, i)
|
|
|
|
listeners_closed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any listeners have been closed, loop over the listeners
|
|
|
|
// Add listeners to the "remaining" list if i insn't in closed_listeners
|
|
|
|
if listeners_closed == true {
|
|
|
|
remaining_listeners := []chan error{}
|
|
|
|
for i, listener := range node.listeners {
|
|
|
|
listener_closed := false
|
|
|
|
for _, index := range closed_listeners {
|
|
|
|
if index == i {
|
|
|
|
listener_closed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if listener_closed == false {
|
|
|
|
remaining_listeners = append(remaining_listeners, listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.listeners = remaining_listeners
|
|
|
|
}
|
|
|
|
node.listeners_lock.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// Basic implementation must be overwritten to do anything useful
|
2023-04-08 13:58:47 -06:00
|
|
|
func (node * BaseNode) Update() error {
|
2023-05-08 21:42:33 -06:00
|
|
|
return errors.New("Cannot Update a BaseNode")
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resources propagate update up to multiple parents, and not downwards
|
|
|
|
// (subscriber to team won't get update to alliance, but subscriber to alliance will get update to team)
|
|
|
|
func (resource * BaseResource) Update() error {
|
|
|
|
err := resource.UpdateListeners()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, parent := range resource.Parents() {
|
|
|
|
err := parent.Update()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 16:51:34 -06:00
|
|
|
if resource.lock_holder != nil {
|
|
|
|
resource.lock_holder.Update()
|
|
|
|
}
|
|
|
|
|
2023-04-08 13:58:47 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the events listeners, and notify the parent to do the same
|
|
|
|
func (event * BaseEvent) Update() error {
|
|
|
|
err := event.UpdateListeners()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.parent != nil{
|
|
|
|
return event.parent.Update()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// Resource is the interface that DAG nodes are made from
|
2023-04-08 13:58:47 -06:00
|
|
|
type Resource interface {
|
|
|
|
GraphNode
|
|
|
|
AddParent(parent Resource) error
|
|
|
|
Children() []Resource
|
|
|
|
Parents() []Resource
|
2023-04-08 16:51:34 -06:00
|
|
|
Lock(event Event) error
|
2023-04-08 15:47:51 -06:00
|
|
|
Unlock() error
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// BaseResource is the most basic resource that can exist in the DAG
|
|
|
|
// It holds a single state variable, which contains a pointer to the event that is locking it
|
2023-04-08 13:58:47 -06:00
|
|
|
type BaseResource struct {
|
|
|
|
BaseNode
|
|
|
|
parents []Resource
|
|
|
|
children []Resource
|
2023-04-08 16:51:34 -06:00
|
|
|
lock_holder Event
|
2023-04-08 15:47:51 -06:00
|
|
|
state_lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the state mutex and check the state, if unlocked continue to hold the mutex while doing the same for children
|
|
|
|
// When the bottom of a tree is reached(no more children) go back up and set the lock state
|
2023-04-08 16:51:34 -06:00
|
|
|
func (resource * BaseResource) Lock(event Event) error {
|
2023-04-08 15:47:51 -06:00
|
|
|
var err error = nil
|
2023-04-08 15:51:42 -06:00
|
|
|
locked := false
|
|
|
|
|
2023-04-08 15:47:51 -06:00
|
|
|
resource.state_lock.Lock()
|
2023-04-08 16:51:34 -06:00
|
|
|
if resource.lock_holder != nil {
|
2023-04-08 15:47:51 -06:00
|
|
|
err = errors.New("Resource already locked")
|
|
|
|
} else {
|
|
|
|
all_children_locked := true
|
|
|
|
for _, child := range resource.Children() {
|
2023-04-08 16:51:34 -06:00
|
|
|
err = child.Lock(event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err != nil {
|
|
|
|
all_children_locked = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_children_locked == true {
|
2023-04-08 16:51:34 -06:00
|
|
|
resource.lock_holder = event
|
2023-04-08 15:51:42 -06:00
|
|
|
locked = true
|
2023-04-08 15:47:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resource.state_lock.Unlock()
|
2023-04-08 15:51:42 -06:00
|
|
|
|
|
|
|
if locked == true {
|
|
|
|
resource.Update()
|
|
|
|
}
|
|
|
|
|
2023-04-08 15:47:51 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse through children, unlocking until no more children
|
|
|
|
func (resource * BaseResource) Unlock() error {
|
|
|
|
var err error = nil
|
2023-04-08 15:51:42 -06:00
|
|
|
unlocked := false
|
|
|
|
|
2023-04-08 15:47:51 -06:00
|
|
|
resource.state_lock.Lock()
|
2023-04-08 16:51:34 -06:00
|
|
|
if resource.lock_holder == nil {
|
2023-04-08 15:47:51 -06:00
|
|
|
err = errors.New("Resource already unlocked")
|
|
|
|
} else {
|
|
|
|
all_children_unlocked := true
|
|
|
|
for _, child := range resource.Children() {
|
|
|
|
err = child.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
all_children_unlocked = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_children_unlocked == true{
|
2023-04-08 16:51:34 -06:00
|
|
|
resource.lock_holder = nil
|
2023-04-08 15:51:42 -06:00
|
|
|
unlocked = true
|
2023-04-08 15:47:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resource.state_lock.Unlock()
|
2023-04-08 15:51:42 -06:00
|
|
|
|
|
|
|
if unlocked == true {
|
|
|
|
resource.Update()
|
|
|
|
}
|
|
|
|
|
2023-04-08 15:47:51 -06:00
|
|
|
return err
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Children() []Resource {
|
|
|
|
return resource.children
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Parents() []Resource {
|
|
|
|
return resource.parents
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// Add a parent to a DAG node
|
2023-04-08 13:58:47 -06:00
|
|
|
func (resource * BaseResource) AddParent(parent Resource) error {
|
|
|
|
// Don't add self as parent
|
|
|
|
if parent.ID() == resource.ID() {
|
2023-04-08 15:23:40 -06:00
|
|
|
error_str := fmt.Sprintf("Will not add %s as parent of itself", parent.ID())
|
2023-04-08 13:58:47 -06:00
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't add parent if it's already a parent
|
|
|
|
for _, p := range resource.parents {
|
|
|
|
if p.ID() == parent.ID() {
|
|
|
|
error_str := fmt.Sprintf("%s is already a parent of %s, will not double-bond", p.ID(), resource.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the parent
|
|
|
|
resource.parents = append(resource.parents, parent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
type EventInfo interface {}
|
2023-04-08 15:47:51 -06:00
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
type BaseEventInfo struct {
|
|
|
|
EventInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventQueueInfo struct {
|
|
|
|
EventInfo
|
|
|
|
priority int
|
|
|
|
}
|
2023-04-08 15:47:51 -06:00
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
func (info * EventQueueInfo) Priority() int {
|
|
|
|
return info.priority
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event is the interface that event tree nodes must implement
|
2023-04-08 13:58:47 -06:00
|
|
|
type Event interface {
|
|
|
|
GraphNode
|
|
|
|
Children() []Event
|
2023-05-08 21:42:33 -06:00
|
|
|
ChildInfo() []EventInfo
|
|
|
|
ChildInfoType() reflect.Type
|
2023-04-08 13:58:47 -06:00
|
|
|
Parent() Event
|
|
|
|
RegisterParent(parent Event) error
|
|
|
|
RequiredResources() []Resource
|
|
|
|
CreatedResources() []Resource
|
2023-05-08 21:42:33 -06:00
|
|
|
AddChild(child Event, info EventInfo) error
|
2023-04-08 13:58:47 -06:00
|
|
|
FindChild(id graphql.ID) Event
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// BaseEvent is the most basic event that can exist in the event tree.
|
|
|
|
// On start it automatically transitions to completion.
|
|
|
|
// It can optionally require events, which will all need to be locked to start it
|
|
|
|
// It can optionally create resources, which will be locked by default and unlocked on completion
|
|
|
|
// This node by itself doesn't implement any special behaviours for children, so they will be ignored.
|
|
|
|
// When starter, this event automatically transitions to completion and unlocks all it's resources(including created)
|
2023-04-08 13:58:47 -06:00
|
|
|
type BaseEvent struct {
|
|
|
|
BaseNode
|
|
|
|
locked_resources []Resource
|
|
|
|
created_resources []Resource
|
|
|
|
required_resources []Resource
|
|
|
|
children []Event
|
2023-05-08 21:42:33 -06:00
|
|
|
child_info []EventInfo
|
|
|
|
child_info_type reflect.Type
|
2023-04-08 13:58:47 -06:00
|
|
|
parent Event
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// EventQueue is a basic event that can have children.
|
|
|
|
// On start, it attempts to start it's children from the highest 'priority'
|
|
|
|
type EventQueue struct {
|
|
|
|
BaseEvent
|
|
|
|
}
|
|
|
|
|
2023-04-08 15:23:40 -06:00
|
|
|
func NewResource(name string, description string, children []Resource) * BaseResource {
|
|
|
|
resource := &BaseResource{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
id: gql_randid(),
|
|
|
|
listeners: []chan error{},
|
|
|
|
},
|
|
|
|
parents: []Resource{},
|
|
|
|
children: children,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEvent(name string, description string, required_resources []Resource) * BaseEvent {
|
2023-04-08 13:58:47 -06:00
|
|
|
event := &BaseEvent{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
id: gql_randid(),
|
|
|
|
listeners: []chan error{},
|
|
|
|
},
|
|
|
|
parent: nil,
|
2023-04-08 15:23:40 -06:00
|
|
|
children: []Event{},
|
2023-05-08 21:42:33 -06:00
|
|
|
child_info: []EventInfo{},
|
|
|
|
child_info_type: reflect.TypeOf((*BaseEventInfo)(nil)).Elem(),
|
2023-04-08 13:58:47 -06:00
|
|
|
locked_resources: []Resource{},
|
|
|
|
created_resources: []Resource{},
|
|
|
|
required_resources: required_resources,
|
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
func NewEventQueue(name string, description string, required_resources []Resource) * EventQueue {
|
|
|
|
queue := &EventQueue{
|
|
|
|
BaseEvent: BaseEvent{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
id: gql_randid(),
|
|
|
|
listeners: []chan error{},
|
|
|
|
},
|
|
|
|
parent: nil,
|
|
|
|
children: []Event{},
|
|
|
|
child_info: []EventInfo{},
|
|
|
|
child_info_type: reflect.TypeOf((*EventQueueInfo)(nil)).Elem(),
|
|
|
|
locked_resources: []Resource{},
|
|
|
|
created_resources: []Resource{},
|
|
|
|
required_resources: required_resources,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue
|
|
|
|
}
|
|
|
|
|
2023-04-08 13:58:47 -06:00
|
|
|
// Store the nodes parent for upwards propagation of changes
|
|
|
|
func (event * BaseEvent) RegisterParent(parent Event) error{
|
|
|
|
if event.parent != nil {
|
|
|
|
return errors.New("Parent already registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
event.parent = parent
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (event * BaseEvent) Parent() Event {
|
|
|
|
return event.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (event * BaseEvent) RequiredResources() []Resource {
|
|
|
|
return event.required_resources
|
|
|
|
}
|
|
|
|
|
|
|
|
func (event * BaseEvent) CreatedResources() []Resource {
|
|
|
|
return event.created_resources
|
|
|
|
}
|
|
|
|
|
|
|
|
func (event * BaseEvent) Children() []Event {
|
|
|
|
return event.children
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
func (event * BaseEvent) ChildInfo() []EventInfo {
|
|
|
|
return event.child_info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (event * BaseEvent) ChildInfoType() reflect.Type {
|
|
|
|
return event.child_info_type
|
|
|
|
}
|
|
|
|
|
2023-04-08 13:58:47 -06:00
|
|
|
func (event * BaseEvent) FindChild(id graphql.ID) Event {
|
|
|
|
if id == event.ID() {
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range event.Children() {
|
|
|
|
result := child.FindChild(id)
|
|
|
|
if result != nil {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
func (event * BaseEvent) AddChild(child Event, info EventInfo) error {
|
|
|
|
if info == nil {
|
|
|
|
return errors.New("info cannot be nil in AddChild")
|
|
|
|
}
|
|
|
|
|
|
|
|
child_info_type := reflect.TypeOf(info).Elem()
|
|
|
|
event_info_type := event.ChildInfoType()
|
|
|
|
if child_info_type != event_info_type {
|
|
|
|
error_str := fmt.Sprintf("BaseEvent only supports child_info of type %s, not %s", child_info_type.Name(), event_info_type.Name())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
2023-04-08 13:58:47 -06:00
|
|
|
err := child.RegisterParent(event)
|
|
|
|
if err != nil {
|
|
|
|
error_str := fmt.Sprintf("Failed to register %s as a parent of %s, cancelling AddChild", event.ID(), child.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
event.children = append(event.children, child)
|
2023-05-08 21:42:33 -06:00
|
|
|
event.child_info = append(event.child_info, info)
|
2023-04-08 13:58:47 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventManager struct {
|
|
|
|
dag_nodes map[graphql.ID]Resource
|
|
|
|
root_event Event
|
|
|
|
}
|
|
|
|
|
2023-04-08 16:51:34 -06:00
|
|
|
// root_event's requirements must be in dag_nodes, and dag_nodes must be ordered by dependency(no children first)
|
|
|
|
func NewEventManager(root_event Event, dag_nodes []Resource) * EventManager {
|
|
|
|
|
|
|
|
manager := &EventManager{
|
2023-04-08 13:58:47 -06:00
|
|
|
dag_nodes: map[graphql.ID]Resource{},
|
|
|
|
root_event: nil,
|
|
|
|
}
|
2023-04-08 16:51:34 -06:00
|
|
|
|
|
|
|
// Construct the DAG
|
|
|
|
for _, resource := range dag_nodes {
|
|
|
|
err := manager.AddResource(resource)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to add %s to EventManager: %s", resource.ID(), err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
manager.AddEvent(nil, root_event, nil)
|
2023-04-08 16:51:34 -06:00
|
|
|
|
|
|
|
return manager;
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-04-08 15:23:40 -06:00
|
|
|
func (manager * EventManager) FindResource(id graphql.ID) Resource {
|
|
|
|
resource, exists := manager.dag_nodes[id]
|
|
|
|
if exists == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager * EventManager) FindEvent(id graphql.ID) Event {
|
|
|
|
event := manager.root_event.FindChild(id)
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2023-04-08 13:58:47 -06:00
|
|
|
func (manager * EventManager) AddResource(resource Resource) error {
|
|
|
|
_, exists := manager.dag_nodes[resource.ID()]
|
|
|
|
if exists == true {
|
2023-04-08 15:23:40 -06:00
|
|
|
error_str := fmt.Sprintf("%s is already in the resource DAG, cannot add again", resource.ID())
|
2023-04-08 13:58:47 -06:00
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range resource.Children() {
|
|
|
|
_, exists := manager.dag_nodes[child.ID()]
|
|
|
|
if exists == false {
|
|
|
|
error_str := fmt.Sprintf("%s is not in the resource DAG, cannot add %s to DAG", child.ID(), resource.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
manager.dag_nodes[resource.ID()] = resource
|
|
|
|
for _, child := range resource.Children() {
|
|
|
|
child.AddParent(resource)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the node doesn't already exist in the tree
|
|
|
|
// Check the the selected parent exists in the tree
|
|
|
|
// Check that required resources exist in the DAG
|
|
|
|
// Check that created resources don't exist in the DAG
|
|
|
|
// Add resources created by the event to the DAG
|
|
|
|
// Add child to parent
|
2023-05-08 21:42:33 -06:00
|
|
|
func (manager * EventManager) AddEvent(parent Event, child Event, info EventInfo) error {
|
2023-04-08 16:51:34 -06:00
|
|
|
if child == nil {
|
|
|
|
return errors.New("Cannot add nil Event to EventManager")
|
|
|
|
} else if len(child.Children()) != 0 {
|
|
|
|
return errors.New("Adding events recursively not implemented")
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range child.RequiredResources() {
|
|
|
|
_, exists := manager.dag_nodes[resource.ID()]
|
|
|
|
if exists == false {
|
|
|
|
error_str := fmt.Sprintf("Required resource %s not in DAG, cannot add event %s", resource.ID(), child.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range child.CreatedResources() {
|
|
|
|
_, exists := manager.dag_nodes[resource.ID()]
|
|
|
|
if exists == true {
|
|
|
|
error_str := fmt.Sprintf("Created resource %s already exists in DAG, cannot add event %s", resource.ID(), child.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 16:51:34 -06:00
|
|
|
if manager.root_event == nil && parent != nil {
|
|
|
|
error_str := fmt.Sprintf("EventManager has no root, so can't add event to parent")
|
|
|
|
return errors.New(error_str)
|
|
|
|
} else if manager.root_event != nil && parent == nil {
|
|
|
|
// TODO
|
|
|
|
return errors.New("Replacing root event not implemented")
|
|
|
|
} else if manager.root_event == nil && parent == nil {
|
|
|
|
manager.root_event = child
|
2023-05-08 21:42:33 -06:00
|
|
|
return nil;
|
2023-04-08 16:51:34 -06:00
|
|
|
} else {
|
|
|
|
if manager.root_event.FindChild(parent.ID()) == nil {
|
|
|
|
error_str := fmt.Sprintf("Event %s is not present in the event tree, cannot add %s as child", parent.ID(), child.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
if manager.root_event.FindChild(child.ID()) != nil {
|
|
|
|
error_str := fmt.Sprintf("Event %s already exists in the event tree, can not add again", child.ID())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
return parent.AddChild(child, info)
|
2023-04-08 16:51:34 -06:00
|
|
|
}
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|