InteractsWithServer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace think\swoole\concerns;
  3. use Exception;
  4. use Swoole\Process;
  5. use Swoole\Runtime;
  6. use Swoole\Server;
  7. use Swoole\Server\Task;
  8. use think\App;
  9. use think\Event;
  10. use think\helper\Str;
  11. use think\swoole\FileWatcher;
  12. use think\swoole\Job;
  13. /**
  14. * Trait InteractsWithServer
  15. * @package think\swoole\concerns
  16. * @property App $container
  17. */
  18. trait InteractsWithServer
  19. {
  20. /**
  21. * 启动服务
  22. */
  23. public function run(): void
  24. {
  25. $this->getServer()->set([
  26. 'task_enable_coroutine' => true,
  27. 'send_yield' => true,
  28. 'reload_async' => true,
  29. 'enable_coroutine' => true,
  30. 'max_request' => 0,
  31. 'task_max_request' => 0,
  32. ]);
  33. $this->initialize();
  34. $this->triggerEvent('init');
  35. //热更新
  36. if ($this->getConfig('hot_update.enable', false)) {
  37. $this->addHotUpdateProcess();
  38. }
  39. $this->getServer()->start();
  40. }
  41. /**
  42. * 停止服务
  43. */
  44. public function stop(): void
  45. {
  46. $this->getServer()->shutdown();
  47. }
  48. /**
  49. * "onStart" listener.
  50. */
  51. public function onStart()
  52. {
  53. $this->setProcessName('master process');
  54. $this->triggerEvent('start', func_get_args());
  55. }
  56. /**
  57. * The listener of "managerStart" event.
  58. *
  59. * @return void
  60. */
  61. public function onManagerStart()
  62. {
  63. $this->setProcessName('manager process');
  64. $this->triggerEvent('managerStart', func_get_args());
  65. }
  66. /**
  67. * "onWorkerStart" listener.
  68. *
  69. * @param \Swoole\Http\Server|mixed $server
  70. *
  71. * @throws Exception
  72. */
  73. public function onWorkerStart($server)
  74. {
  75. $this->resumeCoordinator('workerStart', function () use ($server) {
  76. Runtime::enableCoroutine(
  77. $this->getConfig('coroutine.enable', true),
  78. $this->getConfig('coroutine.flags', SWOOLE_HOOK_ALL)
  79. );
  80. $this->clearCache();
  81. $this->setProcessName($server->taskworker ? 'task process' : 'worker process');
  82. $this->prepareApplication();
  83. $this->bindServer();
  84. $this->triggerEvent('workerStart', $this->app);
  85. });
  86. }
  87. /**
  88. * Set onTask listener.
  89. *
  90. * @param mixed $server
  91. * @param Task $task
  92. */
  93. public function onTask($server, Task $task)
  94. {
  95. $this->runInSandbox(function (Event $event, App $app) use ($task) {
  96. if ($task->data instanceof Job) {
  97. $task->data->run($app);
  98. } else {
  99. $event->trigger('swoole.task', $task);
  100. }
  101. }, $task->id);
  102. }
  103. /**
  104. * Set onShutdown listener.
  105. */
  106. public function onShutdown()
  107. {
  108. $this->triggerEvent('shutdown');
  109. }
  110. protected function bindServer()
  111. {
  112. $this->app->bind(Server::class, $this->getServer());
  113. $this->app->bind('swoole.server', Server::class);
  114. }
  115. /**
  116. * @return Server
  117. */
  118. public function getServer()
  119. {
  120. return $this->container->make(Server::class);
  121. }
  122. /**
  123. * Set swoole server listeners.
  124. */
  125. protected function setSwooleServerListeners()
  126. {
  127. foreach ($this->events as $event) {
  128. $listener = Str::camel("on_$event");
  129. $callback = method_exists($this, $listener) ? [$this, $listener] : function () use ($event) {
  130. $this->triggerEvent($event, func_get_args());
  131. };
  132. $this->getServer()->on($event, $callback);
  133. }
  134. }
  135. /**
  136. * 热更新
  137. */
  138. protected function addHotUpdateProcess()
  139. {
  140. $process = new Process(function () {
  141. $watcher = new FileWatcher(
  142. $this->getConfig('hot_update.include', []),
  143. $this->getConfig('hot_update.exclude', []),
  144. $this->getConfig('hot_update.name', [])
  145. );
  146. $watcher->watch(function () {
  147. $this->getServer()->reload();
  148. });
  149. }, false, 0, true);
  150. $this->addProcess($process);
  151. }
  152. /**
  153. * Add process to http server
  154. *
  155. * @param Process $process
  156. */
  157. public function addProcess(Process $process): void
  158. {
  159. $this->getServer()->addProcess($process);
  160. }
  161. /**
  162. * 清除apc、op缓存
  163. */
  164. protected function clearCache()
  165. {
  166. if (extension_loaded('apc')) {
  167. apc_clear_cache();
  168. }
  169. if (extension_loaded('Zend OPcache')) {
  170. opcache_reset();
  171. }
  172. }
  173. /**
  174. * Set process name.
  175. *
  176. * @param $process
  177. */
  178. protected function setProcessName($process)
  179. {
  180. $serverName = 'swoole server';
  181. $appName = $this->container->config->get('app.name', 'ThinkPHP');
  182. $name = sprintf('%s: %s for %s', $serverName, $process, $appName);
  183. @cli_set_process_title($name);
  184. }
  185. }