Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#19663 closed enhancement (fixed)

auto-draft garbage collection should be done via cron, not when starting new post

Reported by: _ck_'s profile _ck_ Owned by: ryan's profile ryan
Milestone: 3.4 Priority: normal
Severity: normal Version: 3.3
Component: Performance Keywords: has-patch commit
Focuses: Cc:


On small sites this isn't a problem but active, larger sites with multiple authors, the issue becomes magnified.

A user should not have to wait when clicking on post -> add new
but it can happen because of auto-draft deletion is forced to happen at that time for ALL authors in a system.

in wp-admin/includes/post.php function get_default_post_to_edit
garbage collection is currently done like so

if ( $create_in_db ) {
		// Cleanup old auto-drafts more than 7 days old
		$old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
		foreach ( (array) $old_posts as $delete )
			wp_delete_post( $delete, true ); // Force delete
		$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
		$post = get_post( $post_id );
		if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
			set_post_format( $post, get_option( 'default_post_format' ) );

Seems trivial but it's not. It can cause a large number of queries to occur as meta is also cleared and causes plugins related to new/deleted posts to fire. Even if there is nothing to cleanup, the code still fires for every author on every new post.

I propose a simple change instead, where the deletion code is moved simply to it's own small function and added to a once-a-day wp-cron that could be fired for example early in the morning or re-scheduled as desired.

This way an author can be guaranteed to have faster access to "add new" since it's otherwise a very complex page that needs many more of it's own queries to initialize and often several plugins to affect the new post abilities.

Attachments (2)

patch_19663.diff (1.7 KB) - added by mgolawala 12 years ago.
Move deletion of auto-draft posts to the wp_scheduled_delete cron function
19663.diff (2.9 KB) - added by ryan 12 years ago.

Download all attachments as: .zip

Change History (12)

#1 @nacin
12 years ago

  • Milestone changed from Awaiting Review to 3.4
  • Type changed from defect (bug) to enhancement

Yeah, this is a good idea.

12 years ago

Move deletion of auto-draft posts to the wp_scheduled_delete cron function

#2 @mgolawala
12 years ago

  • Cc mgolawala added
  • Keywords has-patch added
  • Version changed from 3.3 to 3.3.1

I moved the inline deletion of auto-posts from the wp-admin/includes/post.php: get_default_post_to_edit and made it part of the daily garbage collection cron entry that cleans out posts, comments, pages, attachments etc.

Let me know if you would like it to be organized differently. From a code organization standpoint, I could use one of the alternate twotechniques:

  • Create a separate cron schedule for it to be more frequent than daily (configured via UI?)
  • Have the post.php entry queue up the separate cron entry for auto-post garbage collection, and then have the auto-post garbage collection function deque it self. This would end up invoking the garbage collection as often as before, just asynchronously.

Let me know your thoughts.

#3 @DrewAPicture
12 years ago

  • Version changed from 3.3.1 to 3.3

The version field is used to indicate the first version in which a problem was identified in, or reproduced in.

#4 @nacin
12 years ago

  • Keywords commit added

I like this.

#5 @scribu
12 years ago

  • Cc scribu added

#6 @scribu
12 years ago

There was an older ticket for this: #16785

#7 @ryan
12 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In [20440]:

Move deletion of auto-draft posts to the wp_scheduled_delete cron function. Props mgolawala. fixes #19663

#8 @ryan
12 years ago

This caused an avalanche on Let's schedule this only from post-new.php.

#9 @ryan
12 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

12 years ago

#10 @ryan
12 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In [20453]:

Schedule auto-draft deletion from post-new.php instead of from admin.php. This provides better throttling for large multisite installs and reduces the risk of a delete avalanche.

fixes #19663

Note: See TracTickets for help on using tickets.