This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
new d577943 WIP.
d577943 is described below
commit d5779432078b7927a737888301b3182d9db69bc7
Author: Aaron Radzinski <[email protected]>
AuthorDate: Thu Oct 1 00:19:39 2020 -0700
WIP.
---
.../nlpcraft/model/tools/cmdline/NCCli.scala | 74 ++++++++++++++--------
.../model/tools/cmdline/NCCliServerBeacon.scala | 3 +-
.../org/apache/nlpcraft/server/NCServer.scala | 14 ++--
3 files changed, 60 insertions(+), 31 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 8294075..26bf082 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
@@ -36,6 +36,8 @@ import org.apache.nlpcraft.common.ansi.NCAnsi._
import org.apache.nlpcraft.common.version.NCVersion
import java.lang.ProcessBuilder.Redirect
import java.lang.management.ManagementFactory
+import java.text.DateFormat
+import java.util.Date
import resource.managed
@@ -440,26 +442,29 @@ object NCCli extends App {
}
/**
+ * Loads server beacon file and return its data and its corresponding
process handle.
*
* @return
*/
- private def loadServerBeacon(): Option[NCCliServerBeacon] = {
- val path = new File(SystemUtils.getUserHome, SRV_BEACON_PATH)
+ private def loadServerBeacon(): Option[(NCCliServerBeacon, ProcessHandle)]
=
+ try {
+ val rawObj = managed(
+ new ObjectInputStream(
+ new FileInputStream(
+ new File(SystemUtils.getUserHome, SRV_BEACON_PATH)
+ )
+ )
+ ) acquireAndGet {
+ _.readObject()
+ }
- if (path.exists())
- try {
- val rawObj = managed(new ObjectInputStream(new
FileInputStream(path))) acquireAndGet {
- _.readObject()
- }
+ val beacon = rawObj.asInstanceOf[NCCliServerBeacon]
- Some(rawObj.asInstanceOf[NCCliServerBeacon])
- }
- catch {
- case _: Exception ⇒ None
- }
- else
- None
- }
+ ProcessHandle.of(beacon.pid).asScala.map(beacon → _)
+ }
+ catch {
+ case _: Exception ⇒ None
+ }
/**
* @param cmd Command descriptor.
@@ -467,19 +472,13 @@ object NCCli extends App {
*/
private def cmdStopServer(cmd: Command, args: Seq[Argument]): Unit = {
loadServerBeacon() match {
- case Some(beacon) ⇒
+ case Some((beacon, ph)) ⇒
val pid = beacon.pid
- ProcessHandle.of(pid).asScala match {
- case Some(ph) ⇒
- if (ph.destroy())
- logln(s"Local REST server (pid ${c(pid.toString)})
has been stopped.")
- else
- error(s"Failed to stop the local REST server (pid
${c(pid.toString)}).")
-
-
- case None ⇒ error("Cannot find locally running REST
server.")
- }
+ if (ph.destroy())
+ logln(s"Local REST server (pid ${c(pid.toString)}) has
been stopped.")
+ else
+ error(s"Failed to stop the local REST server (pid
${c(pid.toString)}).")
case None ⇒
error("Cannot detect locally running REST server.")
@@ -619,10 +618,33 @@ object NCCli extends App {
/**
*
+ * @param beacon
+ * @return
+ */
+ private def mkServerBeaconTable(beacon: NCCliServerBeacon): NCAsciiTable =
{
+ val tbl = new NCAsciiTable
+
+ tbl += ("PID", s"${g(beacon.pid)}")
+ tbl += ("JDBC URL", s"${g(beacon.jdbcUrl)}")
+ tbl += ("REST endpoint", s"${g(beacon.restEndpoint)}")
+ tbl += ("Uplink", s"${g(beacon.upLink)}")
+ tbl += ("Downlink", s"${g(beacon.downLink)}")
+ tbl += ("Started on", s"${g(DateFormat.getDateTimeInstance.format(new
Date(beacon.startMs)))}")
+
+ tbl
+ }
+
+ /**
+ *
* @param cmd Command descriptor.
* @param args Arguments, if any, for this command.
*/
private def cmdRepl(cmd: Command, args: Seq[Argument]): Unit = {
+ loadServerBeacon() match {
+ case Some((beacon, _)) ⇒ logln(s"Local REST server
detected:\n${mkServerBeaconTable(beacon).toString}")
+ case None ⇒ ()
+ }
+
logln(s"Type ${c("help")} or ${c("help -c=repl")} to get help.")
logln(s"Type ${c("quit")} to exit.")
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 ffe0e5c..76a46ec 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
@@ -29,6 +29,7 @@ case class NCCliServerBeacon(
jdbcUrl: String,
restEndpoint: String,
upLink: String,
- downLink: String
+ downLink: String,
+ startMs: Long
)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
index 290d653..e9ef08c 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -253,7 +253,8 @@ object NCServer extends App with NCIgniteInstance with
LazyLogging with NCOpenCe
jdbcUrl = Config.jdbcUrl,
restEndpoint =
s"${Config.restHost}:${Config.restPort}",
upLink = Config.upLink,
- downLink = Config.downLink
+ downLink = Config.downLink,
+ startMs = currentTime
))
stream.flush()
}
@@ -272,7 +273,13 @@ object NCServer extends App with NCIgniteInstance with
LazyLogging with NCOpenCe
catching(classOf[IOException]) either {
managed(new ObjectInputStream(new FileInputStream(path)))
acquireAndGet { _.readObject() }
} match {
- case Left(e) ⇒ U.prettyError(logger, s"Failed to read existing
server beacon: ${path.getAbsolutePath}", e)
+ case Left(e) ⇒
+ U.prettyError(logger, s"Failed to read existing server
beacon: ${path.getAbsolutePath}", e)
+
+ logger.trace(s"Overriding failed server beacon:
${path.getAbsolutePath}")
+
+ storeInUserHome()
+
case Right(rawObj) ⇒
val beacon = rawObj.asInstanceOf[NCCliServerBeacon]
@@ -284,10 +291,9 @@ object NCServer extends App with NCIgniteInstance with
LazyLogging with NCOpenCe
storeInUserHome()
}
}
- else {
+ else
// No existing beacon file detected.
storeInUserHome()
- }
}
NCIgniteRunner.runWith(