windows.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Start file for windows
  4. */
  5. require_once __DIR__ . '/vendor/autoload.php';
  6. use process\Monitor;
  7. use Workerman\Worker;
  8. use Webman\Config;
  9. ini_set('display_errors', 'on');
  10. error_reporting(E_ALL);
  11. Config::load(config_path(), ['route', 'container']);
  12. $runtime_process_path = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
  13. if (!is_dir($runtime_process_path)) {
  14. mkdir($runtime_process_path);
  15. }
  16. $process_files = [
  17. __DIR__ . DIRECTORY_SEPARATOR . 'start.php'
  18. ];
  19. foreach (config('process', []) as $process_name => $config) {
  20. $file_content = <<<EOF
  21. <?php
  22. require_once __DIR__ . '/../../vendor/autoload.php';
  23. use Workerman\Worker;
  24. use Webman\Config;
  25. ini_set('display_errors', 'on');
  26. error_reporting(E_ALL);
  27. if (is_callable('opcache_reset')) {
  28. opcache_reset();
  29. }
  30. Config::load(config_path(), ['route', 'container']);
  31. worker_start('$process_name', config('process')['$process_name']);
  32. if (DIRECTORY_SEPARATOR != "/") {
  33. Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
  34. }
  35. Worker::runAll();
  36. EOF;
  37. $process_file = $runtime_process_path . DIRECTORY_SEPARATOR . "start_$process_name.php";
  38. $process_files[] = $process_file;
  39. file_put_contents($process_file, $file_content);
  40. }
  41. foreach (config('plugin', []) as $firm => $projects) {
  42. foreach ($projects as $name => $project) {
  43. foreach ($project['process'] ?? [] as $process_name => $config) {
  44. $file_content = <<<EOF
  45. <?php
  46. require_once __DIR__ . '/../../vendor/autoload.php';
  47. use Workerman\Worker;
  48. use Webman\Config;
  49. ini_set('display_errors', 'on');
  50. error_reporting(E_ALL);
  51. if (is_callable('opcache_reset')) {
  52. opcache_reset();
  53. }
  54. Config::load(config_path(), ['route', 'container']);
  55. worker_start("plugin.$firm.$name.$process_name", config("plugin.$firm.$name.process")['$process_name']);
  56. if (DIRECTORY_SEPARATOR != "/") {
  57. Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
  58. }
  59. Worker::runAll();
  60. EOF;
  61. $process_file = $runtime_process_path . DIRECTORY_SEPARATOR . "start_$process_name.php";
  62. $process_files[] = $process_file;
  63. file_put_contents($process_file, $file_content);
  64. }
  65. }
  66. }
  67. $monitor = new Monitor(...array_values(config('process.monitor.constructor')));
  68. function popen_processes($process_files)
  69. {
  70. $cmd = "php " . implode(' ', $process_files);
  71. $descriptorspec = [STDIN, STDOUT, STDOUT];
  72. $resource = proc_open($cmd, $descriptorspec, $pipes);
  73. if (!$resource) {
  74. exit("Can not execute $cmd\r\n");
  75. }
  76. return $resource;
  77. }
  78. $resource = popen_processes($process_files);
  79. echo "\r\n";
  80. while (1) {
  81. sleep(1);
  82. if ($monitor->checkAllFilesChange()) {
  83. $status = proc_get_status($resource);
  84. $pid = $status['pid'];
  85. shell_exec("taskkill /F /T /PID $pid");
  86. proc_close($resource);
  87. $resource = popen_processes($process_files);
  88. }
  89. }