Pheanstalk.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace Pheanstalk;
  3. use Pheanstalk\Contract\CommandInterface;
  4. use Pheanstalk\Contract\JobIdInterface;
  5. use Pheanstalk\Contract\PheanstalkInterface;
  6. use Pheanstalk\Contract\ResponseInterface;
  7. use Pheanstalk\Contract\SocketFactoryInterface;
  8. use Pheanstalk\Exception\DeadlineSoonException;
  9. /**
  10. * Pheanstalk is a PHP client for the beanstalkd workqueue.
  11. */
  12. class Pheanstalk implements PheanstalkInterface
  13. {
  14. /**
  15. * @var Connection
  16. */
  17. private $connection;
  18. /**
  19. * @var ?string
  20. */
  21. private $using = PheanstalkInterface::DEFAULT_TUBE;
  22. /**
  23. * @var array<string,bool>
  24. */
  25. private $watching = [PheanstalkInterface::DEFAULT_TUBE => true];
  26. public function __construct(Connection $connection)
  27. {
  28. $this->connection = $connection;
  29. }
  30. /**
  31. * Static constructor that uses autodetection to choose an underlying socket implementation
  32. * @param string $host
  33. * @param int $port
  34. * @param int $connectTimeout
  35. * @return Pheanstalk
  36. */
  37. public static function create(string $host, int $port = 11300, int $connectTimeout = 10)
  38. {
  39. return self::createWithFactory(new SocketFactory($host, $port, $connectTimeout));
  40. }
  41. /**
  42. * Static constructor that uses a given socket factory for underlying connections
  43. * @param SocketFactoryInterface $factory
  44. * @return Pheanstalk
  45. */
  46. public static function createWithFactory(SocketFactoryInterface $factory)
  47. {
  48. return new self(new Connection($factory));
  49. }
  50. // ----------------------------------------
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function bury(JobIdInterface $job, int $priority = PheanstalkInterface::DEFAULT_PRIORITY): void
  55. {
  56. $this->dispatch(new Command\BuryCommand($job, $priority));
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function delete(JobIdInterface $job): void
  62. {
  63. $this->dispatch(new Command\DeleteCommand($job));
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function ignore(string $tube): PheanstalkInterface
  69. {
  70. if (isset($this->watching[$tube])) {
  71. $this->dispatch(new Command\IgnoreCommand($tube));
  72. unset($this->watching[$tube]);
  73. }
  74. return $this;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function kick(int $max): int
  80. {
  81. $response = $this->dispatch(new Command\KickCommand($max));
  82. return $response['kicked'];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function kickJob(JobIdInterface $job): void
  88. {
  89. $this->dispatch(new Command\KickJobCommand($job));
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function listTubes(): array
  95. {
  96. return (array)$this->dispatch(
  97. new Command\ListTubesCommand()
  98. );
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function listTubesWatched(bool $askServer = false): array
  104. {
  105. if ($askServer) {
  106. $response = (array)$this->dispatch(
  107. new Command\ListTubesWatchedCommand()
  108. );
  109. $this->watching = array_fill_keys($response, true);
  110. }
  111. return array_keys($this->watching);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function listTubeUsed(bool $askServer = false): string
  117. {
  118. if ($askServer) {
  119. $response = $this->dispatch(
  120. new Command\ListTubeUsedCommand()
  121. );
  122. $this->using = $response['tube'];
  123. }
  124. return $this->using;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function pauseTube(string $tube, int $delay): void
  130. {
  131. $this->dispatch(new Command\PauseTubeCommand($tube, $delay));
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function resumeTube(string $tube): void
  137. {
  138. // Pause a tube with zero delay will resume the tube
  139. $this->pauseTube($tube, 0);
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function peek(JobIdInterface $job): Job
  145. {
  146. $response = $this->dispatch(
  147. new Command\PeekJobCommand($job)
  148. );
  149. return new Job($response['id'], $response['jobdata']);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function peekReady(): ?Job
  155. {
  156. $response = $this->dispatch(
  157. new Command\PeekCommand(Command\PeekCommand::TYPE_READY)
  158. );
  159. if ($response->getResponseName() === ResponseInterface::RESPONSE_NOT_FOUND) {
  160. return null;
  161. }
  162. return new Job($response['id'], $response['jobdata']);
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function peekDelayed(): ?Job
  168. {
  169. $response = $this->dispatch(
  170. new Command\PeekCommand(Command\PeekCommand::TYPE_DELAYED)
  171. );
  172. if ($response->getResponseName() === ResponseInterface::RESPONSE_NOT_FOUND) {
  173. return null;
  174. }
  175. return new Job($response['id'], $response['jobdata']);
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function peekBuried(): ?Job
  181. {
  182. $response = $this->dispatch(
  183. new Command\PeekCommand(Command\PeekCommand::TYPE_BURIED)
  184. );
  185. if ($response->getResponseName() === ResponseInterface::RESPONSE_NOT_FOUND) {
  186. return null;
  187. }
  188. return new Job($response['id'], $response['jobdata']);
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function put(
  194. string $data,
  195. int $priority = PheanstalkInterface::DEFAULT_PRIORITY,
  196. int $delay = PheanstalkInterface::DEFAULT_DELAY,
  197. int $ttr = PheanstalkInterface::DEFAULT_TTR
  198. ): Job {
  199. $response = $this->dispatch(
  200. new Command\PutCommand($data, $priority, $delay, $ttr)
  201. );
  202. return new Job($response['id'], $data);
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function release(
  208. JobIdInterface $job,
  209. int $priority = PheanstalkInterface::DEFAULT_PRIORITY,
  210. int $delay = PheanstalkInterface::DEFAULT_DELAY
  211. ): void {
  212. $this->dispatch(
  213. new Command\ReleaseCommand($job, $priority, $delay)
  214. );
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function reserve(): Job
  220. {
  221. $response = $this->dispatch(
  222. new Command\ReserveCommand()
  223. );
  224. return new Job($response['id'], $response['jobdata']);
  225. }
  226. /**
  227. * {@inheritdoc}
  228. */
  229. public function reserveWithTimeout(int $timeout): ?Job
  230. {
  231. $response = $this->dispatch(
  232. new Command\ReserveWithTimeoutCommand($timeout)
  233. );
  234. if ($response->getResponseName() === ResponseInterface::RESPONSE_DEADLINE_SOON) {
  235. throw new DeadlineSoonException();
  236. }
  237. if ($response->getResponseName() === ResponseInterface::RESPONSE_TIMED_OUT) {
  238. return null;
  239. }
  240. return new Job($response['id'], $response['jobdata']);
  241. }
  242. /**
  243. * {@inheritdoc}
  244. */
  245. public function statsJob(JobIdInterface $job): ResponseInterface
  246. {
  247. return $this->dispatch(new Command\StatsJobCommand($job));
  248. }
  249. /**
  250. * {@inheritdoc}
  251. */
  252. public function statsTube(string $tube): ResponseInterface
  253. {
  254. return $this->dispatch(new Command\StatsTubeCommand($tube));
  255. }
  256. /**
  257. * {@inheritdoc}
  258. */
  259. public function stats(): ResponseInterface
  260. {
  261. return $this->dispatch(new Command\StatsCommand());
  262. }
  263. /**
  264. * {@inheritdoc}
  265. */
  266. public function touch(JobIdInterface $job): void
  267. {
  268. $this->dispatch(new Command\TouchCommand($job));
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function useTube(string $tube): PheanstalkInterface
  274. {
  275. if ($this->using !== $tube) {
  276. $this->dispatch(new Command\UseCommand($tube));
  277. $this->using = $tube;
  278. }
  279. return $this;
  280. }
  281. /**
  282. * {@inheritdoc}
  283. */
  284. public function watch(string $tube): PheanstalkInterface
  285. {
  286. if (!isset($this->watching[$tube])) {
  287. $this->dispatch(new Command\WatchCommand($tube));
  288. $this->watching[$tube] = true;
  289. }
  290. return $this;
  291. }
  292. /**
  293. * {@inheritdoc}
  294. */
  295. public function watchOnly(string $tube): PheanstalkInterface
  296. {
  297. $this->watch($tube);
  298. $ignoreTubes = array_diff_key($this->watching, [$tube => true]);
  299. foreach ($ignoreTubes as $ignoreTube => $true) {
  300. $this->ignore($ignoreTube);
  301. }
  302. return $this;
  303. }
  304. // ----------------------------------------
  305. /**
  306. * Dispatches the specified command to the connection object.
  307. *
  308. * If a SocketException occurs, the connection is reset, and the command is
  309. * re-attempted once.
  310. *
  311. * @param CommandInterface $command
  312. *
  313. * @return ResponseInterface
  314. */
  315. private function dispatch($command)
  316. {
  317. try {
  318. $response = $this->connection->dispatchCommand($command);
  319. } catch (Exception\SocketException $e) {
  320. $this->reconnect();
  321. $response = $this->connection->dispatchCommand($command);
  322. }
  323. return $response;
  324. }
  325. /**
  326. * Creates a new connection object, based on the existing connection object,
  327. * and re-establishes the used tube and watchlist.
  328. */
  329. private function reconnect()
  330. {
  331. $this->connection->disconnect();
  332. if ($this->using !== PheanstalkInterface::DEFAULT_TUBE) {
  333. $this->dispatch(new Command\UseCommand($this->using));
  334. }
  335. foreach ($this->watching as $tube => $true) {
  336. if ($tube != PheanstalkInterface::DEFAULT_TUBE) {
  337. unset($this->watching[$tube]);
  338. $this->watch($tube);
  339. }
  340. }
  341. if (!isset($this->watching[PheanstalkInterface::DEFAULT_TUBE])) {
  342. $this->ignore(PheanstalkInterface::DEFAULT_TUBE);
  343. }
  344. }
  345. /**
  346. * @param string $tube The tube to use during execution
  347. * @param \Closure $closure Closure to execute while using the specified tube
  348. * @return mixed the return value of the closure.
  349. * @internal This is marked as internal since it is not part of a stabilized interface.
  350. */
  351. public function withUsedTube(string $tube, \Closure $closure)
  352. {
  353. $used = $this->listTubeUsed();
  354. try {
  355. $this->useTube($tube);
  356. return $closure($this);
  357. } finally {
  358. $this->useTube($used);
  359. }
  360. }
  361. /**
  362. * @param string $tube The tube to watch during execution
  363. * @param \Closure $closure Closure to execute while using the specified tube
  364. * @return mixed the return value of the closure.
  365. * @internal This is marked as internal since it is not part of a stabilized interface.
  366. */
  367. public function withWatchedTube(string $tube, \Closure $closure)
  368. {
  369. $watched = $this->listTubesWatched();
  370. try {
  371. $this->watchOnly($tube);
  372. return $closure($this);
  373. } finally {
  374. foreach ($watched as $tube) {
  375. $this->watch($tube);
  376. }
  377. if (!in_array($tube, $watched)) {
  378. $this->ignore($tube);
  379. }
  380. }
  381. }
  382. }