PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Queue/Listener.php

https://gitlab.com/judielsm/Handora
PHP | 234 lines | 84 code | 26 blank | 124 comment | 4 complexity | 02cba14e7e9ff1a2119cc360cbc377e6 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Closure;
  4. use Symfony\Component\Process\Process;
  5. class Listener
  6. {
  7. /**
  8. * The command working path.
  9. *
  10. * @var string
  11. */
  12. protected $commandPath;
  13. /**
  14. * The environment the workers should run under.
  15. *
  16. * @var string
  17. */
  18. protected $environment;
  19. /**
  20. * The amount of seconds to wait before polling the queue.
  21. *
  22. * @var int
  23. */
  24. protected $sleep = 3;
  25. /**
  26. * The amount of times to try a job before logging it failed.
  27. *
  28. * @var int
  29. */
  30. protected $maxTries = 0;
  31. /**
  32. * The queue worker command line.
  33. *
  34. * @var string
  35. */
  36. protected $workerCommand;
  37. /**
  38. * The output handler callback.
  39. *
  40. * @var \Closure|null
  41. */
  42. protected $outputHandler;
  43. /**
  44. * Create a new queue listener.
  45. *
  46. * @param string $commandPath
  47. * @return void
  48. */
  49. public function __construct($commandPath)
  50. {
  51. $this->commandPath = $commandPath;
  52. $this->workerCommand = '"'.PHP_BINARY.'" artisan queue:work %s --queue="%s" --delay=%s --memory=%s --sleep=%s --tries=%s';
  53. }
  54. /**
  55. * Listen to the given queue connection.
  56. *
  57. * @param string $connection
  58. * @param string $queue
  59. * @param string $delay
  60. * @param string $memory
  61. * @param int $timeout
  62. * @return void
  63. */
  64. public function listen($connection, $queue, $delay, $memory, $timeout = 60)
  65. {
  66. $process = $this->makeProcess($connection, $queue, $delay, $memory, $timeout);
  67. while (true) {
  68. $this->runProcess($process, $memory);
  69. }
  70. }
  71. /**
  72. * Run the given process.
  73. *
  74. * @param \Symfony\Component\Process\Process $process
  75. * @param int $memory
  76. * @return void
  77. */
  78. public function runProcess(Process $process, $memory)
  79. {
  80. $process->run(function ($type, $line) {
  81. $this->handleWorkerOutput($type, $line);
  82. });
  83. // Once we have run the job we'll go check if the memory limit has been
  84. // exceeded for the script. If it has, we will kill this script so a
  85. // process manager will restart this with a clean slate of memory.
  86. if ($this->memoryExceeded($memory)) {
  87. $this->stop();
  88. }
  89. }
  90. /**
  91. * Create a new Symfony process for the worker.
  92. *
  93. * @param string $connection
  94. * @param string $queue
  95. * @param int $delay
  96. * @param int $memory
  97. * @param int $timeout
  98. * @return \Symfony\Component\Process\Process
  99. */
  100. public function makeProcess($connection, $queue, $delay, $memory, $timeout)
  101. {
  102. $string = $this->workerCommand;
  103. // If the environment is set, we will append it to the command string so the
  104. // workers will run under the specified environment. Otherwise, they will
  105. // just run under the production environment which is not always right.
  106. if (isset($this->environment)) {
  107. $string .= ' --env='.$this->environment;
  108. }
  109. // Next, we will just format out the worker commands with all of the various
  110. // options available for the command. This will produce the final command
  111. // line that we will pass into a Symfony process object for processing.
  112. $command = sprintf(
  113. $string, $connection, $queue, $delay,
  114. $memory, $this->sleep, $this->maxTries
  115. );
  116. return new Process($command, $this->commandPath, null, null, $timeout);
  117. }
  118. /**
  119. * Handle output from the worker process.
  120. *
  121. * @param int $type
  122. * @param string $line
  123. * @return void
  124. */
  125. protected function handleWorkerOutput($type, $line)
  126. {
  127. if (isset($this->outputHandler)) {
  128. call_user_func($this->outputHandler, $type, $line);
  129. }
  130. }
  131. /**
  132. * Determine if the memory limit has been exceeded.
  133. *
  134. * @param int $memoryLimit
  135. * @return bool
  136. */
  137. public function memoryExceeded($memoryLimit)
  138. {
  139. return (memory_get_usage() / 1024 / 1024) >= $memoryLimit;
  140. }
  141. /**
  142. * Stop listening and bail out of the script.
  143. *
  144. * @return void
  145. */
  146. public function stop()
  147. {
  148. die;
  149. }
  150. /**
  151. * Set the output handler callback.
  152. *
  153. * @param \Closure $outputHandler
  154. * @return void
  155. */
  156. public function setOutputHandler(Closure $outputHandler)
  157. {
  158. $this->outputHandler = $outputHandler;
  159. }
  160. /**
  161. * Get the current listener environment.
  162. *
  163. * @return string
  164. */
  165. public function getEnvironment()
  166. {
  167. return $this->environment;
  168. }
  169. /**
  170. * Set the current environment.
  171. *
  172. * @param string $environment
  173. * @return void
  174. */
  175. public function setEnvironment($environment)
  176. {
  177. $this->environment = $environment;
  178. }
  179. /**
  180. * Get the amount of seconds to wait before polling the queue.
  181. *
  182. * @return int
  183. */
  184. public function getSleep()
  185. {
  186. return $this->sleep;
  187. }
  188. /**
  189. * Set the amount of seconds to wait before polling the queue.
  190. *
  191. * @param int $sleep
  192. * @return void
  193. */
  194. public function setSleep($sleep)
  195. {
  196. $this->sleep = $sleep;
  197. }
  198. /**
  199. * Set the amount of times to try a job before logging it failed.
  200. *
  201. * @param int $tries
  202. * @return void
  203. */
  204. public function setMaxTries($tries)
  205. {
  206. $this->maxTries = $tries;
  207. }
  208. }