-
Notifications
You must be signed in to change notification settings - Fork 24
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 "interruptible sleep"
Signed-off-by: Demin Yin <deminy@deminy.net>
- Loading branch information
Showing
4 changed files
with
74 additions
and
1 deletion.
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
12 changes: 12 additions & 0 deletions
12
dockerfiles/server/rootfilesystem/etc/supervisor/service.d/interruptible-sleep.conf
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,12 @@ | ||
[supervisord] | ||
user = root | ||
|
||
[program:interruptible-sleep] | ||
command = /var/www/servers/interruptible-sleep.php | ||
user = root | ||
autostart = true | ||
autorestart = true | ||
stdout_logfile=/proc/self/fd/1 | ||
stdout_logfile_maxbytes=0 | ||
stderr_logfile=/proc/self/fd/1 | ||
stderr_logfile_maxbytes=0 |
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,60 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* In this example, we start a web server with a cronjob setup to run every 19 seconds. The cron job sends a message "[INTERRUPTIBLE-SLEEP] Simulating cronjob execution.(case 1)" to the logs of the server container when executed. | ||
* | ||
* The problem with traditional cronjobs is that they don't get a chance to execute at a last time before the server | ||
* shuts down. For example, if the cronjob is scheduled to run every 19 seconds, and the server is shutting down 15 | ||
* seconds after the last cronjob execution, then the cronjob will never get a chance to execute one more time before | ||
* the server shuts down. | ||
* | ||
* In this example, we use Channel to schedule a cronjob to run every 19 seconds. The implementation allows the cronjob | ||
* to execute one more time by checking if the Channel is closed when server shuts down. | ||
* | ||
* When running the following command to stop and restart the server, you will see the cronjob is executed one more time | ||
* with message "[INTERRUPTIBLE-SLEEP] Simulating cronjob execution.(case 2)" logged in the logs of the server container: | ||
* docker compose exec -t server bash -c "supervisorctl restart interruptible-sleep" | ||
*/ | ||
|
||
use Swoole\Coroutine; | ||
use Swoole\Coroutine\Channel; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
$exited = new Channel(); | ||
$server = new Server('0.0.0.0', 9512); | ||
|
||
$server->on('workerStart', function (Server $server, int $workerId) use ($exited) { | ||
if ($workerId === 0) { | ||
Coroutine::create(function () use ($exited) { | ||
// Here we start the second cron job that makes an HTTP request every 19 seconds. | ||
while (true) { | ||
echo '[INTERRUPTIBLE-SLEEP] Simulating cronjob execution. (case 1)', PHP_EOL; | ||
$exited->pop(19); | ||
if ($exited->errCode === SWOOLE_CHANNEL_CLOSED) { | ||
echo '[INTERRUPTIBLE-SLEEP] Simulating cronjob execution. (case 2)', PHP_EOL; | ||
break; | ||
} | ||
} | ||
echo '[INTERRUPTIBLE-SLEEP] The cronjob has exited.', PHP_EOL; | ||
}); | ||
} | ||
}); | ||
$server->on('workerExit', function (Server $server, int $workerId) use ($exited) { | ||
echo "[INTERRUPTIBLE-SLEEP] Worker #{$workerId} is exiting.", PHP_EOL; | ||
if ($workerId === 0) { | ||
Coroutine::create(function () use ($exited) { | ||
$exited->close(); | ||
}); | ||
} | ||
echo "[INTERRUPTIBLE-SLEEP] Worker #{$workerId} has exited.", PHP_EOL; | ||
}); | ||
$server->on('request', function (Request $request, Response $response) { | ||
$response->end('OK' . PHP_EOL); | ||
}); | ||
|
||
$server->start(); |