Skip to content

Commit

Permalink
Merge pull request #399 from woocommerce/fix/398
Browse files Browse the repository at this point in the history
mark action as migrated if delete fails
  • Loading branch information
thenbrent authored Dec 19, 2019
2 parents 8a2deb3 + 78f13b5 commit 9f015cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions classes/abstracts/ActionScheduler_Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,16 @@ public function has_pending_actions_due() {
return ! empty( $pending_actions );
}

/**
* Callable initialization function optionally overridden in derived classes.
*/
public function init() {}

/**
* Callable function to mark an action as migrated optionally overridden in derived classes.
*/
public function mark_migrated( $action_id ) {}

/**
* @return ActionScheduler_Store
*/
Expand Down
17 changes: 16 additions & 1 deletion classes/data-stores/ActionScheduler_wpPostStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ public function delete_action( $action_id ) {
throw new InvalidArgumentException(sprintf(__('Unidentified action %s', 'action-scheduler'), $action_id));
}
do_action( 'action_scheduler_deleted_action', $action_id );
wp_delete_post($action_id, TRUE);

wp_delete_post( $action_id, TRUE );
}

/**
Expand Down Expand Up @@ -787,6 +788,20 @@ public function mark_complete( $action_id ) {
}
}

/**
* Mark action as migrated when there is an error deleting the action.
*
* @param int $action_id Action ID.
*/
public function mark_migrated( $action_id ) {
wp_update_post(
array(
'ID' => $action_id,
'post_status' => 'migrated'
)
);
}

/**
* Determine whether the post store can be migrated.
*
Expand Down
5 changes: 5 additions & 0 deletions classes/migration/ActionMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ public function migrate( $source_action_id ) {
$this->log_migrator->migrate( $source_action_id, $destination_action_id );
$this->source->delete_action( $source_action_id );

$test_action = $this->source->fetch_action( $source_action_id );
if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) {
throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) );
}
do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );

return $destination_action_id;
} catch ( \Exception $e ) {
// could not delete from the old store
$this->source->mark_migrated( $source_action_id );
do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );

Expand Down

0 comments on commit 9f015cb

Please sign in to comment.