Skip to content

Commit

Permalink
Catch exceptions due to graph/coordinator termination (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
cshapeshifter committed May 25, 2013
1 parent 94ed772 commit 7d86c8c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/main/scala/com/signalcollect/DefaultGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class DefaultGraph[@specialized(Int, Long) Id: ClassTag, @specialized(Int, Long,
if (console != null) { console.setExecution(this) }
else {
println(
"warning: using interactive execution mode without console. To use the console,\n" +
"Warning: using interactive execution mode without console. To use the console,\n" +
" build the graph with: val graph = GraphBuilder.withConsole(true).build")
}

Expand Down Expand Up @@ -687,6 +687,7 @@ class DefaultGraph[@specialized(Int, Long) Id: ClassTag, @specialized(Int, Long,

def shutdown = {
parallelBootstrapNodeProxies.foreach(_.shutdown)
if (console != null) { console.shutdown }
system.shutdown
system.awaitTermination
}
Expand Down
68 changes: 40 additions & 28 deletions src/main/scala/com/signalcollect/console/ConsoleServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ class ConsoleServer[Id](graphConfiguration: GraphConfiguration) {

/** Stop both HTTP and WebSocket servers and exit */
def shutdown {
println("Stopping http server...")
server.stop(5)
println("Stopping WebSocket...")
sockets.stop(5000)
println("Stopping http server...")
server.stop(5)
}
}

Expand Down Expand Up @@ -402,36 +402,48 @@ class WebSocketConsoleServer[Id](port: InetSocketAddress, config: GraphConfigura
}

def onMessage(socket: WebSocket, msg: String) {
val j = parse(msg)
val p = (j \ "provider").extract[String]
def provider: DataProvider = coordinator match {
case Some(c) => p match {
case "configuration" => new ConfigurationDataProvider(this, c)
case "log" => new LogDataProvider(c)
case "graph" => new GraphDataProvider(c, j)
case "resources" => new ResourcesDataProvider(c, j)
case "state" => new StateDataProvider(this)
case "controls" => new ControlsProvider(this, j)
case "breakconditions" => new BreakConditionsProvider(c, this, j)
case otherwise => new InvalidDataProvider(msg, "invalid provider")
}
case None => p match {
case "state" => new StateDataProvider(this)
case otherwise => new NotReadyDataProvider(msg)
try {
val j = parse(msg)
val p = (j \ "provider").extract[String]
def provider: DataProvider = coordinator match {
case Some(c) => p match {
case "configuration" => new ConfigurationDataProvider(this, c)
case "log" => new LogDataProvider(c)
case "graph" => new GraphDataProvider(c, j)
case "resources" => new ResourcesDataProvider(c, j)
case "state" => new StateDataProvider(this)
case "controls" => new ControlsProvider(this, j)
case "breakconditions" => new BreakConditionsProvider(c, this, j)
case otherwise => new InvalidDataProvider(msg, "invalid provider")
}
case None => p match {
case "state" => new StateDataProvider(this)
case otherwise => new NotReadyDataProvider(msg)
}
}
}

try {
socket.send(compact(render(provider.fetch)))
try {
socket.send(compact(render(provider.fetch)))
} catch {
// Something went wrong during fetch, but it might be a minor exception
case e: NumberFormatException =>
socket.send(compact(render(
new InvalidDataProvider(msg, "number format exception").fetch)))
case e: Exception =>
socket.send(compact(render(
new ErrorDataProvider(e).fetch)))
case e: WebsocketNotConnectedException =>
println("Warning: Couldn't send message to websocket")
}
} catch {
case e: NumberFormatException =>
socket.send(compact(render(
new InvalidDataProvider(msg, "number format exception").fetch)))
// Something is wrong with the WebSocket or the graph/coordinator is already terminated
case e @ (_: java.lang.InterruptedException |
_: akka.pattern.AskTimeoutException |
_: org.java_websocket.exceptions.WebsocketNotConnectedException) =>
println("Warning: Console communication interrupted " +
"(likely due to graph shutdown)")
case e: Exception =>
socket.send(compact(render(
new ErrorDataProvider(e).fetch)))
case e: WebsocketNotConnectedException =>
println("Warning: Couldn't send message to websocket")
println("Warning: Unknown exception occured")
}
}

Expand Down

0 comments on commit 7d86c8c

Please sign in to comment.