FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Properly handle shutdown requests. · DrJavaAtRice/Java8@d830dd1 · GitHub

Repository navigation

Commit d830dd1

Browse files
committed
Properly handle shutdown requests.
1 parent 3dee5cb commit d830dd1

2 files changed

Lines changed: 50 additions & 20 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package edu.rice.cs.dynamicjava.kernel;
2+
3+
public interface IKernelRunnable extends Runnable {
4+
public void start();
5+
public void shutdown();
6+
}

‎dynamicjava/src/edu/rice/cs/dynamicjava/kernel/Kernel.java‎

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
public class Kernel {
2020
private final UUID session;
2121

22-
private final Thread heartbeat;
23-
private final Thread executor;
22+
private final IKernelRunnable heartbeat;
23+
private final IKernelRunnable executor;
2424
private final Thread control;
2525

26+
private volatile boolean isRunning = true;
27+
2628
public Kernel(String configPath, Interpreter i) throws FileNotFoundException {
2729
session = UUID.randomUUID();
2830

@@ -43,41 +45,43 @@ public Kernel(String configPath, Interpreter i) throws FileNotFoundException {
4345
executor = new Executor(i, session, ctx, shellAddr, iopubAddr);
4446

4547
String controlAddr = String.format("%s://%s:%d", transport, ip, config.getInt("control_port"));
46-
control = new Control(ctx, controlAddr);
48+
control = new Control(heartbeat, executor, session, ctx, controlAddr);
49+
}
4750

48-
// stdin = ctx.createSocket(ZMQ.ROUTER);
49-
// stdin.bind(String.format("%s://%s:%d", transport, ip, config.getInt("stdin_port")));
50-
//
51-
// iopub = ctx.createSocket(ZMQ.PUB);
52-
// iopub.bind();
51+
private void shutdown() {
52+
isRunning = false;
5353
}
5454

55-
private static class Heartbeat extends Thread {
56-
private final ZContext ctx;
55+
private static class Heartbeat extends Thread implements IKernelRunnable {
5756
private final ZMQ.Socket skt;
57+
private volatile boolean isRunning = true;
5858

5959
public Heartbeat(ZContext ctx, String addr) {
60-
this.ctx = ctx;
6160
skt = ctx.createSocket(ZMQ.REP);
6261
skt.bind(addr);
6362
}
6463

64+
public void shutdown() {
65+
isRunning = false;
66+
}
67+
6568
@Override
6669
public void run() {
67-
while (true) {
70+
while (isRunning) {
6871
byte[] msg = skt.recv();
6972
skt.send(msg);
7073
}
7174
}
7275
}
7376

74-
private static class Executor extends Thread {
77+
private static class Executor extends Thread implements IKernelRunnable {
7578
private final Interpreter i;
7679
private final UUID session;
7780
private final ZMQ.Socket shellSkt;
7881
private final ZMQ.Socket iopubSkt;
7982

8083
private int execCount = 1;
84+
private volatile boolean isRunning = true;
8185

8286
public Executor(Interpreter i, UUID session, ZContext ctx, String shellAddr, String iopubAddr) {
8387
this.i = i;
@@ -89,9 +93,13 @@ public Executor(Interpreter i, UUID session, ZContext ctx, String shellAddr, Str
8993
iopubSkt.bind(iopubAddr);
9094
}
9195

96+
public void shutdown() {
97+
isRunning = false;
98+
}
99+
92100
@Override
93101
public void run() {
94-
while (true) {
102+
while (isRunning) {
95103
Message msg = new Message(ZMsg.recvMsg(shellSkt));
96104

97105
String msgType = msg.getHeader().getString("msg_type");
@@ -169,27 +177,43 @@ public void run() {
169177
msg.respond(session, "execute_reply", retContent).serialize().send(shellSkt);
170178
}
171179
execCount += 1;
172-
} else {
180+
} else if (msgType.equals("shutdown_request")) { }
181+
else {
173182
System.out.format("ERROR: Unknown message type: %s\n", msgType);
174183
}
175184
}
176185
}
177186
}
178187

179188
private static class Control extends Thread {
180-
private final ZContext ctx;
189+
private final UUID session;
181190
private final ZMQ.Socket skt;
191+
private final IKernelRunnable heartbeat;
192+
private final IKernelRunnable executor;
182193

183-
public Control(ZContext ctx, String addr) {
184-
this.ctx = ctx;
194+
public Control(IKernelRunnable heartbeat, IKernelRunnable executor, UUID session, ZContext ctx, String addr) {
195+
this.heartbeat = heartbeat;
196+
this.executor = executor;
197+
this.session = session;
185198
skt = ctx.createSocket(ZMQ.ROUTER);
186199
skt.bind(addr);
187200
}
188201

189202
@Override
190203
public void run() {
191-
Message msg = new Message(ZMsg.recvMsg(skt));
192-
System.out.println(msg.toString());
204+
while (true) {
205+
Message msg = new Message(ZMsg.recvMsg(skt));
206+
String msgType = msg.getHeader().getString("msg_type");
207+
if (msgType.equals("shutdown_request")) {
208+
executor.shutdown();
209+
heartbeat.shutdown();
210+
211+
JSONObject retContent = new JSONObject();
212+
retContent.put("restart", "false");
213+
msg.respond(session, "shutdown_reply", retContent).serialize().send(skt);
214+
return;
215+
}
216+
}
193217
}
194218
}
195219

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL