Skip to content

Commit

Permalink
Added healthcheck to image that pings the ws server
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaRecon19 committed Jun 28, 2023
1 parent e1fe02e commit 7f398ec
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 181 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ USER 82

ENTRYPOINT ["php", "/app/server.php"]

HEALTHCHECK --interval=20s --timeout=1s --start-period=5s \
CMD php /app/healthcheck.php

COPY --from=composer /app /app
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"require": {
"cboden/ratchet": "dev-php82"
"cboden/ratchet": "dev-php82",
"ratchet/pawl": "^0.4.1"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/josh-gaby/Ratchet"
}
]
],
"replace": {
"symfony/polyfill-mbstring": "*",
"symfony/polyfill-php82": "*",
"symfony/polyfill-php81": "*",
"symfony/polyfill-php80": "*"
}
}
222 changes: 55 additions & 167 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions healthcheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App;

require __DIR__.'/vendor/autoload.php';

\Ratchet\Client\connect('ws://localhost:8089')->then(function($conn) {
$conn->send('ping');
$conn->close();
}, function ($e) {
echo "Could not connect:\n{$e->getMessage()}\n";
exit(1);
});
27 changes: 15 additions & 12 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ public function onOpen(ConnectionInterface $conn): void

public function onMessage(ConnectionInterface $from, $msg): void
{
/**
* @var \Ratchet\WebSocket\WsConnection $from
*/
$numRecv = count($this->clients) - 1;
echo sprintf(
'Sending message "%s" to %d other connection%s' . "\n",
$msg,
$numRecv,
$numRecv == 1 ? '' : 's'
);
// Don't log output if ping message
if ('ping' !== $msg) {
/**
* @var \Ratchet\WebSocket\WsConnection $from
*/
$numRecv = count($this->clients) - 1;
echo sprintf(
'Sending message "%s" to %d other connection%s' . "\n",
$msg,
$numRecv,
$numRecv == 1 ? '' : 's'
);
}

foreach ($this->clients as $client) {
if ($from !== $client) {
Expand Down Expand Up @@ -79,5 +82,5 @@ public function onError(ConnectionInterface $conn, \Exception $e): void
$port
);

echo 'Initializing websocket server';
$server->run();
echo "Initializing websocket server";
$server->run();

0 comments on commit 7f398ec

Please sign in to comment.