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.
wp-config.php
, I'd forgotten about it. Will try without it.