php使用goto實現自動重啟swoole、reactphp、workerman服務的程式碼
阿新 • • 發佈:2020-04-14
在平時使用swoole進行開發中,常常遇到這種問題,改了程式碼之後,手動ctrl+c中斷服務,再敲命令重啟服務。頻繁地重啟,感覺心很累。
php提供了inotify擴充套件,呼叫linux的inotify系統呼叫,監控檔案的變化.
這時候就產生了一個想法,我開一個主程序監控檔案變化,再開一個子程序執行swoole服務。主程序監聽到檔案變化之後,幹掉子程序,然後再開一個子程序執行swoole服務. 子程序如果想優雅地退出,安裝個訊號處理器,在退出之前做一些操作。
<?php //index.php require './vendor/autoload.php'; Restart: $pid = pcntl_fork(); if ($pid > 0) { $fd = inotify_init(); $watch_descriptor = inotify_add_watch($fd,'./src/',IN_MODIFY); $events = inotify_read($fd); posix_kill($pid,SIGTERM); fclose($fd); pcntl_wait($status); goto Restart; } elseif ($pid == 0) { \Church\Application::run(); } else { exit(0); }
<?php namespace Church; /** use Psr\Http\Message\ServerRequestInterface; use React\Http\Response; use React\Http\Server; **/ class Application { public static function run() { /** $loop = \React\EventLoop\Factory::create(); $loop->addSignal(SIGTERM,function() use ($loop) { $loop->stop(); }); $server = new Server(function (ServerRequestInterface $request) { return new Response( 200,array( 'Content-Type' => 'text/plain' ),"Hello World1!\n" ); }); $socket = new \React\Socket\Server(8080,$loop); $server->listen($socket); $loop->run(); **/ //高效能HTTP伺服器 $http = new \Swoole\Http\Server("127.0.0.1",9501); $http->on("start",function ($server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $http->on("request",function ($request,$response) { $response->header("Content-Type","text/plain"); $response->end("Hello World1\n"); }); $http->start(); } }
個人覺得這裡最優雅的實現方式應該是用GOTO了。
到此這篇關於php使用goto實現自動重啟swoole、reactphp、workerman服務的程式碼的文章就介紹到這了,更多相關php自動重啟swoole、reactphp、workerman服務內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!