Beanstalk.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * beanstalk: A minimalistic PHP beanstalk client.
  4. *
  5. * Copyright (c) 2009-2011 David Persson
  6. *
  7. * Distributed under the terms of the MIT License.
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright 2009-2011 David Persson <nperson@gmx.de>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://github.com/davidpersson/beanstalk
  13. */
  14. /**
  15. * An interface to the beanstalk queue service. Implements the beanstalk
  16. * protocol spec 1.2. Where appropriate the documentation from the protcol has
  17. * been added to the docblocks in this class.
  18. *
  19. * @link https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
  20. */
  21. class Socket_Beanstalk {
  22. /**
  23. * Holds a boolean indicating whether a connection to the server is
  24. * currently established or not.
  25. *
  26. * @var boolean
  27. */
  28. public $connected = false;
  29. /**
  30. * Holds configuration values.
  31. *
  32. * @var array
  33. */
  34. protected $_config = array();
  35. /**
  36. * The current connection resource handle (if any).
  37. *
  38. * @var resource
  39. */
  40. protected $_connection;
  41. /**
  42. * Generated errors.
  43. *
  44. * @see Socket_Beanstalk::errors()
  45. * @var array
  46. */
  47. protected $_errors = array();
  48. /**
  49. * Constructor.
  50. *
  51. * @param array $config An array of configuration values:
  52. * - `'persistent'` Whether to make the connection persistent or
  53. * not, defaults to `true` as the FAQ recommends
  54. * persistent connections.
  55. * - `'host'` The beanstalk server hostname or IP address to
  56. * connect to, defaults to `127.0.0.1`.
  57. * - `'port'` The port of the server to connect to, defaults
  58. * to `11300`.
  59. * - `'timeout'` Timeout in seconds when establishing the
  60. * connection, defaults to `1`.
  61. * @return void
  62. */
  63. public function __construct(array $config = array()) {
  64. $defaults = array(
  65. 'persistent' => true,
  66. 'host' => '127.0.0.1',
  67. 'port' => 11300,
  68. 'timeout' => 1
  69. );
  70. $this->_config = $config + $defaults;
  71. }
  72. /**
  73. * Destructor, disconnects from the server.
  74. *
  75. * @return void
  76. */
  77. public function __destruct() {
  78. $this->disconnect();
  79. }
  80. /**
  81. * Initiates a socket connection to the beanstalk server. The resulting
  82. * stream will not have any timeout set on it. Which means it can wait an
  83. * unlimited amount of time until a packet becomes available. This is
  84. * required for doing blocking reads.
  85. *
  86. * @see Socket_Beanstalk::$_connection
  87. * @see Socket_Beanstalk::reserve()
  88. * @return boolean `true` if the connection was established, `false` otherwise.
  89. */
  90. public function connect() {
  91. if (isset($this->_connection)) {
  92. $this->disconnect();
  93. }
  94. $function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
  95. $params = array($this->_config['host'], $this->_config['port'], &$errNum, &$errStr);
  96. if ($this->_config['timeout']) {
  97. $params[] = $this->_config['timeout'];
  98. }
  99. $this->_connection = @call_user_func_array($function, $params);
  100. if (!empty($errNum) || !empty($errStr)) {
  101. $this->_errors[] = "{$errNum}: {$errStr}";
  102. }
  103. $this->connected = is_resource($this->_connection);
  104. if ($this->connected) {
  105. stream_set_timeout($this->_connection, -1);
  106. }
  107. return $this->connected;
  108. }
  109. /**
  110. * Closes the connection to the beanstalk server.
  111. *
  112. * @return boolean `true` if diconnecting was successful.
  113. */
  114. public function disconnect() {
  115. if (!is_resource($this->_connection)) {
  116. $this->connected = false;
  117. } else {
  118. $this->connected = !fclose($this->_connection);
  119. if (!$this->connected) {
  120. $this->_connection = null;
  121. }
  122. }
  123. return !$this->connected;
  124. }
  125. /**
  126. * Returns collected error messages.
  127. *
  128. * @return array An array of error messages.
  129. */
  130. public function errors() {
  131. return $this->_errors;
  132. }
  133. /**
  134. * Writes a packet to the socket. Prior to writing to the socket will check
  135. * for availability of the connection.
  136. *
  137. * @param string $data
  138. * @return integer|boolean number of written bytes or `false` on error.
  139. */
  140. protected function _write($data) {
  141. if (!$this->connected && !$this->connect()) {
  142. return false;
  143. }
  144. $data .= "\r\n";
  145. return fwrite($this->_connection, $data, strlen($data));
  146. }
  147. /**
  148. * Reads a packet from the socket. Prior to reading from the socket will
  149. * check for availability of the connection.
  150. *
  151. * @param int $length Number of bytes to read.
  152. * @return string|boolean Data or `false` on error.
  153. */
  154. protected function _read($length = null) {
  155. if (!$this->connected && !$this->connect()) {
  156. return false;
  157. }
  158. if ($length) {
  159. if (feof($this->_connection)) {
  160. return false;
  161. }
  162. $data = fread($this->_connection, $length + 2);
  163. $meta = stream_get_meta_data($this->_connection);
  164. if ($meta['timed_out']) {
  165. $this->_errors[] = 'Connection timed out.';
  166. return false;
  167. }
  168. $packet = rtrim($data, "\r\n");
  169. } else {
  170. $packet = stream_get_line($this->_connection, 16384, "\r\n");
  171. }
  172. return $packet;
  173. }
  174. /* Producer Commands */
  175. /**
  176. * The `put` command is for any process that wants to insert a job into the queue.
  177. *
  178. * @param integer $pri Jobs with smaller priority values will be scheduled
  179. * before jobs with larger priorities. The most urgent priority is
  180. * 0; the least urgent priority is 4294967295.
  181. * @param integer $delay Seconds to wait before putting the job in the
  182. * ready queue. The job will be in the "delayed" state during this time.
  183. * @param integer $ttr Time to run - Number of seconds to allow a worker to
  184. * run this job. The minimum ttr is 1.
  185. * @param string $data The job body.
  186. * @return integer|boolean `false` on error otherwise an integer indicating
  187. * the job id.
  188. */
  189. public function put($pri, $delay, $ttr, $data) {
  190. $this->_write(sprintf('put %d %d %d %d', $pri, $delay, $ttr, strlen($data)));
  191. $this->_write($data);
  192. $status = strtok($this->_read(), ' ');
  193. switch ($status) {
  194. case 'INSERTED':
  195. case 'BURIED':
  196. return (integer)strtok(' '); // job id
  197. case 'EXPECTED_CRLF':
  198. case 'JOB_TOO_BIG':
  199. default:
  200. $this->_errors[] = $status;
  201. return false;
  202. }
  203. }
  204. /**
  205. * The `use` command is for producers. Subsequent put commands will put jobs into
  206. * the tube specified by this command. If no use command has been issued, jobs
  207. * will be put into the tube named `default`.
  208. *
  209. * Please note that while obviously this method should better be named
  210. * `use` it is not. This is because `use` is a reserved keyword in PHP.
  211. *
  212. * @param string $tube A name at most 200 bytes. It specifies the tube to
  213. * use. If the tube does not exist, it will be created.
  214. * @return string|boolean `false` on error otherwise the name of the tube.
  215. */
  216. public function choose($tube) {
  217. $this->_write(sprintf('use %s', $tube));
  218. $status = strtok($this->_read(), ' ');
  219. switch ($status) {
  220. case 'USING':
  221. return strtok(' ');
  222. default:
  223. $this->_errors[] = $status;
  224. return false;
  225. }
  226. }
  227. /**
  228. * Alias for choose.
  229. *
  230. * @see Socket_Beanstalk::choose()
  231. * @param string $tube
  232. * @return string|boolean
  233. */
  234. public function useTube($tube) {
  235. return $this->choose($tube);
  236. }
  237. /* Worker Commands */
  238. /**
  239. * Reserve a job (with a timeout)
  240. *
  241. * @param integer $timeout If given specifies number of seconds to wait for
  242. * a job. 0 returns immediately.
  243. * @return array|false `false` on error otherwise an array holding job id
  244. * and body.
  245. */
  246. public function reserve($timeout = null) {
  247. if (isset($timeout)) {
  248. $this->_write(sprintf('reserve-with-timeout %d', $timeout));
  249. } else {
  250. $this->_write('reserve');
  251. }
  252. $status = strtok($this->_read(), ' ');
  253. switch ($status) {
  254. case 'RESERVED':
  255. return array(
  256. 'id' => (integer)strtok(' '),
  257. 'body' => $this->_read((integer)strtok(' '))
  258. );
  259. case 'DEADLINE_SOON':
  260. case 'TIMED_OUT':
  261. default:
  262. $this->_errors[] = $status;
  263. return false;
  264. }
  265. }
  266. /**
  267. * Removes a job from the server entirely.
  268. *
  269. * @param integer $id The id of the job.
  270. * @return boolean `false` on error, `true` on success.
  271. */
  272. public function delete($id) {
  273. $this->_write(sprintf('delete %d', $id));
  274. $status = $this->_read();
  275. switch ($status) {
  276. case 'DELETED':
  277. return true;
  278. case 'NOT_FOUND':
  279. default:
  280. $this->_errors[] = $status;
  281. return false;
  282. }
  283. }
  284. /**
  285. * Puts a reserved job back into the ready queue.
  286. *
  287. * @param integer $id The id of the job.
  288. * @param integer $pri Priority to assign to the job.
  289. * @param integer $delay Number of seconds to wait before putting the job in the ready queue.
  290. * @return boolean `false` on error, `true` on success.
  291. */
  292. public function release($id, $pri, $delay) {
  293. $this->_write(sprintf('release %d %d %d', $id, $pri, $delay));
  294. $status = $this->_read();
  295. switch ($status) {
  296. case 'RELEASED':
  297. case 'BURIED':
  298. return true;
  299. case 'NOT_FOUND':
  300. default:
  301. $this->_errors[] = $status;
  302. return false;
  303. }
  304. }
  305. /**
  306. * Puts a job into the `buried` state Buried jobs are put into a FIFO
  307. * linked list and will not be touched until a client kicks them.
  308. *
  309. * @param integer $id The id of the job.
  310. * @param integer $pri *New* priority to assign to the job.
  311. * @return boolean `false` on error, `true` on success.
  312. */
  313. public function bury($id, $pri) {
  314. $this->_write(sprintf('bury %d %d', $id, $pri));
  315. $status = $this->_read();
  316. switch ($status) {
  317. case 'BURIED':
  318. return true;
  319. case 'NOT_FOUND':
  320. default:
  321. $this->_errors[] = $status;
  322. return false;
  323. }
  324. }
  325. /**
  326. * Allows a worker to request more time to work on a job
  327. *
  328. * @param integer $id The id of the job.
  329. * @return boolean `false` on error, `true` on success.
  330. */
  331. public function touch($id) {
  332. $this->_write(sprintf('touch %d', $id));
  333. $status = $this->_read();
  334. switch ($status) {
  335. case 'TOUCHED':
  336. return true;
  337. case 'NOT_TOUCHED':
  338. default:
  339. $this->_errors[] = $status;
  340. return false;
  341. }
  342. }
  343. /**
  344. * Adds the named tube to the watch list for the current
  345. * connection.
  346. *
  347. * @param string $tube Name of tube to watch.
  348. * @return integer|boolean `false` on error otherwise number of tubes in watch list.
  349. */
  350. public function watch($tube) {
  351. $this->_write(sprintf('watch %s', $tube));
  352. $status = strtok($this->_read(), ' ');
  353. switch ($status) {
  354. case 'WATCHING':
  355. return (integer)strtok(' ');
  356. default:
  357. $this->_errors[] = $status;
  358. return false;
  359. }
  360. }
  361. /**
  362. * Remove the named tube from the watch list.
  363. *
  364. * @param string $tube Name of tube to ignore.
  365. * @return integer|boolean `false` on error otherwise number of tubes in watch list.
  366. */
  367. public function ignore($tube) {
  368. $this->_write(sprintf('ignore %s', $tube));
  369. $status = strtok($this->_read(), ' ');
  370. switch ($status) {
  371. case 'WATCHING':
  372. return (integer)strtok(' ');
  373. case 'NOT_IGNORED':
  374. default:
  375. $this->_errors[] = $status;
  376. return false;
  377. }
  378. }
  379. /* Other Commands */
  380. /**
  381. * Inspect a job by its id.
  382. *
  383. * @param integer $id The id of the job.
  384. * @return string|boolean `false` on error otherwise the body of the job.
  385. */
  386. public function peek($id) {
  387. $this->_write(sprintf('peek %d', $id));
  388. return $this->_peekRead();
  389. }
  390. /**
  391. * Inspect the next ready job.
  392. *
  393. * @return string|boolean `false` on error otherwise the body of the job.
  394. */
  395. public function peekReady() {
  396. $this->_write('peek-ready');
  397. return $this->_peekRead();
  398. }
  399. /**
  400. * Inspect the job with the shortest delay left.
  401. *
  402. * @return string|boolean `false` on error otherwise the body of the job.
  403. */
  404. public function peekDelayed() {
  405. $this->_write('peek-delayed');
  406. return $this->_peekRead();
  407. }
  408. /**
  409. * Inspect the next job in the list of buried jobs.
  410. *
  411. * @return string|boolean `false` on error otherwise the body of the job.
  412. */
  413. public function peekBuried() {
  414. $this->_write('peek-buried');
  415. return $this->_peekRead();
  416. }
  417. /**
  418. * Handles response for all peek methods.
  419. *
  420. * @return string|boolean `false` on error otherwise the body of the job.
  421. */
  422. protected function _peekRead() {
  423. $status = strtok($this->_read(), ' ');
  424. switch ($status) {
  425. case 'FOUND':
  426. return array(
  427. 'id' => (integer)strtok(' '),
  428. 'body' => $this->_read((integer)strtok(' '))
  429. );
  430. case 'NOT_FOUND':
  431. default:
  432. $this->_errors[] = $status;
  433. return false;
  434. }
  435. }
  436. /**
  437. * Moves jobs into the ready queue (applies to the current tube).
  438. *
  439. * If there are buried jobs those get kicked only otherwise
  440. * delayed jobs get kicked.
  441. *
  442. * @param integer $bound Upper bound on the number of jobs to kick.
  443. * @return integer|boolean False on error otherwise number of job kicked.
  444. */
  445. public function kick($bound) {
  446. $this->_write(sprintf('kick %d', $bound));
  447. $status = strtok($this->_read(), ' ');
  448. switch ($status) {
  449. case 'KICKED':
  450. return (integer)strtok(' ');
  451. default:
  452. $this->_errors[] = $status;
  453. return false;
  454. }
  455. }
  456. /* Stats Commands */
  457. /**
  458. * Gives statistical information about the specified job if it exists.
  459. *
  460. * @param integer $id The job id
  461. * @return string|boolean `false` on error otherwise a string with a yaml formatted dictionary
  462. */
  463. public function statsJob($id) {
  464. $this->_write(sprintf('stats-job %d', $id));
  465. return $this->_statsRead();
  466. }
  467. /**
  468. * Gives statistical information about the specified tube if it exists.
  469. *
  470. * @param string $tube Name of the tube.
  471. * @return string|boolean `false` on error otherwise a string with a yaml formatted dictionary.
  472. */
  473. public function statsTube($tube) {
  474. $this->_write(sprintf('stats-tube %s', $tube));
  475. return $this->_statsRead();
  476. }
  477. /**
  478. * Gives statistical information about the system as a whole.
  479. *
  480. * @return string|boolean `false` on error otherwise a string with a yaml formatted dictionary.
  481. */
  482. public function stats() {
  483. $this->_write('stats');
  484. return $this->_statsRead();
  485. }
  486. /**
  487. * Returns a list of all existing tubes.
  488. *
  489. * @return string|boolean `false` on error otherwise a string with a yaml formatted list.
  490. */
  491. public function listTubes() {
  492. $this->_write('list-tubes');
  493. return $this->_statsRead();
  494. }
  495. /**
  496. * Returns the tube currently being used by the producer.
  497. *
  498. * @return string|boolean `false` on error otherwise a string with the name of the tube.
  499. */
  500. public function listTubeUsed() {
  501. $this->_write('list-tube-used');
  502. return $this->_statsRead(false);
  503. }
  504. /**
  505. * Alias for listTubeUsed.
  506. *
  507. * @see Socket_Beanstalk::listTubeUsed()
  508. * @return string|boolean `false` on error otherwise a string with the name of the tube.
  509. */
  510. public function listTubeChosen() {
  511. return $this->listTubeUsed();
  512. }
  513. /**
  514. * Returns a list of tubes currently being watched by the worker.
  515. *
  516. * @return string|boolean `false` on error otherwise a string with a yaml formatted list.
  517. */
  518. public function listTubesWatched() {
  519. $this->_write('list-tubes-watched');
  520. return $this->_statsRead();
  521. }
  522. /**
  523. * Handles responses for all stat methods.
  524. *
  525. * @param boolean $decode Whether to decode data before returning it or not. Default is `true`.
  526. * @return array|string|boolean `false` on error otherwise statistical data.
  527. */
  528. protected function _statsRead($decode = true) {
  529. $status = strtok($this->_read(), ' ');
  530. switch ($status) {
  531. case 'OK':
  532. $data = $this->_read((integer)strtok(' '));
  533. return $decode ? $this->_decode($data) : $data;
  534. default:
  535. $this->_errors[] = $status;
  536. return false;
  537. }
  538. }
  539. /**
  540. * Decodes YAML data. This is a super naive decoder which just works on a
  541. * subset of YAML which is commonly returned by beanstalk.
  542. *
  543. * @param string $data The data in YAML format, can be either a list or a dictionary.
  544. * @return array An (associative) array of the converted data.
  545. */
  546. protected function _decode($data) {
  547. $data = array_slice(explode("\n", $data), 1);
  548. $result = array();
  549. foreach ($data as $key => $value) {
  550. if ($value[0] === '-') {
  551. $value = ltrim($value, '- ');
  552. } elseif (strpos($value, ':') !== false) {
  553. list($key, $value) = explode(':', $value);
  554. $value = ltrim($value, ' ');
  555. }
  556. if (is_numeric($value)) {
  557. $value = (integer) $value == $value ? (integer) $value : (float) $value;
  558. }
  559. $result[$key] = $value;
  560. }
  561. return $result;
  562. }
  563. }
  564. ?>