Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a plugin that lets users upload post attachments via AJAX in the admin area. I later try to push these files to DropBox via their REST API. It almost works. The issue is with WP cron task which, when running, prevents the page from loading until the push to DropBox finishes (which can take up to 30 minutes for larger files).

I've looked at similar questions (here and here), but the latter implies that these cron tasks do, indeed, prevent the page from loading properly as the task is running.

Is it possible to set it up so that page loads are unaffected by cron task executions? Perhaps I'm not doing it the right way.

Here are the relevant bits of my plugin:

function __construct() {

    add_action( 'wp_ajax_my_admin_upload_attachment', array( $this, 'my_admin_upload_attachment') );
    add_action('my_push_to_dropbox', array($this,'push_to_dropbox'));

} // end constructor

function my_admin_upload_attachment() {
    /* upload and save file locally, update post meta */
    /* schedule the files to be uploaded to DropBox */
    wp_schedule_single_event(time()+5, 'my_push_to_dropbox');
    /* return success JSON */
}

function push_to_dropbox() {
    $this->log('push_to_dropbox - started.');
    //$this->do_push_to_dropbox();
    @ignore_user_abort(true); // this does not help: 'finished' message still never prints
    set_time_limit(0);
    sleep(30); // this mimicks the long-running upload task
    $this->log('push_to_dropbox - finished.');
}

EDIT: removed define('ALTERNATE_WP_CRON', true); from wp-config.php, now the pages load without delay, but cron task gets interrupted by successive page refreshes: the 'finished' message above doesn't get printed in the log.

share|improve this question
1  
Have you tried alternate cron? define('ALTERNATE_WP_CRON', true); codex.wordpress.org/Editing_wp-config.php#Alternative_Cron –  Andrew Bartel May 22 '13 at 16:40
    
@AndrewBartel Hm, it is actually the current setting in wp-config.php, I'd forgotten about it. Will try without it. –  dalbaeb May 22 '13 at 16:48
    
@AndrewBartel Just edited my answer with new info. –  dalbaeb May 22 '13 at 18:26
add comment

1 Answer

Cron tasks don't directly affect page load besides the 0.01s timeout (of course, they do affect server load, so they can impact page load indirectly).

My hunch is that your PHP config is the issue. The cron tasks run like any other web request, and they're subject to PHP's max_execution_time INI setting. If this is set to, say, 90 seconds, then the request will die and your files won't sync. You can test this by setting sleep to a lower number than your max_execution_time, and see if it finishes.

If that is the case, you can try adding:

ini_set( 'max_execution_time', 0 );

to the top of your push_to_dropbox() function. It's not a great solution, because it won't work on every host, but it's a start.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.