Açıklama Yok

ActionScheduler_QueueCleaner.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Class ActionScheduler_QueueCleaner
  4. */
  5. class ActionScheduler_QueueCleaner {
  6. /** @var int */
  7. protected $batch_size;
  8. /** @var ActionScheduler_Store */
  9. private $store = null;
  10. /**
  11. * 31 days in seconds.
  12. *
  13. * @var int
  14. */
  15. private $month_in_seconds = 2678400;
  16. /**
  17. * ActionScheduler_QueueCleaner constructor.
  18. *
  19. * @param ActionScheduler_Store $store The store instance.
  20. * @param int $batch_size The batch size.
  21. */
  22. public function __construct( ActionScheduler_Store $store = null, $batch_size = 20 ) {
  23. $this->store = $store ? $store : ActionScheduler_Store::instance();
  24. $this->batch_size = $batch_size;
  25. }
  26. public function delete_old_actions() {
  27. $lifespan = apply_filters( 'action_scheduler_retention_period', $this->month_in_seconds );
  28. $cutoff = as_get_datetime_object($lifespan.' seconds ago');
  29. $statuses_to_purge = array(
  30. ActionScheduler_Store::STATUS_COMPLETE,
  31. ActionScheduler_Store::STATUS_CANCELED,
  32. );
  33. foreach ( $statuses_to_purge as $status ) {
  34. $actions_to_delete = $this->store->query_actions( array(
  35. 'status' => $status,
  36. 'modified' => $cutoff,
  37. 'modified_compare' => '<=',
  38. 'per_page' => $this->get_batch_size(),
  39. 'orderby' => 'none',
  40. ) );
  41. foreach ( $actions_to_delete as $action_id ) {
  42. try {
  43. $this->store->delete_action( $action_id );
  44. } catch ( Exception $e ) {
  45. /**
  46. * Notify 3rd party code of exceptions when deleting a completed action older than the retention period
  47. *
  48. * This hook provides a way for 3rd party code to log or otherwise handle exceptions relating to their
  49. * actions.
  50. *
  51. * @since 2.0.0
  52. *
  53. * @param int $action_id The scheduled actions ID in the data store
  54. * @param Exception $e The exception thrown when attempting to delete the action from the data store
  55. * @param int $lifespan The retention period, in seconds, for old actions
  56. * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch
  57. */
  58. do_action( 'action_scheduler_failed_old_action_deletion', $action_id, $e, $lifespan, count( $actions_to_delete ) );
  59. }
  60. }
  61. }
  62. }
  63. /**
  64. * Unclaim pending actions that have not been run within a given time limit.
  65. *
  66. * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed
  67. * as a parameter is 10x the time limit used for queue processing.
  68. *
  69. * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes).
  70. */
  71. public function reset_timeouts( $time_limit = 300 ) {
  72. $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit );
  73. if ( $timeout < 0 ) {
  74. return;
  75. }
  76. $cutoff = as_get_datetime_object($timeout.' seconds ago');
  77. $actions_to_reset = $this->store->query_actions( array(
  78. 'status' => ActionScheduler_Store::STATUS_PENDING,
  79. 'modified' => $cutoff,
  80. 'modified_compare' => '<=',
  81. 'claimed' => true,
  82. 'per_page' => $this->get_batch_size(),
  83. 'orderby' => 'none',
  84. ) );
  85. foreach ( $actions_to_reset as $action_id ) {
  86. $this->store->unclaim_action( $action_id );
  87. do_action( 'action_scheduler_reset_action', $action_id );
  88. }
  89. }
  90. /**
  91. * Mark actions that have been running for more than a given time limit as failed, based on
  92. * the assumption some uncatachable and unloggable fatal error occurred during processing.
  93. *
  94. * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed
  95. * as a parameter is 10x the time limit used for queue processing.
  96. *
  97. * @param int $time_limit The number of seconds to allow an action to run before it is considered to have failed. Default 300 (5 minutes).
  98. */
  99. public function mark_failures( $time_limit = 300 ) {
  100. $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit );
  101. if ( $timeout < 0 ) {
  102. return;
  103. }
  104. $cutoff = as_get_datetime_object($timeout.' seconds ago');
  105. $actions_to_reset = $this->store->query_actions( array(
  106. 'status' => ActionScheduler_Store::STATUS_RUNNING,
  107. 'modified' => $cutoff,
  108. 'modified_compare' => '<=',
  109. 'per_page' => $this->get_batch_size(),
  110. 'orderby' => 'none',
  111. ) );
  112. foreach ( $actions_to_reset as $action_id ) {
  113. $this->store->mark_failure( $action_id );
  114. do_action( 'action_scheduler_failed_action', $action_id, $timeout );
  115. }
  116. }
  117. /**
  118. * Do all of the cleaning actions.
  119. *
  120. * @param int $time_limit The number of seconds to use as the timeout and failure period. Default 300 (5 minutes).
  121. * @author Jeremy Pry
  122. */
  123. public function clean( $time_limit = 300 ) {
  124. $this->delete_old_actions();
  125. $this->reset_timeouts( $time_limit );
  126. $this->mark_failures( $time_limit );
  127. }
  128. /**
  129. * Get the batch size for cleaning the queue.
  130. *
  131. * @author Jeremy Pry
  132. * @return int
  133. */
  134. protected function get_batch_size() {
  135. /**
  136. * Filter the batch size when cleaning the queue.
  137. *
  138. * @param int $batch_size The number of actions to clean in one batch.
  139. */
  140. return absint( apply_filters( 'action_scheduler_cleanup_batch_size', $this->batch_size ) );
  141. }
  142. }