SwooleAsyncTimerController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * yii2 基于swoole的异步处理
  4. * $Id: SwooleAsyncController.php 9507 2016-09-29 06:48:44Z mevyen $
  5. * $Date: 2016-09-29 14:48:44 +0800 (Wed, 07 Sep 2016) $
  6. * $Author: mevyen $
  7. */
  8. namespace anlity\swooleAsyncTimer;
  9. use Yii;
  10. use yii\console\Controller;
  11. use anlity\swooleAsyncTimer\src\SwooleService;
  12. class SwooleAsyncTimerController extends Controller {
  13. public $versionNumber = '0.9.1';
  14. /**
  15. * 存储swooleAsync配置中的所有配置项
  16. * @var array
  17. */
  18. public $settings = [];
  19. /**
  20. * 默认controller
  21. * @var string
  22. */
  23. public $defaultAction = 'run';
  24. /**
  25. * 强制执行的选项
  26. * @var
  27. */
  28. public $force;
  29. public $version;
  30. /**
  31. * 接收force选项
  32. * @param string $actionID
  33. * @return array|string[]
  34. */
  35. public function options($actionID)
  36. {
  37. return ['force', 'version'];
  38. }
  39. /**
  40. * 设置别名
  41. * @return array
  42. */
  43. public function optionAliases()
  44. {
  45. return [
  46. 'f' => 'force',
  47. 'v' => 'version',
  48. ];
  49. }
  50. /**
  51. * 初始化
  52. * @throws Yii\base\ErrorException
  53. */
  54. public function init() {
  55. parent::init();
  56. $this->prepareSettings();
  57. }
  58. /**
  59. * 默认执行的action
  60. */
  61. public function actionDefault(){
  62. if($this->version){
  63. echo($this->versionNumber.PHP_EOL);
  64. }
  65. }
  66. /**
  67. * 初始化配置信息
  68. * @throws Yii\base\ErrorException
  69. */
  70. protected function prepareSettings()
  71. {
  72. $runtimePath = Yii::$app->getRuntimePath();
  73. $this->settings = [
  74. 'host' => '127.0.0.1',
  75. 'port' => '9512',
  76. 'process_name' => 'swooleServ',
  77. 'with_timer' => true,
  78. 'timer_interval' => 30000,
  79. 'open_tcp_nodelay' => '1',
  80. 'daemonize' => '1',
  81. 'worker_num' => '2',
  82. 'task_worker_num' => '2',
  83. 'task_max_request' => '10000',
  84. 'task_enable_coroutine' => true,
  85. 'debug' => true,
  86. 'pidfile' => $runtimePath.'/tmp/yii2-swoole-async-timer.pid',
  87. 'log_dir' => $runtimePath.'/yii2-swoole-async-timer/log',
  88. 'task_tmpdir' => $runtimePath.'/yii2-swoole-async-timer/task',
  89. 'log_file' => $runtimePath.'/yii2-swoole-async-timer/log/http.log',
  90. 'log_size' => 204800000,
  91. 'restartBeforeStartCallback' => null, //重启服务时的回调方法,重启后并开始服务前的回调
  92. 'restartAfterStartCallback' => null, //重启服务时的回调方法,重启后并开始服务后的回调
  93. ];
  94. try {
  95. $settings = Yii::$app->params['swooleAsyncTimer'];
  96. }catch (yii\base\ErrorException $e) {
  97. throw new yii\base\ErrorException('Empty param swooleAsyncTimer in params. ',8);
  98. }
  99. $this->settings = yii\helpers\ArrayHelper::merge(
  100. $this->settings,
  101. $settings
  102. );
  103. }
  104. /**
  105. * 启动服务
  106. * @param string $mode
  107. */
  108. public function actionRun($mode='start'){
  109. $swooleService = new SwooleService($this->settings,Yii::$app, $this);
  110. switch ($mode) {
  111. case 'start':
  112. $swooleService->serviceStart();
  113. break;
  114. case 'restart':
  115. $swooleService->serviceStop(!!$this->force, function() use ($swooleService){
  116. $restartBeforeStartCallback = $this->settings['restartBeforeStartCallback'];
  117. if(!is_null($restartBeforeStartCallback) && ($restartBeforeStartCallback instanceof \Closure || is_callable($restartBeforeStartCallback))){
  118. $restartBeforeStartCallback($swooleService, $this);
  119. }
  120. $swooleService->serviceStart();
  121. $restartAfterStartCallback = $this->settings['restartAfterStartCallback'];
  122. if(!is_null($restartAfterStartCallback) && ($restartAfterStartCallback instanceof \Closure || is_callable($restartAfterStartCallback))){
  123. $restartAfterStartCallback($swooleService, $this);
  124. }
  125. });
  126. break;
  127. case 'stop':
  128. $swooleService->serviceStop(!!$this->force);
  129. break;
  130. case 'stats':
  131. $swooleService->serviceStats();
  132. break;
  133. case 'list':
  134. $swooleService->serviceList();
  135. break;
  136. default:
  137. exit('error:参数错误');
  138. break;
  139. }
  140. }
  141. }