diff --git a/README.md b/README.md index 1fe6ae0..c0e976a 100644 --- a/README.md +++ b/README.md @@ -48,13 +48,13 @@ php artisan test2 ```php get(ProcessManager::class)->run(TestProcess::class,$processNum); + } +} diff --git a/app/Console/Command/Test2Consumer.php b/app/Console/Command/Test2Consumer.php index 98dda85..c946fcd 100644 --- a/app/Console/Command/Test2Consumer.php +++ b/app/Console/Command/Test2Consumer.php @@ -1,14 +1,14 @@ testService->testDi(); - return apiResponse([]); - } -} diff --git a/app/Controller/MqController.php b/app/Controller/MqController.php index 0df4755..22bcd2e 100644 --- a/app/Controller/MqController.php +++ b/app/Controller/MqController.php @@ -1,14 +1,14 @@ toDateTimeString().PHP_EOL; + sleep(1); + } + } +} \ No newline at end of file diff --git a/app/Provider/EventServiceProvider.php b/app/Provider/EventServiceProvider.php index 1a429ea..0801e48 100644 --- a/app/Provider/EventServiceProvider.php +++ b/app/Provider/EventServiceProvider.php @@ -1,15 +1,15 @@ get($key)) { + if ($result = \App\Utils\Redis::connection()->get($key)) { return unserialize($result); } $result = $closure($ttl); - \app\Utils\Redis::connection()->set($key, serialize($result), $ttl); + \App\Utils\Redis::connection()->set($key, serialize($result), $ttl); return $result; } } function getRedis() { - return \app\Utils\Redis::connection(); + return \App\Utils\Redis::connection(); } @@ -635,7 +635,7 @@ function request(): SymfonyRequest function reponse(): SymfonyResponse { - return \dcr\Response\Response::instance(); + return \Dcr\Response\Response::instance(); } diff --git a/artisan b/artisan index ca32adb..7a3173c 100755 --- a/artisan +++ b/artisan @@ -1,13 +1,13 @@ #!/usr/bin/env php make(HttpKernel::class)->run($container); - $response = dcr\Response\Response::instance(); + $response = Dcr\Response\Response::instance(); $responseStr = is_array($responseStr) ? json_encode($responseStr) : $responseStr; if ($responseStr instanceof SymfonyResponse) { return $responseStr; diff --git a/bin/startWs.php b/bin/startWs.php index d568c23..556044b 100644 --- a/bin/startWs.php +++ b/bin/startWs.php @@ -1,13 +1,13 @@ make(\dcr\Boostrap::class); + $bootstrap = $container->make(\Dcr\Boostrap::class); $bootstrap->run(); } diff --git a/composer.json b/composer.json index 72f7497..7645c99 100644 --- a/composer.json +++ b/composer.json @@ -59,7 +59,8 @@ "workerman/crontab": "^1.0", "workerman/gateway-worker": "^3.0.0", "workerman/gatewayclient": "^3.0", - "workerman/workerman": "^3.5" + "workerman/workerman": "^3.5", + "ext-posix": "*" }, "require-dev": { "phpunit/php-timer": "^5.0", @@ -75,8 +76,8 @@ }, "autoload": { "psr-4": { - "app\\": "app/", - "dcr\\": "dcr/" + "App\\": "app/", + "Dcr\\": "dcr/" }, "files": [ "app/helpers.php" @@ -89,7 +90,8 @@ }, "scripts": { "post-autoload-dump": [ - "cp -r -f ./fix/AbstractHandler.php ./vendor/inhere/console/src/Handler" + "cp -r -f ./fix/AbstractHandler.php ./vendor/inhere/console/src/Handler", + "cp -r -f ./fix/Event.php ./vendor/workerman/workerman/Events/" ], "serve": [ "@php -S 127.0.0.1:8001 -t ./public" diff --git a/config/config.php b/config/config.php index 1f7254e..daea838 100755 --- a/config/config.php +++ b/config/config.php @@ -21,6 +21,6 @@ ], 'aspects' => [ // 这里写入对应的 Aspect - app\Aspect\DebugAspect::class, + App\Aspect\DebugAspect::class, ] ]; \ No newline at end of file diff --git a/config/crontab.php b/config/crontab.php index 58908ad..2af5db3 100755 --- a/config/crontab.php +++ b/config/crontab.php @@ -1,7 +1,7 @@ define(app\Model\UserModel::class, function (Faker $faker) { +$factory->define(App\Model\UserModel::class, function (Faker $faker) { return [ 'name' => $faker->name, 'title' => $faker->sentence($nbWords = 6, $variableNbWords = true), diff --git a/database/migrations/Version20230102054754.php b/database/migrations/Version20230102054754.php index db9ca3a..4845d1f 100644 --- a/database/migrations/Version20230102054754.php +++ b/database/migrations/Version20230102054754.php @@ -4,7 +4,7 @@ namespace database\migrations; -use app\Model\UserModel; +use App\Model\UserModel; use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; //use Illuminate\Support\Facades\DB; // 不能用laravel的门面 diff --git a/dcr/Annotation/Mapping/AbstractAnnotation.php b/dcr/Annotation/Mapping/AbstractAnnotation.php index 9d41f1b..b9a5da7 100644 --- a/dcr/Annotation/Mapping/AbstractAnnotation.php +++ b/dcr/Annotation/Mapping/AbstractAnnotation.php @@ -1,12 +1,12 @@ get(RouteAnnotation::class)->handle(); - header('Content-type: text/html; charset=utf-8'); $this->loadDotEnv(); - \dcr\Router::load(PROJECT_ROOT.'routes'); + \Dcr\Router::load(PROJECT_ROOT.'routes'); $config = Config::get('app'); define('DEBUG', $config['debug']); // online set false diff --git a/dcr/CommandBootstrap.php b/dcr/CommandBootstrap.php index 7bef6f9..db18877 100644 --- a/dcr/CommandBootstrap.php +++ b/dcr/CommandBootstrap.php @@ -1,14 +1,14 @@ 0) { // 在父进程中 + cli_set_process_title($className); +// pcntl_waitpid($pid, $status, WNOHANG); + // 给父进程安装一个SIGCHLD信号处理器 + // 想在子进程中执行任务,这种写法子进程如果不报错就会是固定数量定时运行,如果报错一个子进程就少一个 需要处理 + pcntl_signal(SIGCHLD, static function () use ($pid) { + pcntl_waitpid($pid, $status, WNOHANG); + }); + // 父进程不断while循环,去反复执行pcntl_waitpid(),从而试图解决已经退出的子进程 + while (true) { + pcntl_signal_dispatch(); + } + } elseif (0 === $pid) { + (new $className())->hook(); + } + } + } +} \ No newline at end of file diff --git a/dcr/Request.php b/dcr/Request.php index 0617a10..6fa0601 100644 --- a/dcr/Request.php +++ b/dcr/Request.php @@ -1,13 +1,13 @@ [ diff --git a/doc/event.md b/doc/event.md index a271b4b..43eb998 100644 --- a/doc/event.md +++ b/doc/event.md @@ -30,7 +30,7 @@ get(ProcessManager::class)->run(TestProcess::class,$processNum); + } +} + +``` + + +```php +toDateTimeString().PHP_EOL; + sleep(1); + } + } +} +``` \ No newline at end of file diff --git a/fix/Event.php b/fix/Event.php new file mode 100644 index 0000000..9380a21 --- /dev/null +++ b/fix/Event.php @@ -0,0 +1,219 @@ + + * @copyright 有个鬼<42765633@qq.com> + * @link http://www.workerman.net/ + * @license http://www.opensource.org/licenses/mit-license.php MIT License + */ +namespace Workerman\Events; + +use Workerman\Worker; + +/** + * libevent eventloop + */ +class Event implements EventInterface +{ + /** + * Event base. + * @var object + */ + protected $_eventBase = null; + + /** + * All listeners for read/write event. + * @var array + */ + protected $_allEvents = array(); + + /** + * Event listeners of signal. + * @var array + */ + protected $_eventSignal = array(); + + /** + * All timer event listeners. + * [func, args, event, flag, time_interval] + * @var array + */ + protected $_eventTimer = array(); + + /** + * Timer id. + * @var int + */ + protected static $_timerId = 1; + + /** + * construct + * @return void + */ + public function __construct() + { + if (\class_exists('\\\\EventBase', false)) { + $class_name = '\\\\EventBase'; + } else { + $class_name = '\EventBase'; + } + $this->_eventBase = new $class_name(); + } + + /** + * @see EventInterface::add() + */ + public function add($fd, $flag, $func, $args=array()) + { + if (\class_exists('\\\\Event', false)) { + $class_name = '\\\\Event'; + } else { + $class_name = '\Event'; + } + switch ($flag) { + case self::EV_SIGNAL: + + $fd_key = (int)$fd; + $event = $class_name::signal($this->_eventBase, $fd, $func); + if (!$event||!$event->add()) { + return false; + } + $this->_eventSignal[$fd_key] = $event; + return true; + + case self::EV_TIMER: + case self::EV_TIMER_ONCE: + + $param = array($func, (array)$args, $flag, $fd, self::$_timerId); + $event = new $class_name($this->_eventBase, -1, $class_name::TIMEOUT|$class_name::PERSIST, array($this, "timerCallback"), $param); + if (!$event||!$event->addTimer($fd)) { + return false; + } + $this->_eventTimer[self::$_timerId] = $event; + return self::$_timerId++; + + default : + $fd_key = (int)$fd; + $real_flag = $flag === self::EV_READ ? $class_name::READ | $class_name::PERSIST : $class_name::WRITE | $class_name::PERSIST; + $event = new $class_name($this->_eventBase, $fd, $real_flag, $func, $fd); + if (!$event||!$event->add()) { + return false; + } + $this->_allEvents[$fd_key][$flag] = $event; + return true; + } + } + + /** + * @see Events\EventInterface::del() + */ + public function del($fd, $flag) + { + switch ($flag) { + + case self::EV_READ: + case self::EV_WRITE: + + $fd_key = (int)$fd; + if (isset($this->_allEvents[$fd_key][$flag])) { + $this->_allEvents[$fd_key][$flag]->del(); + unset($this->_allEvents[$fd_key][$flag]); + } + if (empty($this->_allEvents[$fd_key])) { + unset($this->_allEvents[$fd_key]); + } + break; + + case self::EV_SIGNAL: + $fd_key = (int)$fd; + if (isset($this->_eventSignal[$fd_key])) { + $this->_eventSignal[$fd_key]->del(); + unset($this->_eventSignal[$fd_key]); + } + break; + + case self::EV_TIMER: + case self::EV_TIMER_ONCE: + if (isset($this->_eventTimer[$fd])) { + $this->_eventTimer[$fd]->del(); + unset($this->_eventTimer[$fd]); + } + break; + } + return true; + } + + /** + * Timer callback. + * @param null $fd + * @param int $what + * @param int $timer_id + */ + public function timerCallback($fd, $what, $param) + { + $timer_id = $param[4]; + + if ($param[2] === self::EV_TIMER_ONCE) { + $this->_eventTimer[$timer_id]->del(); + unset($this->_eventTimer[$timer_id]); + } + + try { + \call_user_func_array($param[0], $param[1]); + } catch (\Exception $e) { + Worker::log($e); + exit(250); + } catch (\Error $e) { + Worker::log($e); + exit(250); + } + } + + /** + * @see Events\EventInterface::clearAllTimer() + * @return void + */ + public function clearAllTimer() + { + foreach ($this->_eventTimer as $event) { + $event->del(); + } + $this->_eventTimer = array(); + } + + + /** + * @see EventInterface::loop() + */ + public function loop() + { + @$this->_eventBase->loop(); + } + + /** + * Destroy loop. + * + * @return void + */ + public function destroy() + { + foreach ($this->_eventSignal as $event) { + $event->del(); + } + } + + /** + * Get timer count. + * + * @return integer + */ + public function getTimerCount() + { + return \count($this->_eventTimer); + } +} diff --git a/migrate.php b/migrate.php index bd8cc89..6e838cc 100644 --- a/migrate.php +++ b/migrate.php @@ -3,9 +3,9 @@ require_once __DIR__.'/vendor/autoload.php'; -use app\Utils\Config; -use dcr\Boostrap; -use dcr\Container; +use App\Utils\Config; +use Dcr\Boostrap; +use Dcr\Container; use Doctrine\DBAL\DriverManager; use Doctrine\Migrations\Configuration\Configuration; use Doctrine\Migrations\Configuration\Connection\ExistingConnection; diff --git a/public/index.php b/public/index.php index 5d9e2fc..eaf2a6a 100644 --- a/public/index.php +++ b/public/index.php @@ -2,12 +2,14 @@ require __DIR__.'/../vendor/autoload.php'; -use app\Utils\Json; -use dcr\Boostrap; -use dcr\Container; -use dcr\HttpKernel; +use App\Utils\Json; +use Dcr\Boostrap; +use Dcr\Container; +use Dcr\HttpKernel; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; define('PROJECT_ROOT', dirname(__DIR__).'/'); +! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); +header('Content-type: text/html; charset=utf-8'); $container = Container::instance(); @@ -27,7 +29,7 @@ } // response -$response = dcr\Response\Response::instance(); +$response = Dcr\Response\Response::instance(); $response->setContent($responseStr); $response->send(); diff --git a/routes/api.php b/routes/api.php index 685288e..b65a999 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,28 +1,18 @@ middleware(TestMiddleware::class); -//\dcr\facade\Route::addRoute('abc',[HomeController::class,'test2']); -//Route::addRoute('abc',[HomeController::class,'test2']); - - - - -//Router::addRoute(['GET', 'POST'],'/test',[TestController::class,'test2'])->middleware(TestMiddleware::class); -//Router::addRoute(['GET', 'POST'],'/test2',[TestController::class,'test2']); -//Router::addRoute(['GET', 'POST'],'/mq/test1',[MqController::class,'test1']); -// - -//\dcr\Route::getRoutes(); +//\Dcr\Route::getRoutes();