LockBackendInterface.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Drupal\Core\Lock;
  3. /**
  4. * @defgroup lock Locking mechanisms
  5. * @{
  6. * Functions to coordinate long operations across requests.
  7. *
  8. * In most environments, multiple Drupal page requests (a.k.a. threads or
  9. * processes) will execute in parallel. This leads to potential conflicts or
  10. * race conditions when two requests execute the same code at the same time. For
  11. * instance, some implementations of hook_cron() implicitly assume they are
  12. * running only once, rather than having multiple calls in parallel. To prevent
  13. * problems with such code, the cron system uses a locking process to ensure
  14. * that cron is not started again if it is already running.
  15. *
  16. * To avoid these types of conflicts, Drupal has a cooperative, advisory lock
  17. * system. Any long-running operation that could potentially be attempted in
  18. * parallel by multiple requests should try to acquire a lock before
  19. * proceeding. By obtaining a lock, one request notifies any other requests that
  20. * a specific operation is in progress which must not be executed in parallel.
  21. *
  22. * To use this API, pick a unique name for the lock. A sensible choice is the
  23. * name of the function performing the operation. Here is a simple example:
  24. * @code
  25. * function mymodule_long_operation() {
  26. * $lock = \Drupal::lock();
  27. * if ($lock->acquire('mymodule_long_operation')) {
  28. * // Do the long operation here.
  29. * // ...
  30. * $lock->release('mymodule_long_operation');
  31. * }
  32. * }
  33. * @endcode
  34. *
  35. * If a function acquires a lock it should always release it when the operation
  36. * is complete by calling $lock->release(), as in the example.
  37. *
  38. * A function that has acquired a lock may attempt to renew a lock (extend the
  39. * duration of the lock) by calling $lock->acquire() again during the operation.
  40. * Failure to renew a lock is indicative that another request has acquired the
  41. * lock, and that the current operation may need to be aborted.
  42. *
  43. * If a function fails to acquire a lock it may either immediately return, or
  44. * it may call $lock->wait() if the rest of the current page request requires
  45. * that the operation in question be complete. After $lock->wait() returns, the
  46. * function may again attempt to acquire the lock, or may simply allow the page
  47. * request to proceed on the assumption that a parallel request completed the
  48. * operation.
  49. *
  50. * $lock->acquire() and $lock->wait() will automatically break (delete) a lock
  51. * whose duration has exceeded the timeout specified when it was acquired.
  52. *
  53. * The following limitations in this implementation should be carefully noted:
  54. * - Time: Timestamps are derived from the local system clock of the environment
  55. * the code is executing in. The orderly progression of time from this
  56. * viewpoint can be disrupted by external events such as NTP synchronization
  57. * and operator intervention. Where multiple web servers are involved in
  58. * serving the site, they will have their own independent clocks, introducing
  59. * another source of error in the time keeping process. Timeout values applied
  60. * to locks must therefore be considered approximate, and should not be relied
  61. * upon.
  62. * - Uniqueness: Uniqueness of lock names is not enforced. The impact of the
  63. * use of a common lock name will depend on what processes and resources the
  64. * lock is being used to manage.
  65. * - Sharing: There is limited support for resources shared across sites.
  66. * The locks are stored as rows in the semaphore table and, as such, they
  67. * have the same visibility as the table. If resources managed by a lock are
  68. * shared across sites then the semaphore table must be shared across sites
  69. * as well. This is a binary situation: either all resources are shared and
  70. * the semaphore table is shared or no resources are shared and the semaphore
  71. * table is not shared. Mixed mode operation is not supported.
  72. *
  73. * @} End of "defgroup lock".
  74. */
  75. /**
  76. * Lock backend interface.
  77. *
  78. * @ingroup lock
  79. */
  80. interface LockBackendInterface {
  81. /**
  82. * Acquires a lock.
  83. *
  84. * @param string $name
  85. * Lock name. Limit of name's length is 255 characters.
  86. * @param float $timeout
  87. * (optional) Lock lifetime in seconds. Defaults to 30.0.
  88. *
  89. * @return bool
  90. */
  91. public function acquire($name, $timeout = 30.0);
  92. /**
  93. * Checks if a lock is available for acquiring.
  94. *
  95. * @param string $name
  96. * Lock to acquire.
  97. *
  98. * @return bool
  99. */
  100. public function lockMayBeAvailable($name);
  101. /**
  102. * Waits a short amount of time before a second lock acquire attempt.
  103. *
  104. * While this method is subject to have a generic implementation in abstract
  105. * backend implementation, some backends may provide non blocking or less I/O
  106. * intensive wait mechanism: this is why this method remains on the backend
  107. * interface.
  108. *
  109. * @param string $name
  110. * Lock name currently being locked.
  111. * @param int $delay
  112. * Seconds to wait for. Defaults to 30.
  113. *
  114. * @return bool
  115. * TRUE if the lock holds, FALSE if it may be available. You still need to
  116. * acquire the lock manually and it may fail again.
  117. */
  118. public function wait($name, $delay = 30);
  119. /**
  120. * Releases the given lock.
  121. *
  122. * @param string $name
  123. */
  124. public function release($name);
  125. /**
  126. * Releases all locks for the given lock token identifier.
  127. *
  128. * @param string $lockId
  129. * (optional) If none given, remove all locks from the current page.
  130. * Defaults to NULL.
  131. */
  132. public function releaseAll($lockId = NULL);
  133. /**
  134. * Gets the unique page token for locks.
  135. *
  136. * Locks will be wiped out at the end of each page request on a token basis.
  137. *
  138. * @return string
  139. */
  140. public function getLockId();
  141. }