DatabaseQueue.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace Drupal\Core\Queue;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Database\DatabaseException;
  5. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  6. /**
  7. * Default queue implementation.
  8. *
  9. * @ingroup queue
  10. */
  11. class DatabaseQueue implements ReliableQueueInterface, QueueGarbageCollectionInterface {
  12. use DependencySerializationTrait;
  13. /**
  14. * The database table name.
  15. */
  16. const TABLE_NAME = 'queue';
  17. /**
  18. * The name of the queue this instance is working with.
  19. *
  20. * @var string
  21. */
  22. protected $name;
  23. /**
  24. * The database connection.
  25. *
  26. * @var \Drupal\Core\Database\Connection
  27. */
  28. protected $connection;
  29. /**
  30. * Constructs a \Drupal\Core\Queue\DatabaseQueue object.
  31. *
  32. * @param string $name
  33. * The name of the queue.
  34. * @param \Drupal\Core\Database\Connection $connection
  35. * The Connection object containing the key-value tables.
  36. */
  37. public function __construct($name, Connection $connection) {
  38. $this->name = $name;
  39. $this->connection = $connection;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function createItem($data) {
  45. $try_again = FALSE;
  46. try {
  47. $id = $this->doCreateItem($data);
  48. }
  49. catch (\Exception $e) {
  50. // If there was an exception, try to create the table.
  51. if (!$try_again = $this->ensureTableExists()) {
  52. // If the exception happened for other reason than the missing table,
  53. // propagate the exception.
  54. throw $e;
  55. }
  56. }
  57. // Now that the table has been created, try again if necessary.
  58. if ($try_again) {
  59. $id = $this->doCreateItem($data);
  60. }
  61. return $id;
  62. }
  63. /**
  64. * Adds a queue item and store it directly to the queue.
  65. *
  66. * @param $data
  67. * Arbitrary data to be associated with the new task in the queue.
  68. *
  69. * @return
  70. * A unique ID if the item was successfully created and was (best effort)
  71. * added to the queue, otherwise FALSE. We don't guarantee the item was
  72. * committed to disk etc, but as far as we know, the item is now in the
  73. * queue.
  74. */
  75. protected function doCreateItem($data) {
  76. $query = $this->connection->insert(static::TABLE_NAME)
  77. ->fields([
  78. 'name' => $this->name,
  79. 'data' => serialize($data),
  80. // We cannot rely on REQUEST_TIME because many items might be created
  81. // by a single request which takes longer than 1 second.
  82. 'created' => time(),
  83. ]);
  84. // Return the new serial ID, or FALSE on failure.
  85. return $query->execute();
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function numberOfItems() {
  91. try {
  92. return (int) $this->connection->query('SELECT COUNT(item_id) FROM {' . static::TABLE_NAME . '} WHERE name = :name', [':name' => $this->name])
  93. ->fetchField();
  94. }
  95. catch (\Exception $e) {
  96. $this->catchException($e);
  97. // If there is no table there cannot be any items.
  98. return 0;
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function claimItem($lease_time = 30) {
  105. // Claim an item by updating its expire fields. If claim is not successful
  106. // another thread may have claimed the item in the meantime. Therefore loop
  107. // until an item is successfully claimed or we are reasonably sure there
  108. // are no unclaimed items left.
  109. while (TRUE) {
  110. try {
  111. $item = $this->connection->queryRange('SELECT data, created, item_id FROM {' . static::TABLE_NAME . '} q WHERE expire = 0 AND name = :name ORDER BY created, item_id ASC', 0, 1, [':name' => $this->name])->fetchObject();
  112. }
  113. catch (\Exception $e) {
  114. $this->catchException($e);
  115. }
  116. // If the table does not exist there are no items currently available to
  117. // claim.
  118. if (empty($item)) {
  119. return FALSE;
  120. }
  121. // Try to update the item. Only one thread can succeed in UPDATEing the
  122. // same row. We cannot rely on REQUEST_TIME because items might be
  123. // claimed by a single consumer which runs longer than 1 second. If we
  124. // continue to use REQUEST_TIME instead of the current time(), we steal
  125. // time from the lease, and will tend to reset items before the lease
  126. // should really expire.
  127. $update = $this->connection->update(static::TABLE_NAME)
  128. ->fields([
  129. 'expire' => time() + $lease_time,
  130. ])
  131. ->condition('item_id', $item->item_id)
  132. ->condition('expire', 0);
  133. // If there are affected rows, this update succeeded.
  134. if ($update->execute()) {
  135. $item->data = unserialize($item->data);
  136. return $item;
  137. }
  138. }
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function releaseItem($item) {
  144. try {
  145. $update = $this->connection->update(static::TABLE_NAME)
  146. ->fields([
  147. 'expire' => 0,
  148. ])
  149. ->condition('item_id', $item->item_id);
  150. return $update->execute();
  151. }
  152. catch (\Exception $e) {
  153. $this->catchException($e);
  154. // If the table doesn't exist we should consider the item released.
  155. return TRUE;
  156. }
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function deleteItem($item) {
  162. try {
  163. $this->connection->delete(static::TABLE_NAME)
  164. ->condition('item_id', $item->item_id)
  165. ->execute();
  166. }
  167. catch (\Exception $e) {
  168. $this->catchException($e);
  169. }
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function createQueue() {
  175. // All tasks are stored in a single database table (which is created on
  176. // demand) so there is nothing we need to do to create a new queue.
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function deleteQueue() {
  182. try {
  183. $this->connection->delete(static::TABLE_NAME)
  184. ->condition('name', $this->name)
  185. ->execute();
  186. }
  187. catch (\Exception $e) {
  188. $this->catchException($e);
  189. }
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function garbageCollection() {
  195. try {
  196. // Clean up the queue for failed batches.
  197. $this->connection->delete(static::TABLE_NAME)
  198. ->condition('created', REQUEST_TIME - 864000, '<')
  199. ->condition('name', 'drupal_batch:%', 'LIKE')
  200. ->execute();
  201. // Reset expired items in the default queue implementation table. If that's
  202. // not used, this will simply be a no-op.
  203. $this->connection->update(static::TABLE_NAME)
  204. ->fields([
  205. 'expire' => 0,
  206. ])
  207. ->condition('expire', 0, '<>')
  208. ->condition('expire', REQUEST_TIME, '<')
  209. ->execute();
  210. }
  211. catch (\Exception $e) {
  212. $this->catchException($e);
  213. }
  214. }
  215. /**
  216. * Check if the table exists and create it if not.
  217. */
  218. protected function ensureTableExists() {
  219. try {
  220. $database_schema = $this->connection->schema();
  221. if (!$database_schema->tableExists(static::TABLE_NAME)) {
  222. $schema_definition = $this->schemaDefinition();
  223. $database_schema->createTable(static::TABLE_NAME, $schema_definition);
  224. return TRUE;
  225. }
  226. }
  227. // If another process has already created the queue table, attempting to
  228. // recreate it will throw an exception. In this case just catch the
  229. // exception and do nothing.
  230. catch (DatabaseException $e) {
  231. return TRUE;
  232. }
  233. return FALSE;
  234. }
  235. /**
  236. * Act on an exception when queue might be stale.
  237. *
  238. * If the table does not yet exist, that's fine, but if the table exists and
  239. * yet the query failed, then the queue is stale and the exception needs to
  240. * propagate.
  241. *
  242. * @param $e
  243. * The exception.
  244. *
  245. * @throws \Exception
  246. * If the table exists the exception passed in is rethrown.
  247. */
  248. protected function catchException(\Exception $e) {
  249. if ($this->connection->schema()->tableExists(static::TABLE_NAME)) {
  250. throw $e;
  251. }
  252. }
  253. /**
  254. * Defines the schema for the queue table.
  255. *
  256. * @internal
  257. */
  258. public function schemaDefinition() {
  259. return [
  260. 'description' => 'Stores items in queues.',
  261. 'fields' => [
  262. 'item_id' => [
  263. 'type' => 'serial',
  264. 'unsigned' => TRUE,
  265. 'not null' => TRUE,
  266. 'description' => 'Primary Key: Unique item ID.',
  267. ],
  268. 'name' => [
  269. 'type' => 'varchar_ascii',
  270. 'length' => 255,
  271. 'not null' => TRUE,
  272. 'default' => '',
  273. 'description' => 'The queue name.',
  274. ],
  275. 'data' => [
  276. 'type' => 'blob',
  277. 'not null' => FALSE,
  278. 'size' => 'big',
  279. 'serialize' => TRUE,
  280. 'description' => 'The arbitrary data for the item.',
  281. ],
  282. 'expire' => [
  283. 'type' => 'int',
  284. 'not null' => TRUE,
  285. 'default' => 0,
  286. 'description' => 'Timestamp when the claim lease expires on the item.',
  287. ],
  288. 'created' => [
  289. 'type' => 'int',
  290. 'not null' => TRUE,
  291. 'default' => 0,
  292. 'description' => 'Timestamp when the item was created.',
  293. ],
  294. ],
  295. 'primary key' => ['item_id'],
  296. 'indexes' => [
  297. 'name_created' => ['name', 'created'],
  298. 'expire' => ['expire'],
  299. ],
  300. ];
  301. }
  302. }