From 5be9b9ac07d45ff1b007ad89f7ac8786e1f7c387 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 20 Jun 2023 20:48:48 -0600 Subject: [PATCH] Removed StartRootEvent, now run LockResource(root_event) then RunEvent(root_event) --- event.go | 40 ++++++++++++++-------------------------- manager_test.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/event.go b/event.go index ab5bc0e..bed4ce8 100644 --- a/event.go +++ b/event.go @@ -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") - - err := LockResources(event) - if err != nil { - Log.Logf("event", "ROOT_EVENT_LOCK_ERR: %s", err) - return err - } - - err = RunEvent(event) - if err != nil { - Log.Logf("event", "ROOT_EVENT_RUNE_ERR: %s", err) - return err - } +func RunEvent(event Event) error { + Log.Logf("event", "EVENT_RUN: %s", event.Name()) - err = FinishEvent(event) - if err != nil { - Log.Logf("event", "ROOT_EVENT_FINISH_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()) + } } - 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) } } diff --git a/manager_test.go b/manager_test.go index c74ac15..393b9a8 100644 --- a/manager_test.go +++ b/manager_test.go @@ -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) }