Migration Scheduler

Action_SchedulerMigrationScheduler manages the background scheduling of migration batches.

Since: 3.0.0
Source: classes/migration/Scheduler.php

Purpose

The Scheduler:

  • Schedules recurring migration batches as Action Scheduler actions
  • Checks if migration is already scheduled
  • Unschedules migration when complete
  • Hooks into the migration action to run batches

Constants

const HOOK = 'action_scheduler/migration_hook';
const GROUP = 'action-scheduler-migration';

Methods

hook()

public function hook(): void

Registers the migration callback:

add_action( self::HOOK, array( $this, 'run_migration' ) );

is_migration_scheduled()

public function is_migration_scheduled(): bool

Checks if a migration action is already pending:

return as_has_scheduled_action( self::HOOK, null, self::GROUP );

schedule_migration()

public function schedule_migration( int $interval = 0 ): void

Schedules the migration action to run. Default is immediate (async).

If $interval > 0, schedules a recurring action with that interval.

unschedule_migration()

public function unschedule_migration(): void

Removes all pending migration actions:

as_unschedule_all_actions( self::HOOK, null, self::GROUP );

run_migration()

public function run_migration(): void

Callback that runs when the migration action fires:

  1. Get migration config from Controller
  2. Create Runner with config
  3. Run a batch
  4. If actions were processed and more remain, reschedule
  5. If no actions processed, mark migration complete
public function run_migration() {
    $config = Controller::instance()->get_migration_config_object();
    $runner = new Runner( $config );
    
    $count = $runner->run();
    
    if ( $count > 0 ) {
        // More to migrate, schedule next batch
        $this->schedule_migration();
    } else {
        // Done!
        ActionScheduler_DataController::mark_migration_complete();
        do_action( 'action_scheduler/migration_complete' );
    }
}

Migration Flow

wp_loaded hook
    └── Controller::schedule_migration()
            └── Scheduler::schedule_migration()
                    └── as_enqueue_async_action( HOOK )

Migration action runs
    └── Scheduler::run_migration()
            └── Runner::run( batch_size )
                    ├── Actions remain? → Reschedule
                    └── No actions? → Mark complete

Filters

Filter Purpose
action_scheduler/migration_batch_size Adjust batch size (default 250)

Actions

Action When
action_scheduler/migration_complete Fires when all actions migrated

Completion

When migration completes:

  1. ActionScheduler_DataController::mark_migration_complete() is called
  2. Sets action_scheduler_migration_status option to 'complete'
  3. Future loads skip HybridStore, use DBStore directly
  4. action_scheduler/migration_complete action fires