windows.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Start file for windows
  4. */
  5. require_once __DIR__ . '/vendor/autoload.php';
  6. use Dotenv\Dotenv;
  7. use support\App;
  8. ini_set('display_errors', 'on');
  9. error_reporting(E_ALL);
  10. if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
  11. if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
  12. Dotenv::createUnsafeImmutable(base_path())->load();
  13. } else {
  14. Dotenv::createMutable(base_path())->load();
  15. }
  16. }
  17. App::loadAllConfig(['route']);
  18. $errorReporting = config('app.error_reporting');
  19. if (isset($errorReporting)) {
  20. error_reporting($errorReporting);
  21. }
  22. $runtimeProcessPath = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
  23. if (!is_dir($runtimeProcessPath)) {
  24. mkdir($runtimeProcessPath);
  25. }
  26. $processFiles = [];
  27. if (config('server.listen')) {
  28. $processFiles[] = __DIR__ . DIRECTORY_SEPARATOR . 'start.php';
  29. }
  30. foreach (config('process', []) as $processName => $config) {
  31. $processFiles[] = write_process_file($runtimeProcessPath, $processName, '');
  32. }
  33. foreach (config('plugin', []) as $firm => $projects) {
  34. foreach ($projects as $name => $project) {
  35. if (!is_array($project)) {
  36. continue;
  37. }
  38. foreach ($project['process'] ?? [] as $processName => $config) {
  39. $processFiles[] = write_process_file($runtimeProcessPath, $processName, "$firm.$name");
  40. }
  41. }
  42. foreach ($projects['process'] ?? [] as $processName => $config) {
  43. $processFiles[] = write_process_file($runtimeProcessPath, $processName, $firm);
  44. }
  45. }
  46. function write_process_file($runtimeProcessPath, $processName, $firm): string
  47. {
  48. $processParam = $firm ? "plugin.$firm.$processName" : $processName;
  49. $configParam = $firm ? "config('plugin.$firm.process')['$processName']" : "config('process')['$processName']";
  50. $fileContent = <<<EOF
  51. <?php
  52. require_once __DIR__ . '/../../vendor/autoload.php';
  53. use Workerman\Worker;
  54. use Workerman\Connection\TcpConnection;
  55. use Webman\Config;
  56. use support\App;
  57. ini_set('display_errors', 'on');
  58. error_reporting(E_ALL);
  59. if (is_callable('opcache_reset')) {
  60. opcache_reset();
  61. }
  62. App::loadAllConfig(['route']);
  63. worker_start('$processParam', $configParam);
  64. if (DIRECTORY_SEPARATOR != "/") {
  65. Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
  66. TcpConnection::\$defaultMaxPackageSize = config('server')['max_package_size'] ?? 10*1024*1024;
  67. }
  68. Worker::runAll();
  69. EOF;
  70. $processFile = $runtimeProcessPath . DIRECTORY_SEPARATOR . "start_$processParam.php";
  71. file_put_contents($processFile, $fileContent);
  72. return $processFile;
  73. }
  74. if ($monitorConfig = config('process.monitor.constructor')) {
  75. $monitorHandler = config('process.monitor.handler');
  76. $monitor = new $monitorHandler(...array_values($monitorConfig));
  77. }
  78. function popen_processes($processFiles)
  79. {
  80. $cmd = '"' . PHP_BINARY . '" ' . implode(' ', $processFiles);
  81. $descriptorspec = [STDIN, STDOUT, STDOUT];
  82. $resource = proc_open($cmd, $descriptorspec, $pipes, null, null, ['bypass_shell' => true]);
  83. if (!$resource) {
  84. exit("Can not execute $cmd\r\n");
  85. }
  86. return $resource;
  87. }
  88. $resource = popen_processes($processFiles);
  89. echo "\r\n";
  90. while (1) {
  91. sleep(1);
  92. if (!empty($monitor) && $monitor->checkAllFilesChange()) {
  93. $status = proc_get_status($resource);
  94. $pid = $status['pid'];
  95. shell_exec("taskkill /F /T /PID $pid");
  96. proc_close($resource);
  97. $resource = popen_processes($processFiles);
  98. }
  99. }