Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 6fbac23

Browse files
committed
Added handling of shutdown request
1 parent bb7b4ad commit 6fbac23

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

src/Actions/ShutdownAction.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@
33

44
namespace Litipk\JupyterPHP\Actions;
55

6+
use Litipk\JupyterPHP\JupyterBroker;
7+
use React\ZMQ\SocketWrapper;
68

79
final class ShutdownAction implements Action
810
{
11+
/** @var JupyterBroker */
12+
private $broker;
13+
14+
/** @var SocketWrapper */
15+
private $shellSocket;
16+
17+
/** @var SocketWrapper */
18+
private $iopubSocket;
19+
20+
public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket)
21+
{
22+
$this->broker = $broker;
23+
$this->iopubSocket = $iopubSocket;
24+
$this->shellSocket = $shellSocket;
25+
}
26+
927
public function call(array $header, array $content, $zmqId = null)
1028
{
11-
// TODO: Implement call() method.
29+
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'busy'], $header);
30+
31+
$replyContent = ['restart' => $content['restart']];
32+
$this->broker->send($this->shellSocket, 'shutdown_reply', $replyContent, $header, [], $zmqId);
33+
34+
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $header);
1235
}
1336
}

src/Handlers/ShellMessagesHandler.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Litipk\JupyterPHP\Handlers;
55

6-
76
use Litipk\JupyterPHP\Actions\ExecuteAction;
87
use Litipk\JupyterPHP\Actions\HistoryAction;
98
use Litipk\JupyterPHP\Actions\KernelInfoAction;
@@ -15,7 +14,6 @@
1514
use Psy\Shell;
1615
use React\ZMQ\SocketWrapper;
1716

18-
1917
final class ShellMessagesHandler
2018
{
2119
/** @var ExecuteAction */
@@ -44,23 +42,25 @@ final class ShellMessagesHandler
4442
* @param Logger $logger
4543
*/
4644
public function __construct(
47-
JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket, Logger $logger
48-
)
49-
{
45+
JupyterBroker $broker,
46+
SocketWrapper $iopubSocket,
47+
SocketWrapper $shellSocket,
48+
Logger $logger
49+
) {
5050
$this->shellSoul = new Shell();
51-
51+
5252
$this->executeAction = new ExecuteAction($broker, $iopubSocket, $shellSocket, $this->shellSoul);
5353
$this->historyAction = new HistoryAction($broker, $shellSocket);
5454
$this->kernelInfoAction = new KernelInfoAction($broker, $shellSocket, $iopubSocket);
55-
$this->shutdownAction = new ShutdownAction($broker, $shellSocket);
56-
55+
$this->shutdownAction = new ShutdownAction($broker, $iopubSocket, $shellSocket);
56+
5757
$this->logger = $logger;
5858

5959
$broker->send(
6060
$iopubSocket, 'status', ['execution_state' => 'starting'], []
6161
);
6262

63-
$this->shellSoul->setOutput( new KernelOutput($this->executeAction, $this->logger->withName('KernelOutput')));
63+
$this->shellSoul->setOutput(new KernelOutput($this->executeAction, $this->logger->withName('KernelOutput')));
6464
}
6565

6666
/**
@@ -74,14 +74,14 @@ public function __invoke(array $msg)
7474
$content = json_decode($content, true);
7575

7676
$this->logger->debug('Received message', [
77-
'processId' => getmypid(),
78-
'zmqId' => htmlentities($zmqId, ENT_COMPAT, "UTF-8"),
79-
'delim' => $delim,
80-
'hmac' => $hmac,
81-
'header' => $header,
77+
'processId' => getmypid(),
78+
'zmqId' => htmlentities($zmqId, ENT_COMPAT, "UTF-8"),
79+
'delim' => $delim,
80+
'hmac' => $hmac,
81+
'header' => $header,
8282
'parentHeader' => $parentHeader,
83-
'metadata' => $metadata,
84-
'content' => $content
83+
'metadata' => $metadata,
84+
'content' => $content
8585
]);
8686

8787
if ('kernel_info_request' === $header['msg_type']) {

src/JupyterBroker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function send(
7676
json_encode(empty($content) ? new \stdClass : $content),
7777
];
7878

79-
if ($zmqId !== null) {
79+
if (null !== $zmqId) {
8080
$finalMsg = [$zmqId];
8181
} else {
8282
$finalMsg = [];

0 commit comments

Comments
 (0)