ActionMigrator.php
1 year ago
ActionScheduler_DBStoreMigrator.php
1 year ago
BatchFetcher.php
1 year ago
Config.php
4 years ago
Controller.php
1 year ago
DryRun_ActionMigrator.php
1 year ago
DryRun_LogMigrator.php
1 year ago
LogMigrator.php
1 year ago
Runner.php
1 year ago
Scheduler.php
1 year ago
index.php
3 years ago
Scheduler.php
55 lines
| 1 | <?php |
| 2 | namespace Action_Scheduler\Migration; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | class Scheduler { |
| 5 | const HOOK = 'action_scheduler/migration_hook'; |
| 6 | const GROUP = 'action-scheduler-migration'; |
| 7 | public function hook() { |
| 8 | add_action( self::HOOK, array( $this, 'run_migration' ), 10, 0 ); |
| 9 | } |
| 10 | public function unhook() { |
| 11 | remove_action( self::HOOK, array( $this, 'run_migration' ), 10 ); |
| 12 | } |
| 13 | public function run_migration() { |
| 14 | $migration_runner = $this->get_migration_runner(); |
| 15 | $count = $migration_runner->run( $this->get_batch_size() ); |
| 16 | if ( 0 === $count ) { |
| 17 | $this->mark_complete(); |
| 18 | } else { |
| 19 | $this->schedule_migration( time() + $this->get_schedule_interval() ); |
| 20 | } |
| 21 | } |
| 22 | public function mark_complete() { |
| 23 | $this->unschedule_migration(); |
| 24 | \ActionScheduler_DataController::mark_migration_complete(); |
| 25 | do_action( 'action_scheduler/migration_complete' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 26 | } |
| 27 | public function is_migration_scheduled() { |
| 28 | $next = as_next_scheduled_action( self::HOOK ); |
| 29 | return ! empty( $next ); |
| 30 | } |
| 31 | public function schedule_migration( $when = 0 ) { |
| 32 | $next = as_next_scheduled_action( self::HOOK ); |
| 33 | if ( ! empty( $next ) ) { |
| 34 | return $next; |
| 35 | } |
| 36 | if ( empty( $when ) ) { |
| 37 | $when = time() + MINUTE_IN_SECONDS; |
| 38 | } |
| 39 | return as_schedule_single_action( $when, self::HOOK, array(), self::GROUP ); |
| 40 | } |
| 41 | public function unschedule_migration() { |
| 42 | as_unschedule_action( self::HOOK, null, self::GROUP ); |
| 43 | } |
| 44 | private function get_schedule_interval() { |
| 45 | return (int) apply_filters( 'action_scheduler/migration_interval', 0 ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 46 | } |
| 47 | private function get_batch_size() { |
| 48 | return (int) apply_filters( 'action_scheduler/migration_batch_size', 250 ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 49 | } |
| 50 | private function get_migration_runner() { |
| 51 | $config = Controller::instance()->get_migration_config_object(); |
| 52 | return new Runner( $config ); |
| 53 | } |
| 54 | } |
| 55 |