From 27ee27b8375a9afa17057fcd23bf122b3e4c2c84 Mon Sep 17 00:00:00 2001 From: Kenichi Maehashi Date: Tue, 29 Sep 2015 11:32:12 +0900 Subject: [PATCH 1/2] Send session ID immediately when reconnecting to existing sessions --- .../us/jubat/jubaql_client/JubaQLClient.scala | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala b/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala index 6f73687..22b3185 100644 --- a/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala +++ b/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala @@ -57,17 +57,14 @@ object JubaQLClient { } }) - // login if session id is not given - val sessionId = sessionIdParameter match { - case Some(session) => - session - case _ => - val serverResponse = getSessionId(hostname, port) - if (serverResponse.isEmpty) { - System.exit(1) - } - serverResponse.get + val serverResponse = sessionIdParameter match { + case Some(session) => getSessionId(hostname, port, session) + case _ => getSessionId(hostname, port) + } + if (serverResponse.isEmpty) { + System.exit(1) } + val sessionId = serverResponse.get Console.err.println("Using session id \"%s\"".format(sessionId)) // read from stdin or the given scriptfile @@ -104,14 +101,22 @@ object JubaQLClient { System.exit(0) } - /** Gets a session id from a JubaQL gateway server. - * - * @return some session id if login was successful - */ + /** + * Gets a session id from a JubaQL gateway server. + * + * @return some session id if login was successful + */ def getSessionId(hostname: String, port: Int, - err: PrintStream = Console.err): Option[String] = { + sessionId: String = null, err: PrintStream = Console.err): Option[String] = { val url = :/(hostname, port) / "login" - val req = Http(url.POST OK as.String) + val req = sessionId match { + case null => Http(url.POST OK as.String) + case _ => { + val payloadData = ("session_id" -> sessionId) + val json: String = compact(render(payloadData)) + Http(url << json OK as.String) + } + } req.either.apply() match { case Left(error) => // if request fails or is non-2xx, print error From ba1984b6e2dea1083ef09fef77cc2551072d53ff Mon Sep 17 00:00:00 2001 From: Kenichi Maehashi Date: Tue, 29 Sep 2015 18:51:43 +0900 Subject: [PATCH 2/2] improve error message when failure of processer execution is detected --- .../us/jubat/jubaql_client/JubaQLClient.scala | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala b/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala index 22b3185..9437aa4 100644 --- a/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala +++ b/src/main/scala/us/jubat/jubaql_client/JubaQLClient.scala @@ -110,32 +110,38 @@ object JubaQLClient { sessionId: String = null, err: PrintStream = Console.err): Option[String] = { val url = :/(hostname, port) / "login" val req = sessionId match { - case null => Http(url.POST OK as.String) + case null => Http(url.POST) case _ => { val payloadData = ("session_id" -> sessionId) val json: String = compact(render(payloadData)) - Http(url << json OK as.String) + Http(url << json) } } req.either.apply() match { + case Right(resp) => + if (resp.getStatusCode / 100 != 2) { + println("[ERROR/%s] %s".format(resp.getStatusCode, resp.getResponseBody)) + None + } else { + val resultJson = resp.getResponseBody + parseOpt(resultJson) match { + case Some(result) => + (result \ "session_id") match { + case JString(session_id) => + Some(session_id) + case _ => + err.println("[ERROR] JSON did not contain session_id") + None + } + case None => + err.println("[ERROR] failed to parse JSON: \"%s\"".format(resultJson)) + None + } + } case Left(error) => - // if request fails or is non-2xx, print error + // request fails with network error err.println("[ERROR] " + error.getCause + ": " + error.getMessage) None - case Right(resultJson) => - parseOpt(resultJson) match { - case Some(result) => - (result \ "session_id") match { - case JString(session_id) => - Some(session_id) - case _ => - err.println("[ERROR] JSON did not contain session_id") - None - } - case None => - err.println("[ERROR] failed to parse JSON: \"%s\"".format(resultJson)) - None - } } } @@ -157,7 +163,7 @@ object JubaQLClient { parseOpt(resultJson) match { case Some(result) => if (resp.getStatusCode / 100 != 2) { - out.print("[ERROR/%s] ".format(resp.getStatusCode)) + out.print("[ERROR/%s] %s".format(resp.getStatusCode, resp.getResponseBody)) } result \ "result" match { case JString(status) if status == "STATUS" => @@ -191,7 +197,7 @@ object JubaQLClient { req.either.apply() match { case Left(error) => // if request failed, print error - out.println("[ERROR] " + error.getCause + ": " + error.getMessage) + out.println("[ERROR] cause = " + error.getCause + ": message = " + error.getMessage) out.flush() true case Right(continue_?) =>