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

commit ff1d413ab70ca0b1e4b849290e4d90c529f97ee4
Author: Aaron Radzinski <[email protected]>
AuthorDate: Thu Sep 24 20:21:18 2020 -0700

    WIP.
---
 .../apache/nlpcraft/common/version/NCVersion.scala |  6 ++-
 .../model/tools/cmdline/NCCommandLine.scala        | 49 ++++++++++++++++++----
 .../org/apache/nlpcraft/server/NCServer.scala      |  4 +-
 3 files changed, 49 insertions(+), 10 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala
index 381b87d..00fab4a 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/version/NCVersion.scala
@@ -17,7 +17,7 @@
 
 package org.apache.nlpcraft.common.version
 
-import java.time.LocalDate
+import java.time.{LocalDate, Year}
 
 import com.typesafe.scalalogging.LazyLogging
 import org.apache.nlpcraft.common._
@@ -27,7 +27,9 @@ import org.apache.nlpcraft.common._
   * release the new version will be added to this object manually.
   */
 object NCVersion extends LazyLogging {
-    final val copyright = s"Copyright (C) 2020 Apache Software Foundation"
+    final val year = Year.now().toString
+    final val copyright = s"Copyright (C) $year Apache Software Foundation"
+    final val copyrightShort = s"(C) $year ASF"
     
     /**
       *
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 29f05dd..f7a3cdb 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
@@ -17,14 +17,19 @@
 
 package org.apache.nlpcraft.model.tools.cmdline
 
+import java.io.{File, FileInputStream, ObjectInputStream}
+
 import org.apache.commons.lang3.SystemUtils
 import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.common.ansi.NCAnsiColor
 import org.apache.nlpcraft.common.ansi.NCAnsiColor._
 import org.apache.nlpcraft.common.version.NCVersion
+import resource.managed
 
 import scala.collection.mutable
+import scala.compat.java8.OptionConverters._
+import scala.util.Try
 
 /**
  * 'nlpcraft' script entry point.
@@ -32,6 +37,8 @@ import scala.collection.mutable
 object NCCommandLine extends App {
     private final val NAME = "Apache NLPCraft CLI"
 
+    private final val SRV_PID_PATH = ".nlpcraft/server_pid"
+
     private final lazy val VER = NCVersion.getCurrent
     private final lazy val INSTALL_HOME = 
U.sysEnv("NLPCRAFT_CLI_INSTALL_HOME").getOrElse(
         SystemUtils.USER_DIR
@@ -281,7 +288,30 @@ object NCCommandLine extends App {
     private def cmdStopServer(cmd: Command, params: Seq[String]): Unit = {
         title()
 
-        // TODO
+        val path = new File(SystemUtils.getUserHome, SRV_PID_PATH)
+        var pid = -1L
+
+        if (path.exists())
+            pid =
+                Try {
+                    managed(new ObjectInputStream(new FileInputStream(path))) 
acquireAndGet { _.readLong() }
+                }
+                .getOrElse(-1L)
+
+        if (pid == -1)
+            error("Cannot detect locally running server.")
+        else {
+            ProcessHandle.of(pid).asScala match {
+                case Some(ph) ⇒
+                    if (ph.destroy())
+                        confirm("Local server has been stopped.")
+                    else
+                        error(s"Unable to stop the local server [pid=$pid]")
+
+
+                case None ⇒ error("Cannot find locally running server.")
+            }
+        }
     }
 
     /**
@@ -440,16 +470,16 @@ object NCCommandLine extends App {
      */
     private def cmdVersion(cmd: Command, params: Seq[String]): Unit =
         if (params.isEmpty)
-            title()
+            confirm(s"$NAME ver. ${VER.version}, released on ${VER.date}")
         else {
             val isS = cmd.isParamPresent("semver", params)
             val isD = cmd.isParamPresent("reldate", params)
 
             if (isS || isD) {
                 if (isS)
-                    log(s"${VER.version}")
+                    confirm(s"${VER.version}")
                 if (isD)
-                    log(s"${VER.date}")
+                    confirm(s"${VER.date}")
             }
             else
                 error(s"Invalid parameters for command '${cmd.mainName}': 
${params.mkString(", ")}")
@@ -464,7 +494,7 @@ object NCCommandLine extends App {
         // Make sure we exit with non-zero status.
         exitStatus = 1
 
-        System.err.println(s"ERROR: $msg")
+        System.err.println(s"${ansiRedFg}ERR:$ansiReset $msg")
     }
 
     /**
@@ -475,16 +505,21 @@ object NCCommandLine extends App {
 
     /**
      *
+     * @param msg
+     */
+    private def confirm(msg: String): Unit = 
System.out.println(s"${ansiGreenFg}>$ansiReset $msg")
+
+    /**
+     *
      */
     private def errorHelp(): Unit =
-        error(s"Run '$SCRIPT_NAME ${HELP_CMD.mainName}' to read the manual.")
+        error(s"Run '$ansiCyanFg$SCRIPT_NAME ${HELP_CMD.mainName}$ansiReset' 
to read the manual.")
 
     /**
      * Prints out the version and copyright title header.
      */
     private def title(): Unit = {
         log(s"$NAME ver. ${VER.version}")
-        log(NCVersion.copyright)
         log()
     }
 
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 eef4f51..ec3af09 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -241,7 +241,9 @@ object NCServer extends App with NCIgniteInstance with 
LazyLogging with NCOpenCe
                     stream.flush()
                 }
 
-                logger.info(s"PID stored in: ${path.getAbsolutePath}")
+                path.deleteOnExit()
+
+                logger.trace(s"PID stored in: ${path.getAbsolutePath}")
             }
             catch {
                 case e: IOException ⇒ U.prettyError(logger, "Failed to store 
server PID.", e)

Reply via email to