windows.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Start file for windows
  4. */
  5. require_once __DIR__ . '/vendor/autoload.php';
  6. use Dotenv\Dotenv;
  7. use process\Monitor;
  8. use support\App;
  9. use Workerman\Worker;
  10. ini_set('display_errors', 'on');
  11. error_reporting(E_ALL);
  12. if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
  13. if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
  14. Dotenv::createUnsafeImmutable(base_path())->load();
  15. } else {
  16. Dotenv::createMutable(base_path())->load();
  17. }
  18. }
  19. App::loadAllConfig(['route']);
  20. $errorReporting = config('app.error_reporting');
  21. if (isset($errorReporting)) {
  22. error_reporting($errorReporting);
  23. }
  24. $runtimeProcessPath = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
  25. if (!is_dir($runtimeProcessPath)) {
  26. mkdir($runtimeProcessPath);
  27. }
  28. $processFiles = [
  29. __DIR__ . DIRECTORY_SEPARATOR . 'start.php'
  30. ];
  31. foreach (config('process', []) as $processName => $config) {
  32. $processFiles[] = write_process_file($runtimeProcessPath, $processName, '');
  33. }
  34. foreach (config('plugin', []) as $firm => $projects) {
  35. foreach ($projects as $name => $project) {
  36. if (!is_array($project)) {
  37. continue;
  38. }
  39. foreach ($project['process'] ?? [] as $processName => $config) {
  40. $processFiles[] = write_process_file($runtimeProcessPath, $processName, "$firm.$name");
  41. }
  42. }
  43. foreach ($projects['process'] ?? [] as $processName => $config) {
  44. $processFiles[] = write_process_file($runtimeProcessPath, $processName, $firm);
  45. }
  46. }
  47. function write_process_file($runtimeProcessPath, $processName, $firm): string
  48. {
  49. $processParam = $firm ? "plugin.$firm.$processName" : $processName;
  50. $configParam = $firm ? "config('plugin.$firm.process')['$processName']" : "config('process')['$processName']";
  51. $fileContent = <<<EOF
  52. <?php
  53. require_once __DIR__ . '/../../vendor/autoload.php';
  54. use Workerman\Worker;
  55. use Workerman\Connection\TcpConnection;
  56. use Webman\Config;
  57. use support\App;
  58. ini_set('display_errors', 'on');
  59. error_reporting(E_ALL);
  60. if (is_callable('opcache_reset')) {
  61. opcache_reset();
  62. }
  63. App::loadAllConfig(['route']);
  64. worker_start('$processParam', $configParam);
  65. if (DIRECTORY_SEPARATOR != "/") {
  66. Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
  67. TcpConnection::\$defaultMaxPackageSize = config('server')['max_package_size'] ?? 10*1024*1024;
  68. }
  69. Worker::runAll();
  70. EOF;
  71. $processFile = $runtimeProcessPath . DIRECTORY_SEPARATOR . "start_$processParam.php";
  72. file_put_contents($processFile, $fileContent);
  73. return $processFile;
  74. }
  75. if ($monitorConfig = config('process.monitor.constructor')) {
  76. $monitor = new Monitor(...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. }