with_failure() { if ( $this->state->get_phase() === State::PHASE_MIGRATION_FAILURE_IN_PROGRESS ) { return false; } // Flag our new phase. $this->state->set( 'phase', State::PHASE_MIGRATION_FAILURE_IN_PROGRESS ); $this->state->set( 'locked_by_undo', true ); $this->state->save(); $this->undo(); return true; } /** * Starts the migration undoing process. * * @since 6.0.0 * */ protected function undo() { // Ensure Action Scheduler tables are there. ActionScheduler::store()->init(); // Clear all of our queued workers. $this->empty_process_queue(); // Now queue our undo loop. as_enqueue_async_action( Process_Worker::ACTION_UNDO ); } /** * Unschedules all of our process workers in the Action Schedule queue. * * @since 6.0.0 */ public function empty_process_queue() { // Clear all of our queued migration workers. as_unschedule_all_actions( Process_Worker::ACTION_PROCESS ); // Clear all of our queued state check workers. as_unschedule_all_actions( Process_Worker::ACTION_CHECK_PHASE ); // Clear all of our queued undo workers. as_unschedule_all_actions( Process_Worker::ACTION_UNDO ); } /** * Clean up when a queued migration worker is canceled. * * @since 6.0.0 * * @param $action_id numeric The action scheduler action ID */ public function cancel_async_action( $action_id ) { $store = ActionScheduler::store(); $action = $store->fetch_action( $action_id ); if ( $action->get_hook() !== Process_Worker::ACTION_PROCESS ) { return; } $args = $action->get_args(); $post_id = $args[0]; // Clear our migration state metadata so we are freed up for other operations. $event_report = new Event_Report( get_post( $post_id ) ); $event_report->clear_meta(); } /** * Clean up when our queued migration workers are canceled. * * @since 6.0.0 * * @param array $action_ids List of action IDs. */ public function cancel_async_actions( array $action_ids ) { foreach ( $action_ids as $action_id ) { $this->cancel_async_action( $action_id ); } } /** * Remove the migration report meta from all events. * * @since 6.0.0 * * @return void */ public function remove_migration_report_meta(): void { // Remove what migration phase flags might have been set by previous previews or migrations. delete_metadata( 'post', 0, Event_Report::META_KEY_MIGRATION_PHASE, '', true ); delete_metadata( 'post', 0, Event_Report::META_KEY_REPORT_DATA, '', true ); delete_metadata( 'post', 0, Event_Report::META_KEY_MIGRATION_LOCK_HASH, '', true ); } }