Timeout.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\process\exception;
  12. use think\Process;
  13. class Timeout extends \RuntimeException
  14. {
  15. const TYPE_GENERAL = 1;
  16. const TYPE_IDLE = 2;
  17. private $process;
  18. private $timeoutType;
  19. public function __construct(Process $process, $timeoutType)
  20. {
  21. $this->process = $process;
  22. $this->timeoutType = $timeoutType;
  23. parent::__construct(sprintf('The process "%s" exceeded the timeout of %s seconds.', $process->getCommandLine(), $this->getExceededTimeout()));
  24. }
  25. public function getProcess()
  26. {
  27. return $this->process;
  28. }
  29. public function isGeneralTimeout()
  30. {
  31. return $this->timeoutType === self::TYPE_GENERAL;
  32. }
  33. public function isIdleTimeout()
  34. {
  35. return $this->timeoutType === self::TYPE_IDLE;
  36. }
  37. public function getExceededTimeout()
  38. {
  39. switch ($this->timeoutType) {
  40. case self::TYPE_GENERAL:
  41. return $this->process->getTimeout();
  42. case self::TYPE_IDLE:
  43. return $this->process->getIdleTimeout();
  44. default:
  45. throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType));
  46. }
  47. }
  48. }