-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new example showing how to gracefully stop Swoole servers in Docker
- Loading branch information
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM phpswoole/swoole | ||
|
||
COPY ./rootfilesystem/ / |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Gracefully Stop Swoole Servers In Docker Containers | ||
|
||
In this example, we will demonstrate how to gracefully stop Swoole servers in a Docker container. Note that we use | ||
`supervisord` to manage these Swoole servers in Docker containers. | ||
|
||
## How To Gracefully Stop Supervisord Programs In Docker Container | ||
|
||
### 1. Use command `exec` to run `supervisord` in the foreground. | ||
|
||
As we can see in the [entrypoint.sh] script, command `exec` is used to forward signals to `supervisord` processes: | ||
|
||
```bash | ||
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf -n # Run supervisord in the foreground. | ||
``` | ||
|
||
### 2. Set option `stopasgroup` to `true` in the `supervisord` configuration file(s). | ||
|
||
Also, please make sure that the `stopsignal` option is commented out or set to `TERM` (the default value), as we can see | ||
in the Supervisord configuration file [swoole.conf]: | ||
|
||
```ini | ||
stopasgroup=true | ||
; stopsignal=QUIT ; DON'T set this; it prevents graceful shutdown of Supervisord programs. | ||
``` | ||
|
||
## Docker Commands To Play With This Example | ||
|
||
```bash | ||
docker compose up # Start the Docker container and monitor the logs. | ||
|
||
# You will need a separate terminal to run the following commands. | ||
|
||
docker compose exec -ti app supervisorctl status # Check the status of Supervisord programs. | ||
docker compose exec -ti app curl -i http://127.0.0.1:9501 # Check the status of the Swoole HTTP server. | ||
|
||
docker compose exec -ti app supervisorctl signal SIGTERM swoole # Send SIGTERM signal to the Swoole HTTP server. | ||
docker compose kill --signal=SIGTERM # Send SIGTERM signal to the Docker container. | ||
docker compose stop # Stop the Docker container. | ||
``` | ||
|
||
[entrypoint.sh]: https://github.com/swoole/docker-swoole/blob/master/rootfilesystem/entrypoint.sh | ||
[swoole.conf]: https://github.com/swoole/docker-swoole/blob/master/rootfilesystem/etc/supervisor/service.d/swoole.conf |
5 changes: 5 additions & 0 deletions
5
examples/24-supervisord-gracefully-shutdown/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
version: '3' | ||
|
||
services: | ||
app: | ||
build: . |
26 changes: 26 additions & 0 deletions
26
examples/24-supervisord-gracefully-shutdown/rootfilesystem/var/www/server.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
$http = new Server('0.0.0.0', 9501); | ||
|
||
$http->on('request', function (Request $request, Response $response): void { | ||
$response->end('OK' . PHP_EOL); | ||
}); | ||
$http->on('workerStop', function (Server $http, int $workerId): void { | ||
// When the Docker container is stopped (e.g., by the `docker compose stop` command), event "onWorkerStop" is | ||
// triggered in each worker process, allowing graceful shutdown of Swoole workers. | ||
echo "Event \"onWorkerStop\" is triggered in worker #{$workerId}.", PHP_EOL; | ||
}); | ||
$http->on('shutdown', function (Server $http): void { | ||
// When the Docker container is stopped (e.g., by the `docker compose stop` command), event "onShutdown" is | ||
// triggered, allowing graceful shutdown of Swoole server. | ||
echo 'Event "onShutdown" is triggered.', PHP_EOL; | ||
}); | ||
|
||
$http->start(); |