Removed StartRootEvent, now run LockResource(root_event) then RunEvent(root_event)

graph-rework
noah metz 2023-06-20 20:48:48 -06:00
parent 41f28a2017
commit 5be9b9ac07
2 changed files with 41 additions and 29 deletions

@ -230,34 +230,17 @@ func LinkEvent(event Event, child Event, info EventInfo) error {
return nil
}
func StartRootEvent(event Event) error {
Log.Logf("event", "ROOT_EVEN_START")
func RunEvent(event Event) error {
Log.Logf("event", "EVENT_RUN: %s", event.Name())
err := LockResources(event)
if err != nil {
Log.Logf("event", "ROOT_EVENT_LOCK_ERR: %s", err)
return err
for _, resource := range(event.Resources()) {
if resource.Owner() == nil {
return fmt.Errorf("EVENT_RUN_RESOURCE_NOT_LOCKED: %s, %s", event.Name(), resource.Name())
} else if resource.Owner().ID() != event.ID() {
return fmt.Errorf("EVENT_RUN_RESOURCE_ALREADY_LOCKED: %s, %s, %s", event.Name(), resource.Name(), resource.Owner().Name())
}
err = RunEvent(event)
if err != nil {
Log.Logf("event", "ROOT_EVENT_RUNE_ERR: %s", err)
return err
}
err = FinishEvent(event)
if err != nil {
Log.Logf("event", "ROOT_EVENT_FINISH_ERR: %s", err)
return err
}
Log.Logf("event", "ROOT_EVENT_DONE")
return nil
}
func RunEvent(event Event) error {
Log.Logf("event", "EVENT_RUN: %s", event.Name())
SendUpdate(event, NewSignal(event, "event_start"))
next_action := "start"
var err error = nil
@ -275,6 +258,12 @@ func RunEvent(event Event) error {
}
}
err = FinishEvent(event)
if err != nil {
Log.Logf("event", "EVENT_RUN_FINISH_ERR: %s", err)
return err
}
Log.Logf("event", "EVENT_RUN_DONE: %s", event.Name())
return nil
@ -525,7 +514,6 @@ func NewEventQueue(name string, description string, resources []Resource) (* Eve
Log.Logf("event", "EVENT_ERROR: %s", err)
}
info.state = "done"
FinishEvent(event)
}(event, info, queue)
}
}

@ -293,7 +293,12 @@ func TestStartBaseEvent(t * testing.T) {
t.Fatal("r is not owned by event_1")
}
err := StartRootEvent(event_1)
err := LockResources(event_1)
if err != nil {
t.Fatal(err)
}
err = RunEvent(event_1)
if err != nil {
t.Fatal(err)
}
@ -330,7 +335,11 @@ func TestAbortEventQueue(t * testing.T) {
SendUpdate(root_event, abort_signal)
}()
err = StartRootEvent(root_event)
err = LockResources(root_event)
if err != nil {
t.Fatal(err)
}
err = RunEvent(root_event)
if err == nil {
t.Fatal("root_event completed without error")
}
@ -340,6 +349,16 @@ func TestAbortEventQueue(t * testing.T) {
}
}
func TestStartWithoutLocking(t * testing.T) {
test_resource, _ := NewResource("test_resource", "", []Resource{})
root_event, _ := NewEvent("root_event", "", []Resource{test_resource})
err := RunEvent(root_event)
if err == nil {
t.Fatal("Event ran without error without locking resources")
}
}
func TestStartEventQueue(t * testing.T) {
root_event, _ := NewEventQueue("root_event", "", []Resource{})
r := root_event.DoneResource()
@ -397,7 +416,12 @@ func TestStartEventQueue(t * testing.T) {
SendUpdate(root_event, signal)
}()
err = StartRootEvent(root_event)
err = LockResources(root_event)
if err != nil {
t.Fatal(err)
}
err = RunEvent(root_event)
if err != nil {
t.Fatal(err)
}