Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

61 wiersze
1.8 KiB

  1. <?php
  2. /**
  3. * 远程调度器 Task_Runner_Remote
  4. *
  5. * - 通过远程请求接口实现任务调度
  6. *
  7. * @author dogstar <chanzonghuang@gmail.com> 20150516
  8. */
  9. class Task_Runner_Remote extends Task_Runner {
  10. /**
  11. * @var Task_Runner_Remote_Connector 远程接口连接器实例
  12. */
  13. protected $contector;
  14. /**
  15. * @var int $timeoutMS 接口超时(单位:毫秒)
  16. */
  17. protected $timeoutMS;
  18. /**
  19. * @var int 默认最大接口超时
  20. */
  21. const MAX_TIMEOUT_MS = 3000;
  22. public function __construct(Task_MQ $mq, $step = 10, Task_Runner_Remote_Connector $contector = NULL) {
  23. $config = DI()->config->get('app.Task.runner.remote');
  24. if ($contector === NULL) {
  25. if (empty($config['host'])) {
  26. throw new PhalApi_Exception_InternalServerError(T('task miss api host for'));
  27. }
  28. $contector = new Task_Runner_Remote_Connector_Http($config);
  29. }
  30. $this->contector = $contector;
  31. $this->timeoutMS = isset($config['timeoutMS']) ? intval($config['timeoutMS']) : self::MAX_TIMEOUT_MS;
  32. parent::__construct($mq, $step);
  33. }
  34. protected function youGo($service, $params) {
  35. $rs = $this->contector->request($service, $params, $this->timeoutMS);
  36. if ($this->contector->getRet() == 404) {
  37. throw PhalApi_Exception_InternalServerError('task request api time out',
  38. array('url' => $this->contector->getUrl()));
  39. }
  40. $isOk = $this->contector->getRet() == 200 ? TRUE : FALSE;
  41. if (!$isOk) {
  42. DI()->logger->debug('task remote request not ok',
  43. array('url' => $this->contector->getUrl(), 'ret' => $this->contector->getRet(), 'msg' => $this->contector->getMsg()));
  44. }
  45. return $isOk;
  46. }
  47. }