Browse Source

fix: Not stopping debug server if not running in interactive mode

Matthias Ladkau 3 years ago
parent
commit
4d62c35338
2 changed files with 36 additions and 13 deletions
  1. 18 2
      README.md
  2. 18 11
      cli/tool/debug.go

+ 18 - 2
README.md

@@ -83,7 +83,7 @@ The interpreter can be run in debug mode which adds debug commands to the consol
 
 It is possible to package your ECAL project into an executable that can be run without a separate ECAL interpreter. Run the `sh pack.sh` and see the script for details.
 
-### Embedding ECAL
+### Embedding ECAL and using event processing
 
 The primary purpose of ECAL is to be a simple multi-purpose language which can be embedded into other software:
 - It has a minimal (quite generic) syntax.
@@ -115,9 +115,11 @@ If events are to be used then the processor of the runtime provider needs to be
 ```
 rtp.Processor.Start()
 ```
+The processor must be started *after* all sinks have been declared and *before* events are thrown.
+
 Events can then be injected into the interpreter.
 ```
-monitor, err := rtp.Processor.AddEventAndWait(engine.NewEvent("MyEvent", []string{"foo", "bar"}, map[interface{}]interface{}{
+monitor, err := rtp.Processor.AddEventAndWait(engine.NewEvent("MyEvent", []string{"foo", "bar", "myevent"}, map[interface{}]interface{}{
   "data1": 123,
   "data2": "123",
 }), nil)
@@ -126,6 +128,20 @@ All errors are collected in the returned monitor.
 ```
 monitor.RootMonitor().AllErrors()
 ```
+The above event could be handled in ECAL with the following sinks:
+```
+sink mysink
+  kindmatch [ "foo.bar.myevent" ],
+{
+  log("Got event: ", event)
+}
+
+sink mysink2
+  kindmatch [ "foo.*.*" ],
+{
+  log("Got event: ", event)
+}
+```
 
 ### Using Go plugins in ECAL
 

+ 18 - 11
cli/tool/debug.go

@@ -45,16 +45,16 @@ type CLIDebugInterpreter struct {
 	BreakOnStart    *bool   // Flag if the debugger should stop the execution on start
 	BreakOnError    *bool   // Flag if the debugger should stop when encountering an error
 
-	// Log output
+	LogOut io.Writer // Log output
 
-	LogOut io.Writer
+	debugServer *debugTelnetServer // Debug server if started
 }
 
 /*
 NewCLIDebugInterpreter wraps an existing CLIInterpreter object and adds capabilities.
 */
 func NewCLIDebugInterpreter(i *CLIInterpreter) *CLIDebugInterpreter {
-	return &CLIDebugInterpreter{i, nil, nil, nil, nil, nil, nil, os.Stdout}
+	return &CLIDebugInterpreter{i, nil, nil, nil, nil, nil, nil, os.Stdout, nil}
 }
 
 /*
@@ -112,20 +112,17 @@ func (i *CLIDebugInterpreter) Interpret() error {
 
 			// Start the debug server
 
-			debugServer := &debugTelnetServer{*i.DebugServerAddr, "ECALDebugServer: ",
+			i.debugServer = &debugTelnetServer{*i.DebugServerAddr, "ECALDebugServer: ",
 				nil, true, *i.EchoDebugServer, i, i.RuntimeProvider.Logger}
 
 			wg := &sync.WaitGroup{}
 			wg.Add(1)
-			go debugServer.Run(wg)
+			go i.debugServer.Run(wg)
 			wg.Wait()
 
-			defer func() {
-				if debugServer.listener != nil {
-					debugServer.listen = false
-					debugServer.listener.Close() // Attempt to cleanup
-				}
-			}()
+			if *i.Interactive {
+				defer i.StopDebugServer()
+			}
 		}
 
 		err = i.CLIInterpreter.Interpret(*i.Interactive)
@@ -134,6 +131,16 @@ func (i *CLIDebugInterpreter) Interpret() error {
 	return err
 }
 
+/*
+StopDebugServer stops the debug server if it was started.
+*/
+func (i *CLIDebugInterpreter) StopDebugServer() {
+	if i.debugServer != nil && i.debugServer.listener != nil {
+		i.debugServer.listen = false
+		i.debugServer.listener.Close() // Attempt to cleanup
+	}
+}
+
 /*
 LoadInitialFile clears the global scope and reloads the initial file.
 */