Нет описания

Scheduler.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Action_Scheduler\Migration;
  3. /**
  4. * Class Scheduler
  5. *
  6. * @package Action_Scheduler\WP_CLI
  7. *
  8. * @since 3.0.0
  9. *
  10. * @codeCoverageIgnore
  11. */
  12. class Scheduler {
  13. /** Migration action hook. */
  14. const HOOK = 'action_scheduler/migration_hook';
  15. /** Migration action group. */
  16. const GROUP = 'action-scheduler-migration';
  17. /**
  18. * Set up the callback for the scheduled job.
  19. */
  20. public function hook() {
  21. add_action( self::HOOK, array( $this, 'run_migration' ), 10, 0 );
  22. }
  23. /**
  24. * Remove the callback for the scheduled job.
  25. */
  26. public function unhook() {
  27. remove_action( self::HOOK, array( $this, 'run_migration' ), 10 );
  28. }
  29. /**
  30. * The migration callback.
  31. */
  32. public function run_migration() {
  33. $migration_runner = $this->get_migration_runner();
  34. $count = $migration_runner->run( $this->get_batch_size() );
  35. if ( $count === 0 ) {
  36. $this->mark_complete();
  37. } else {
  38. $this->schedule_migration( time() + $this->get_schedule_interval() );
  39. }
  40. }
  41. /**
  42. * Mark the migration complete.
  43. */
  44. public function mark_complete() {
  45. $this->unschedule_migration();
  46. \ActionScheduler_DataController::mark_migration_complete();
  47. do_action( 'action_scheduler/migration_complete' );
  48. }
  49. /**
  50. * Get a flag indicating whether the migration is scheduled.
  51. *
  52. * @return bool Whether there is a pending action in the store to handle the migration
  53. */
  54. public function is_migration_scheduled() {
  55. $next = as_next_scheduled_action( self::HOOK );
  56. return ! empty( $next );
  57. }
  58. /**
  59. * Schedule the migration.
  60. *
  61. * @param int $when Optional timestamp to run the next migration batch. Defaults to now.
  62. *
  63. * @return string The action ID
  64. */
  65. public function schedule_migration( $when = 0 ) {
  66. $next = as_next_scheduled_action( self::HOOK );
  67. if ( ! empty( $next ) ) {
  68. return $next;
  69. }
  70. if ( empty( $when ) ) {
  71. $when = time() + MINUTE_IN_SECONDS;
  72. }
  73. return as_schedule_single_action( $when, self::HOOK, array(), self::GROUP );
  74. }
  75. /**
  76. * Remove the scheduled migration action.
  77. */
  78. public function unschedule_migration() {
  79. as_unschedule_action( self::HOOK, null, self::GROUP );
  80. }
  81. /**
  82. * Get migration batch schedule interval.
  83. *
  84. * @return int Seconds between migration runs. Defaults to 0 seconds to allow chaining migration via Async Runners.
  85. */
  86. private function get_schedule_interval() {
  87. return (int) apply_filters( 'action_scheduler/migration_interval', 0 );
  88. }
  89. /**
  90. * Get migration batch size.
  91. *
  92. * @return int Number of actions to migrate in each batch. Defaults to 250.
  93. */
  94. private function get_batch_size() {
  95. return (int) apply_filters( 'action_scheduler/migration_batch_size', 250 );
  96. }
  97. /**
  98. * Get migration runner object.
  99. *
  100. * @return Runner
  101. */
  102. private function get_migration_runner() {
  103. $config = Controller::instance()->get_migration_config_object();
  104. return new Runner( $config );
  105. }
  106. }