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 d050eda  WIP.
d050eda is described below

commit d050edaa906ea542e12482463579f9f5b133d36c
Author: Aaron Radzinski <[email protected]>
AuthorDate: Thu Sep 24 17:34:27 2020 -0700

    WIP.
---
 .../model/tools/cmdline/NCCommandLine.scala        | 20 ++++++----
 .../org/apache/nlpcraft/server/NCServer.scala      | 44 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 7 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
index 2f54737..29f05dd 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
@@ -77,7 +77,7 @@ object NCCommandLine extends App {
     }
     // Single command's example.
     case class Example(
-        code: String,
+        usage: String,
         desc: String
     )
     // Single command's parameter.
@@ -135,11 +135,11 @@ object NCCommandLine extends App {
             ),
             examples = Seq(
                 Example(
-                    code = s"$$ $SCRIPT_NAME start-server",
+                    usage = s"$$ $SCRIPT_NAME start-server",
                     desc = "Starts REST server with default configuration."
                 ),
                 Example(
-                    code = s"$$ $SCRIPT_NAME start-server 
-c=/opt/nlpcraft/nlpcraft.conf",
+                    usage = s"$$ $SCRIPT_NAME start-server 
-c=/opt/nlpcraft/nlpcraft.conf",
                     desc = "Starts REST server with alternative configuration 
file."
                 )
             )
@@ -151,7 +151,13 @@ object NCCommandLine extends App {
             desc = Some(
                 s"This is a special command that can be combined with any 
other commands."
             ),
-            body = cmdNoAnsi
+            body = cmdNoAnsi,
+            examples = Seq(
+                Example(
+                    usage = s"$$ $SCRIPT_NAME help -c=repl no-ansi",
+                    desc = "Displays help for 'repl' commands without using 
ANSI escape sequences."
+                )
+            )
         ),
         Command(
             id = "stop-server",
@@ -188,11 +194,11 @@ object NCCommandLine extends App {
             ),
             examples = Seq(
                 Example(
-                    code = s"$$ $SCRIPT_NAME help -c=repl --cmd=ver",
+                    usage = s"$$ $SCRIPT_NAME help -c=repl --cmd=ver",
                     desc = "Displays help for 'repl' and 'version' commands."
                 ),
                 Example(
-                    code = s"$$ $SCRIPT_NAME help -all",
+                    usage = s"$$ $SCRIPT_NAME help -all",
                     desc = "Displays help for all commands."
                 )
             )
@@ -348,7 +354,7 @@ object NCCommandLine extends App {
                 lines += s"${ansiBold}EXAMPLES:$ansiReset"
 
                 for (ex ← cmd.examples) {
-                    lines += s"$T___${ex.code}"
+                    lines += s"$T___${ex.usage}"
                     lines += s"$T___$T___${ex.desc}"
                 }
             }
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 a0dfd1b..eef4f51 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -17,10 +17,12 @@
 
 package org.apache.nlpcraft.server
 
+import java.io.{File, FileInputStream, FileOutputStream, IOException, 
ObjectInputStream, ObjectOutputStream}
 import java.util.concurrent.CountDownLatch
 
 import com.typesafe.config.Config
 import com.typesafe.scalalogging.LazyLogging
+import org.apache.commons.lang3.SystemUtils
 import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.common.ansi.NCAnsiColor
 import org.apache.nlpcraft.common.ansi.NCAnsiColor._
@@ -50,6 +52,7 @@ import org.apache.nlpcraft.server.sql.NCSqlManager
 import org.apache.nlpcraft.server.sugsyn.NCSuggestSynonymManager
 import org.apache.nlpcraft.server.tx.NCTxManager
 import org.apache.nlpcraft.server.user.NCUserManager
+import resource.managed
 
 import scala.collection.mutable
 import scala.compat.Platform.currentTime
@@ -59,6 +62,8 @@ import scala.util.control.Exception.{catching, ignoring}
   * NLPCraft server app.
   */
 object NCServer extends App with NCIgniteInstance with LazyLogging with 
NCOpenCensusTrace {
+    private final val PID_PATH = ".nlpcraft/server_pid"
+
     private val startedMgrs = mutable.Buffer.empty[NCService]
 
     /**
@@ -188,6 +193,8 @@ object NCServer extends App with NCIgniteInstance with 
LazyLogging with NCOpenCe
         )
         
         asciiLogo()
+
+        storePid()
         
         val lifecycle = new CountDownLatch(1)
     
@@ -218,6 +225,43 @@ object NCServer extends App with NCIgniteInstance with 
LazyLogging with NCOpenCe
         }
     }
 
+    /**
+     *
+     */
+    private def storePid(): Unit = {
+        val path = new File(SystemUtils.getUserHome, PID_PATH)
+
+        /**
+         *
+         */
+        def storeInUserHome() =
+            try {
+                managed(new ObjectOutputStream(new FileOutputStream(path))) 
acquireAndGet { stream ⇒
+                    stream.writeLong(ProcessHandle.current().pid())
+                    stream.flush()
+                }
+
+                logger.info(s"PID stored in: ${path.getAbsolutePath}")
+            }
+            catch {
+                case e: IOException ⇒ U.prettyError(logger, "Failed to store 
server PID.", e)
+            }
+
+        if (path.exists())
+            catching(classOf[IOException]) either {
+                managed(new ObjectInputStream(new FileInputStream(path))) 
acquireAndGet { _.readLong() }
+            } match {
+                case Left(e) ⇒ U.prettyError(logger, s"Failed to read existing 
PID from: ${path.getAbsolutePath}", e)
+                case Right(pid) ⇒
+                    if (ProcessHandle.of(pid).isPresent)
+                        logger.error(s"Cannot store PID file as another local 
server detected [existing-pid=$pid]")
+                    else
+                        storeInUserHome()
+            }
+        else
+            storeInUserHome()
+    }
+
     NCIgniteRunner.runWith(
         args.find(_.startsWith("-igniteConfig=")) match {
             case None ⇒ null // Will use default on the classpath 'ignite.xml'.

Reply via email to