webman
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Start file for windows
  4. */
  5. require_once __DIR__ . '/vendor/autoload.php';
  6. use process\Monitor;
  7. use support\App;
  8. use Dotenv\Dotenv;
  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. $error_reporting = config('app.error_reporting');
  21. if (isset($error_reporting)) {
  22. error_reporting($error_reporting);
  23. }
  24. $runtime_process_path = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
  25. if (!is_dir($runtime_process_path)) {
  26. mkdir($runtime_process_path);
  27. }
  28. $process_files = [
  29. __DIR__ . DIRECTORY_SEPARATOR . 'start.php'
  30. ];
  31. foreach (config('process', []) as $process_name => $config) {
  32. $process_files[] = write_process_file($runtime_process_path, $process_name, '');
  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 $process_name => $config) {
  40. $process_files[] = write_process_file($runtime_process_path, $process_name, "$firm.$name");
  41. }
  42. }
  43. foreach ($projects['process'] ?? [] as $process_name => $config) {
  44. $process_files[] = write_process_file($runtime_process_path, $process_name, $firm);
  45. }
  46. }
  47. function write_process_file($runtime_process_path, $process_name, $firm)
  48. {
  49. $process_param = $firm ? "plugin.$firm.$process_name" : $process_name;
  50. $config_param = $firm ? "config('plugin.$firm.process')['$process_name']" : "config('process')['$process_name']";
  51. $file_content = <<<EOF
  52. <?php
  53. require_once __DIR__ . '/../../vendor/autoload.php';
  54. use Workerman\Worker;
  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('$process_param', $config_param);
  64. if (DIRECTORY_SEPARATOR != "/") {
  65. Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
  66. }
  67. Worker::runAll();
  68. EOF;
  69. $process_file = $runtime_process_path . DIRECTORY_SEPARATOR . "start_$process_param.php";
  70. file_put_contents($process_file, $file_content);
  71. return $process_file;
  72. }
  73. if ($monitor_config = config('process.monitor.constructor')) {
  74. $monitor = new Monitor(...array_values($monitor_config));
  75. }
  76. function popen_processes($process_files)
  77. {
  78. $cmd = "php " . implode(' ', $process_files);
  79. $descriptorspec = [STDIN, STDOUT, STDOUT];
  80. $resource = proc_open($cmd, $descriptorspec, $pipes);
  81. if (!$resource) {
  82. exit("Can not execute $cmd\r\n");
  83. }
  84. return $resource;
  85. }
  86. $resource = popen_processes($process_files);
  87. echo "\r\n";
  88. while (1) {
  89. sleep(1);
  90. if (!empty($monitor) && $monitor->checkAllFilesChange()) {
  91. $status = proc_get_status($resource);
  92. $pid = $status['pid'];
  93. shell_exec("taskkill /F /T /PID $pid");
  94. proc_close($resource);
  95. $resource = popen_processes($process_files);
  96. }
  97. }