diff --git a/.env.example b/.env.example index b562493..a2ad8b0 100644 --- a/.env.example +++ b/.env.example @@ -14,4 +14,5 @@ SESSION_DRIVER=file REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null -REDIS_PORT=6379 \ No newline at end of file +REDIS_PORT=6379 +REDIS_DATABASE=0 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 38efb3a..c3694df 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ /.vscode /composer /db +/php /composer.lock *.bat *.log diff --git a/process/FileMonitor.php b/process/FileMonitor.php index 40e8404..8aac4f0 100644 --- a/process/FileMonitor.php +++ b/process/FileMonitor.php @@ -41,7 +41,9 @@ class FileMonitor */ public function __construct($monitor_dir, $monitor_extenstions) { - \file_put_contents(runtime_path() . \DIRECTORY_SEPARATOR . self::pidFile, \posix_getpid()); + if (function_exists('posix_getpid')) { + \file_put_contents(runtime_path() . \DIRECTORY_SEPARATOR . self::pidFile, \posix_getpid()); + } if (Worker::$daemonize) { #return; } diff --git a/process/Task.php b/process/Task.php index f7de1e5..ccaa06b 100644 --- a/process/Task.php +++ b/process/Task.php @@ -38,7 +38,9 @@ class Task public function __construct() { // 保存当前进程pid - \file_put_contents(runtime_path() . \DIRECTORY_SEPARATOR . self::pidFile, \posix_getpid()); + if (function_exists('posix_getpid')) { + \file_put_contents(runtime_path() . \DIRECTORY_SEPARATOR . self::pidFile, \posix_getpid()); + } // 初始化cron domainCron::onWorkerStart(); // 初始化计划任务的绝对路径 diff --git a/start.php b/start.php index 45593b1..90e3b8b 100644 --- a/start.php +++ b/start.php @@ -21,6 +21,8 @@ if (class_exists('app\\common\\ICheck')) { } else { exit('Class ICheck not found'.PHP_EOL); } +//不存在env时,复制一份 +is_file(__DIR__ . DIRECTORY_SEPARATOR . '.env') || copy(__DIR__ . DIRECTORY_SEPARATOR . '.env.example', __DIR__ . DIRECTORY_SEPARATOR . '.env'); if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) { Dotenv::createUnsafeImmutable(base_path())->load(); @@ -99,67 +101,67 @@ $worker->onWorkerStart = function ($worker) { $worker->onMessage = [$app, 'onMessage']; }; - -foreach (config('process', []) as $process_name => $config) { - $worker = new Worker($config['listen'] ?? null, $config['context'] ?? []); - $property_map = [ - 'count', - 'user', - 'group', - 'reloadable', - 'reusePort', - 'transport', - 'protocol', - ]; - $worker->name = $process_name; - foreach ($property_map as $property) { - if (isset($config[$property])) { - $worker->$property = $config[$property]; +if (!isWin()) { + foreach (config('process', []) as $process_name => $config) { + $worker = new Worker($config['listen'] ?? null, $config['context'] ?? []); + $property_map = [ + 'count', + 'user', + 'group', + 'reloadable', + 'reusePort', + 'transport', + 'protocol', + ]; + $worker->name = $process_name; + foreach ($property_map as $property) { + if (isset($config[$property])) { + $worker->$property = $config[$property]; + } } + + $worker->onWorkerStart = function ($worker) use ($config) { + foreach (config('autoload.files', []) as $file) { + include_once $file; + } + Dotenv::createMutable(base_path())->load(); + Config::reload(config_path(), ['route']); + + $bootstrap = $config['bootstrap'] ?? config('bootstrap', []); + if (!in_array(support\bootstrap\Log::class, $bootstrap)) { + $bootstrap[] = support\bootstrap\Log::class; + } + foreach ($bootstrap as $class_name) { + /** @var \Webman\Bootstrap $class_name */ + $class_name::start($worker); + } + + foreach ($config['services'] ?? [] as $server) { + if (!class_exists($server['handler'])) { + echo "process error: class {$server['handler']} not exists\r\n"; + continue; + } + $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []); + if (isset($server['listen'])) { + echo "listen: {$server['listen']}\n"; + } + $class = Container::make($server['handler'], $server['constructor'] ?? []); + worker_bind($listen, $class); + $listen->listen(); + } + + if (isset($config['handler'])) { + if (!class_exists($config['handler'])) { + echo "process error: class {$config['handler']} not exists\r\n"; + return; + } + + $class = Container::make($config['handler'], $config['constructor'] ?? []); + worker_bind($worker, $class); + } + + }; } - - $worker->onWorkerStart = function ($worker) use ($config) { - foreach (config('autoload.files', []) as $file) { - include_once $file; - } - Dotenv::createMutable(base_path())->load(); - Config::reload(config_path(), ['route']); - - $bootstrap = $config['bootstrap'] ?? config('bootstrap', []); - if (!in_array(support\bootstrap\Log::class, $bootstrap)) { - $bootstrap[] = support\bootstrap\Log::class; - } - foreach ($bootstrap as $class_name) { - /** @var \Webman\Bootstrap $class_name */ - $class_name::start($worker); - } - - foreach ($config['services'] ?? [] as $server) { - if (!class_exists($server['handler'])) { - echo "process error: class {$server['handler']} not exists\r\n"; - continue; - } - $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []); - if (isset($server['listen'])) { - echo "listen: {$server['listen']}\n"; - } - $class = Container::make($server['handler'], $server['constructor'] ?? []); - worker_bind($listen, $class); - $listen->listen(); - } - - if (isset($config['handler'])) { - if (!class_exists($config['handler'])) { - echo "process error: class {$config['handler']} not exists\r\n"; - return; - } - - $class = Container::make($config['handler'], $config['constructor'] ?? []); - worker_bind($worker, $class); - } - - }; } - Worker::runAll(); diff --git a/task.php b/task.php new file mode 100644 index 0000000..a0cc1ee --- /dev/null +++ b/task.php @@ -0,0 +1,116 @@ +load(); +} else { + Dotenv::createMutable(base_path())->load(); +} + +Config::load(config_path(), ['route', 'container']); +$config = config('server'); + +if ($timezone = config('app.default_timezone')) { + date_default_timezone_set($timezone); +} + +Worker::$onMasterReload = function (){ + if (function_exists('opcache_get_status')) { + if ($status = opcache_get_status()) { + if (isset($status['scripts']) && $scripts = $status['scripts']) { + foreach (array_keys($scripts) as $file) { + opcache_invalidate($file, true); + } + } + } + } +}; +Worker::$logFile = $config['log_file'] ?? ''; +Worker::$pidFile = $config['pid_file']; +Worker::$stdoutFile = $config['stdout_file']; +TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10*1024*1024; + +$process_name = 'IYUUTask'; +$config = config('process.IYUUTask', []); +$worker = new Worker($config['listen'] ?? null, $config['context'] ?? []); +$property_map = [ + 'count', + 'user', + 'group', + 'reloadable', + 'reusePort', + 'transport', + 'protocol', +]; +$worker->name = $process_name; +foreach ($property_map as $property) { + if (isset($config[$property])) { + $worker->$property = $config[$property]; + } +} + +$worker->onWorkerStart = function ($worker) use ($config) { + foreach (config('autoload.files', []) as $file) { + include_once $file; + } + Dotenv::createMutable(base_path())->load(); + Config::reload(config_path(), ['route']); + + $bootstrap = $config['bootstrap'] ?? config('bootstrap', []); + if (!in_array(support\bootstrap\Log::class, $bootstrap)) { + $bootstrap[] = support\bootstrap\Log::class; + } + foreach ($bootstrap as $class_name) { + /** @var \Webman\Bootstrap $class_name */ + $class_name::start($worker); + } + + foreach ($config['services'] ?? [] as $server) { + if (!class_exists($server['handler'])) { + echo "process error: class {$server['handler']} not exists\r\n"; + continue; + } + $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []); + if (isset($server['listen'])) { + echo "listen: {$server['listen']}\n"; + } + $class = Container::make($server['handler'], $server['constructor'] ?? []); + worker_bind($listen, $class); + $listen->listen(); + } + + if (isset($config['handler'])) { + if (!class_exists($config['handler'])) { + echo "process error: class {$config['handler']} not exists\r\n"; + return; + } + + $class = Container::make($config['handler'], $config['constructor'] ?? []); + worker_bind($worker, $class); + } +}; + +Worker::runAll(); diff --git a/windows_start.cmd b/windows_start.cmd index 3f157b7..ef091dd 100644 --- a/windows_start.cmd +++ b/windows_start.cmd @@ -1,4 +1,4 @@ @echo off chcp 65001 -php %~dp0start.php start +php %~dp0start.php %~dp0task.php pause \ No newline at end of file