DbForUpdate.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_Db
  24. */
  25. require_once 'Zend/Queue/Adapter/Db.php';
  26. /**
  27. * Class for using connecting to a Zend_Db-based queuing system
  28. *
  29. * $config['options'][Zend_Db_Select::FOR_UPDATE] is a new feature that was
  30. * written after this code was written. However, this will still serve as a
  31. * good example adapter
  32. *
  33. * @category Zend
  34. * @package Zend_Queue
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Custom_DbForUpdate extends Zend_Queue_Adapter_Db
  40. {
  41. /**
  42. * Return the first element in the queue
  43. *
  44. * @param integer $maxMessages
  45. * @param integer $timeout
  46. * @param Zend_Queue $queue
  47. * @return Zend_Queue_Message_Iterator
  48. */
  49. public function receive($maxMessages=null, $timeout=null, Zend_Queue $queue=null)
  50. {
  51. if ($maxMessages === null) {
  52. $maxMessages = 1;
  53. }
  54. if ($timeout === null) {
  55. $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
  56. }
  57. if ($queue === null) {
  58. $queue = $this->_queue;
  59. }
  60. $msgs = array();
  61. $info = $this->_msg_table->info();
  62. $microtime = microtime(true); // cache microtime
  63. $db = $this->_msg_table->getAdapter();
  64. try {
  65. // transaction must start before the select query.
  66. $db->beginTransaction();
  67. // changes: added forUpdate
  68. $query = $db->select()->forUpdate();
  69. $query->from($info['name'], array('*'));
  70. $query->where('queue_id=?', $this->getQueueId($queue->getName()));
  71. $query->where('handle IS NULL OR timeout+' . (int)$timeout . ' < ' . (int)$microtime);
  72. $query->limit($maxMessages);
  73. foreach ($db->fetchAll($query) as $data) {
  74. // setup our changes to the message
  75. $data['handle'] = md5(uniqid(rand(), true));
  76. $update = array(
  77. 'handle' => $data['handle'],
  78. 'timeout' => $microtime
  79. );
  80. // update the database
  81. $where = array();
  82. $where[] = $db->quoteInto('message_id=?', $data['message_id']);
  83. $count = $db->update($info['name'], $update, $where);
  84. // we check count to make sure no other thread has gotten
  85. // the rows after our select, but before our update.
  86. if ($count > 0) {
  87. $msgs[] = $data;
  88. }
  89. }
  90. $db->commit();
  91. } catch (Exception $e) {
  92. $db->rollBack();
  93. /**
  94. * @see Zend_Queue_Exception
  95. */
  96. require_once 'Zend/Queue/Exception.php';
  97. throw new Zend_Queue_Exception($e->getMessage(), $e->getCode());
  98. }
  99. $config = array(
  100. 'queue' => $queue,
  101. 'data' => $msgs,
  102. 'messageClass' => $queue->getMessageClass()
  103. );
  104. $classname = $queue->getMessageSetClass();
  105. Zend_Loader::loadClass($classname);
  106. return new $classname($config);
  107. }
  108. }