SupervisordController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/9
  6. * Time: 下午5:26
  7. */
  8. namespace app\backend\modules\supervisord\controllers;
  9. use app\backend\modules\supervisord\services\Supervisor;
  10. use app\common\components\BaseController;
  11. use app\common\facades\SiteSetting;
  12. use app\common\helpers\Cache;
  13. use app\common\helpers\Url;
  14. use app\common\facades\Setting;
  15. use app\host\HostManager;
  16. use Illuminate\Support\Facades\Redis;
  17. use Predis\Connection\ConnectionException;
  18. class SupervisordController extends BaseController
  19. {
  20. private $supervisor = null;
  21. public function preAction()
  22. {
  23. parent::preAction();
  24. $this->supervisor = app('supervisor');
  25. $this->supervisor->setTimeout(5000); // microseconds
  26. }
  27. /**
  28. * 商城设置
  29. * @return mixed
  30. */
  31. public function index()
  32. {
  33. //print_r($supervisor->getState());
  34. //$allProcessInfo = $this->supervisor->getAllProcessInfo();
  35. //$allProcessInfo = $supervisor->stopProcess("dev1-worker:dev1-worker_01");
  36. //$allProcessInfo = $supervisor->readLog(0);
  37. //$allProcessInfo = $supervisor->logMessage();
  38. //dd($allProcessInfo);
  39. $supervisord = SiteSetting::get('supervisor');
  40. return view('supervisor.index', [
  41. 'service_type' => $supervisord['service_type'] ?: 0
  42. ])->render();
  43. }
  44. public function store()
  45. {
  46. $setting = request()->input('setting');
  47. if ($setting) {
  48. $setting['address']['ip'] = $setting['address']['ip'] ? trim($setting['address']['ip']) : 'http://127.0.0.1';
  49. SiteSetting::set('supervisor', $setting);
  50. return $this->successJson("设置保存成功", Url::absoluteWeb('supervisord.supervisord.store'));
  51. }
  52. $supervisord = SiteSetting::get('supervisor');
  53. $data['address']['ip'] = 'http://127.0.0.1';
  54. $supervisord['address']['ip'] ?: SiteSetting::set('supervisor', $data);
  55. return view('supervisor.store', [
  56. 'setting' => json_encode($supervisord)
  57. ])->render();
  58. }
  59. public function process()
  60. {
  61. //print_r($supervisor->getState());
  62. $allProcessInfo = $this->supervisor->getAllProcessInfo();
  63. $state = $this->supervisor->getState();
  64. // dd($state);
  65. foreach ($allProcessInfo as $host => &$value) {
  66. foreach ($value->val as $key => &$val) {
  67. $val['cstate'] = false;
  68. // echo $val;
  69. }
  70. }
  71. $current_time = time();
  72. $queue_hearteat = [
  73. 'daemon' => $this->daemonStatus(),
  74. 'cron' => \app\backend\modules\survey\models\CronHeartbeat::getLog($current_time),
  75. 'job' => \app\backend\modules\survey\models\JobHeartbeat::getLog($current_time),
  76. 'redis' => $this->getRedisStatus()
  77. ];
  78. return json_encode([
  79. 'process' => $allProcessInfo,
  80. 'state' => $state,
  81. 'queue_hearteat' => $queue_hearteat,
  82. 'queue_hearteat_icon' => 'icon-fontclass-deng',
  83. ]);
  84. }
  85. public function showlog()
  86. {
  87. $process = \YunShop::request()->process;
  88. $hostname = \YunShop::request()->hostname;
  89. $this->supervisor->setCurrentHostname($hostname);
  90. $result = $this->supervisor->tailProcessStdoutLog($process, 1, 100000);
  91. $this->supervisor->setCurrentHostname();
  92. //取当前hostname的数据
  93. $result = $result[$hostname];
  94. return json_encode($result);
  95. }
  96. public function clearlog()
  97. {
  98. $process = \YunShop::request()->process;
  99. $hostname = \YunShop::request()->hostname;
  100. $this->supervisor->setCurrentHostname($hostname);
  101. $result = $this->supervisor->clearProcessLogs($process);
  102. $this->supervisor->setCurrentHostname();
  103. $result = $result[$hostname];
  104. return json_encode($result);
  105. }
  106. public function stop()
  107. {
  108. $process = \YunShop::request()->process;
  109. $hostname = \YunShop::request()->hostname;
  110. $this->supervisor->setCurrentHostname($hostname);
  111. $result = $this->supervisor->stopProcess($process);
  112. $this->supervisor->setCurrentHostname();
  113. $result = $result[$hostname];
  114. return json_encode($result);
  115. }
  116. public function stopAll()
  117. {
  118. $result = $this->supervisor->stopAllProcesses();
  119. return json_encode($result);
  120. }
  121. public function start()
  122. {
  123. $process = \YunShop::request()->process;
  124. $hostname = \YunShop::request()->hostname;
  125. $this->supervisor->setCurrentHostname($hostname);
  126. $result = $this->supervisor->startProcess($process);
  127. $this->supervisor->setCurrentHostname();
  128. $result = $result[$hostname];
  129. return json_encode($result);
  130. }
  131. public function startAll()
  132. {
  133. (new HostManager())->restart();
  134. $result = $this->supervisor->startAllProcesses();
  135. return json_encode($result);
  136. }
  137. public function restart()
  138. {
  139. (new HostManager())->restart();
  140. $result = $this->supervisor->restart();
  141. return json_encode($result);
  142. }
  143. private function daemonStatus()
  144. {
  145. $all_status = app('supervisor')->getState();
  146. $queue_status = 'green';
  147. $msg = '正常';
  148. $title = '';
  149. if (!function_exists('stream_socket_server')) {
  150. return array('queue_status' => 'yellow', 'msg' => '请解禁stream_socket_server函数');
  151. }
  152. foreach ($all_status as $hostname => $status) {
  153. $code = '正常';
  154. if ($status->val['statecode'] != 1) {
  155. $queue_status = 'not_running';
  156. $msg = $code = '异常';
  157. }
  158. $title .= '服务器' . $hostname . ":$code\r\n";
  159. }
  160. if (count($all_status) == 1) {
  161. $title = $msg;
  162. }
  163. return array('queue_status' => $queue_status, 'msg' => $msg, 'title' => $title);
  164. }
  165. /**
  166. * @return array
  167. * uninstall redis未安装
  168. * unexecute redis未执行
  169. */
  170. private function getRedisStatus()
  171. {
  172. try {
  173. if (!class_exists('Predis\Client')) {
  174. return array('queue_status' => 'uninstall', 'msg' => '未安装');
  175. }
  176. $res = \Illuminate\Support\Facades\Redis::ping() == 'PONG';
  177. if ($res) {
  178. return array('queue_status' => 'green', 'msg' => '正常');
  179. }
  180. } catch (ConnectionException $exception) {
  181. return array('queue_status' => 'unconnection', 'msg' => '连接失败');
  182. } catch (\Exception $exception) {
  183. return array('queue_status' => 'unexecute', 'msg' => '无法使用');
  184. }
  185. }
  186. }