This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch NLPCRAFT-301
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-301 by this push:
     new 0f3eddc  Fix for NLPCRAFT-301.
0f3eddc is described below

commit 0f3eddca7e3f743ecb3ba8cb1f45d07ef30b7f5b
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Sat Apr 24 10:37:42 2021 +0300

    Fix for NLPCRAFT-301.
---
 .../nlpcraft/model/tools/cmdline/NCCli.scala       | 68 +++++++++---------
 .../nlpcraft/model/tools/cmdline/NCCliBase.scala   | 83 +++++++---------------
 .../model/tools/cmdline/NCCliServerBeacon.scala    |  4 +-
 3 files changed, 63 insertions(+), 92 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index 94e3678..6e9e67c 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -357,7 +357,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not running from REPL.
      */
@@ -500,7 +500,7 @@ object NCCli extends NCCliBase {
 
                         // Once beacon is loaded, ensure that REST endpoint is 
live.
                         if (beacon != null)
-                            online = Try(restHealth("http://"; + 
beacon.restEndpoint) == 200).getOrElse(false)
+                            online = Try(restHealth(beacon.restUrl) == 
200).getOrElse(false)
                     }
 
                     if (!online)
@@ -513,7 +513,8 @@ object NCCli extends NCCliBase {
                 if (!online && currentTime >= endOfWait) // Timed out - 
attempt to kill the timed out process...
                     ProcessHandle.of(srvPid).asScala match {
                         case Some(ph) ⇒
-                            ph.destroy()
+                            if (ph.destroy())
+                                error(s"Timed out server process terminated.")
 
                             if (beacon != null && beacon.beaconPath != null)
                                 new File(beacon.beaconPath).delete()
@@ -524,7 +525,7 @@ object NCCli extends NCCliBase {
                 if (!online) {
                     logln(r(" [Error]"))
 
-                    error(s"Server start failed, check full log for errors: 
${c(output.getAbsolutePath)}")
+                    error(s"Server start failed - check full log for errors: 
${c(output.getAbsolutePath)}")
 
                     tailFile(output.getAbsolutePath, 20)
                 }
@@ -543,7 +544,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not running from REPL.
      */
@@ -609,7 +610,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not running from REPL.
      */
@@ -779,7 +780,8 @@ object NCCli extends NCCliBase {
                 if (currentTime >= endOfWait)
                     ProcessHandle.of(prbPid).asScala match {
                         case Some(ph) ⇒
-                            ph.destroy()
+                            if (ph.destroy())
+                                error(s"Timed out probe process terminated.")
 
                             if (beacon != null && beacon.beaconPath != null)
                                 new File(beacon.beaconPath).delete()
@@ -790,7 +792,7 @@ object NCCli extends NCCliBase {
                 if (beacon == null) {
                     logln(r(" [Error]"))
 
-                    error(s"Probe start failed, check full log for errors: 
${c(output.getAbsolutePath)}")
+                    error(s"Probe start failed - check full log for errors: 
${c(output.getAbsolutePath)}")
 
                     tailFile(output.getAbsolutePath, 20)
                 }
@@ -820,7 +822,7 @@ object NCCli extends NCCliBase {
      */
     private def getRestEndpointFromBeacon: String =
         loadServerBeacon() match {
-            case Some(beacon) ⇒ s"http://${beacon.restEndpoint}";
+            case Some(beacon) ⇒ beacon.restUrl
             case None ⇒ throw NoLocalServer()
         }
 
@@ -853,7 +855,7 @@ object NCCli extends NCCliBase {
         }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -870,7 +872,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -887,7 +889,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1002,12 +1004,10 @@ object NCCli extends NCCliBase {
                 state.isServerOnline = true
 
                 try {
-                    val baseUrl = "http://"; + beacon.restEndpoint
-
                     // Attempt to sign in with the default account.
                     if (autoSignIn && state.accessToken.isEmpty)
                         httpPostResponseJson(
-                            baseUrl,
+                            beacon.restUrl,
                             "signin",
                             s"""{"email": "$DFLT_USER_EMAIL", "passwd": 
"$DFLT_USER_PASSWD"}""") match {
                             case Some(json) ⇒
@@ -1026,7 +1026,7 @@ object NCCli extends NCCliBase {
                     // Attempt to get all connected probes if successfully 
signed in prior.
                     if (state.accessToken.isDefined)
                         httpPostResponseJson(
-                            baseUrl,
+                            beacon.restUrl,
                             "probe/all",
                             "{\"acsTok\": \"" + state.accessToken.get + "\"}") 
match {
                             case Some(json) ⇒ state.probes =
@@ -1113,7 +1113,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1131,7 +1131,7 @@ object NCCli extends NCCliBase {
         }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1141,7 +1141,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1166,7 +1166,7 @@ object NCCli extends NCCliBase {
         }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1191,7 +1191,7 @@ object NCCli extends NCCliBase {
         }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1200,7 +1200,7 @@ object NCCli extends NCCliBase {
     }
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1208,7 +1208,7 @@ object NCCli extends NCCliBase {
         NCAnsi.setEnabled(false)
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1216,7 +1216,7 @@ object NCCli extends NCCliBase {
         NCAnsi.setEnabled(true)
 
     /**
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1406,8 +1406,6 @@ object NCCli extends NCCliBase {
 
         val logPath = if (beacon.logPath != null) g(beacon.logPath) else 
y("<not available>")
 
-        val epUrl = "http://"; + beacon.restEndpoint
-
         tbl += ("PID", s"${g(beacon.pid)}")
         tbl += ("Database:", "")
         tbl += ("  URL", s"${g(beacon.dbUrl)}")
@@ -1418,7 +1416,7 @@ object NCCli extends NCCliBase {
         tbl += ("  Pool increment", s"${g(beacon.dbPoolInc)}")
         tbl += ("  Reset on start", s"${g(beacon.dbInit)}")
         tbl += ("REST:", "")
-        tbl += ("  Endpoint", s"${g(epUrl)}")
+        tbl += ("  Endpoint", s"${g(beacon.restUrl)}")
         tbl += ("  API provider", s"${g(beacon.restApi)}")
         tbl += ("Probe:", "")
         tbl += ("  Uplink", s"${g(beacon.upLink)}")
@@ -1488,12 +1486,12 @@ object NCCli extends NCCliBase {
 
         state.probes.foreach(addProbeToTable(tbl, _))
 
-        logln(s"Connected probes (${state.probes.size}):\n${tbl.toString}")
+        logln(s"All server connected probes 
(${state.probes.size}):\n${tbl.toString}")
     }
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1504,7 +1502,7 @@ object NCCli extends NCCliBase {
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1517,7 +1515,7 @@ object NCCli extends NCCliBase {
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1530,7 +1528,7 @@ object NCCli extends NCCliBase {
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1547,7 +1545,7 @@ object NCCli extends NCCliBase {
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1573,7 +1571,7 @@ object NCCli extends NCCliBase {
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
@@ -1612,7 +1610,7 @@ object NCCli extends NCCliBase {
 
     /**
      *
-     * @param cmd  Command descriptor.
+     * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
      * @param repl Whether or not executing from REPL.
      */
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
index 39323ad..d263930 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliBase.scala
@@ -20,6 +20,7 @@ package org.apache.nlpcraft.model.tools.cmdline
 import org.apache.commons.lang3.SystemUtils
 import org.apache.nlpcraft.common.version.NCVersion
 import org.apache.nlpcraft.common._
+import java.lang.{IllegalStateException => ISE}
 
 import java.io.File
 import java.lang.management.ManagementFactory
@@ -31,6 +32,8 @@ import java.util.regex.Pattern
 class NCCliBase extends App {
     final val NAME = "NLPCraft CLI"
 
+    private final val dir = ".nlpcraft"
+
     /*
      * Disable warnings from Ignite on JDK 11.
      */
@@ -57,13 +60,16 @@ class NCCliBase extends App {
     // Used for progress bar functionality.
     // +==================================================================+
     // | MAKE SURE TO UPDATE THIS VAR WHEN NUMBER OF SERVICES IS CHANGED. |
-    // +==================================================================+
+    // +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^+
     final val NUM_SRV_SERVICES = 31 /*services*/ + 1 /*progress start*/
     final val NUM_PRB_SERVICES = 23 /*services*/ + 1 /*progress start*/
+    // +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^+
+    // | MAKE SURE TO UPDATE THIS VAR WHEN NUMBER OF SERVICES IS CHANGED. |
+    // +==================================================================+
 
-    final val SRV_BEACON_PATH = ".nlpcraft/server_beacon"
-    final val PRB_BEACON_PATH = ".nlpcraft/probe_beacon"
-    final val HIST_PATH = ".nlpcraft/.cli_history"
+    final val SRV_BEACON_PATH = s"$dir/server_beacon"
+    final val PRB_BEACON_PATH = s"$dir/probe_beacon"
+    final val HIST_PATH = s"$dir/.cli_history"
 
     final val DFLT_USER_EMAIL = "[email protected]"
     final val DFLT_USER_PASSWD = "admin"
@@ -113,61 +119,26 @@ class NCCliBase extends App {
          name: String,
          version: String,
          enabledBuiltInTokens: Array[String]
-     )
+    )
 
     case class ProbeAllResponse(
        probes: Array[Probe],
        status: String
-   )
-
-    case class SplitError(index: Int)
-        extends Exception
-
-    case class UnknownCommand(cmd: String)
-        extends IllegalArgumentException(s"Unknown command ${c("'" + cmd + 
"'")}, type ${c("'help'")} to get help.")
-
-    case class NoLocalServer()
-        extends IllegalStateException(s"Local server not found, use 
$C'start-server'$RST command to start one.")
-
-    case class NoLocalProbe()
-        extends IllegalStateException(s"Local probe not found, use 
$C'start-probe'$RST command to start one.")
-
-    case class MissingParameter(cmd: Command, paramId: String)
-        extends IllegalArgumentException(
-            s"Missing mandatory parameter $C${"'" + cmd.params.find(_.id == 
paramId).get.names.head + "'"}$RST, " +
-                s"type $C'help --cmd=${cmd.name}'$RST to get help."
-        )
-
-    case class NotSignedIn()
-        extends IllegalStateException(s"Not signed in. Use ${c("'signin'")} 
command to sign in first.")
-
-    case class MissingMandatoryJsonParameters(cmd: Command, missingParams: 
Seq[RestSpecParameter], path: String)
-        extends IllegalArgumentException(
-            s"Missing mandatory JSON parameters (${missingParams.map(s ⇒ 
y(s.name)).mkString(",")}) " +
-                s"for $C${"'" + cmd.name + s" --path=$path'"}$RST, type 
$C'help --cmd=${cmd.name}'$RST to get help."
-        )
-
-    case class InvalidParameter(cmd: Command, paramId: String)
-        extends IllegalArgumentException(
-            s"Invalid parameter $C${"'" + cmd.params.find(_.id == 
paramId).get.names.head + "'"}$RST, " +
-                s"type $C'help --cmd=${cmd.name}'$RST to get help."
-        )
-
-    case class InvalidJsonParameter(cmd: Command, param: String)
-        extends IllegalArgumentException(
-            s"Invalid JSON parameter $C${"'" + param + "'"}$RST, " +
-                s"type $C'help --cmd=${cmd.name}'$RST to get help."
-        )
-
-    case class HttpError(httpCode: Int)
-        extends IllegalStateException(s"REST error (HTTP ${c(httpCode)}).")
-
-    case class MalformedJson()
-        extends IllegalStateException(s"Malformed JSON. ${c("Tip:")} on 
Windows make sure to escape double quotes.")
-
-    case class TooManyArguments(cmd: Command)
-        extends IllegalArgumentException(s"Too many arguments, type $C'help 
--cmd=${cmd.name}'$RST to get help.")
+    )
 
-    case class NotEnoughArguments(cmd: Command)
-        extends IllegalArgumentException(s"Not enough arguments, type $C'help 
--cmd=${cmd.name}'$RST to get help.")
+    private def help(cmd: Command): String = s"type $C'help 
--cmd=${cmd.name}'$RST to get help."
+
+    case class SplitError(index: Int) extends Exception
+    case class UnknownCommand(cmd: String) extends ISE(s"Unknown command 
${c("'" + cmd + "'")}, type ${c("'help'")} to get help.")
+    case class NoLocalServer() extends ISE(s"Local server not found, use 
$C'start-server'$RST command to start one.")
+    case class NotSignedIn() extends ISE(s"Not signed in. Use ${c("'signin'")} 
command to sign in first.")
+    case class NoLocalProbe() extends ISE(s"Local probe not found, use 
$C'start-probe'$RST command to start one.")
+    case class HttpError(httpCode: Int) extends ISE(s"REST error (HTTP 
${c(httpCode)}).")
+    case class MalformedJson() extends ISE(s"Malformed JSON. ${c("Tip:")} on 
Windows make sure to escape double quotes.")
+    case class TooManyArguments(cmd: Command) extends ISE(s"Too many 
arguments, ${help(cmd)}")
+    case class NotEnoughArguments(cmd: Command) extends ISE(s"Not enough 
arguments, ${help(cmd)}")
+    case class MissingParameter(cmd: Command, paramId: String) extends ISE( 
s"Missing mandatory parameter $C${"'" + cmd.params.find(_.id == 
paramId).get.names.head + "'"}$RST, ${help(cmd)}")
+    case class MissingMandatoryJsonParameters(cmd: Command, missingParams: 
Seq[RestSpecParameter], path: String) extends ISE(s"Missing mandatory JSON 
parameters (${missingParams.map(s ⇒ y(s.name)).mkString(",")}) for $C${"'" + 
cmd.name + s" --path=$path'"}$RST, ${help(cmd)}")
+    case class InvalidParameter(cmd: Command, paramId: String) extends 
ISE(s"Invalid parameter $C${"'" + cmd.params.find(_.id == 
paramId).get.names.head + "'"}$RST, ${help(cmd)}")
+    case class InvalidJsonParameter(cmd: Command, param: String) extends 
ISE(s"Invalid JSON parameter $C${"'" + param + "'"}$RST, ${help(cmd)}")
 }
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliServerBeacon.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliServerBeacon.scala
index b4dda99..3610e57 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliServerBeacon.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliServerBeacon.scala
@@ -65,5 +65,7 @@ case class NCCliServerBeacon(
     acsToksExpireMins: Int,
     @transient var logPath: String = null,
     @transient var ph: ProcessHandle = null
-)
+) {
+    lazy val restUrl: String = "http://"; + restEndpoint
+}
 

Reply via email to