admin管理员组文章数量:1130349
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.
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.
2 Answers
Reset to default 2Cron 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.
According to the WP Plugin Handbook "Any tasks scheduled to be run will be run during that page load." Thus, it will always slow down the page load and should not be used to directly replace an actual CRON (or scheduled task) for large tasks that the user should not have to wait for.
A better solution is to set up a WP Cron task to fire off an asynchronous task that will do the work in the background - most easily done with a library such as this one. This way the user will not have to wait for the process to finish, and the process will not be killed if the user disconnects.
Alternatively, one could use a service like like AWS Lambda or CronJob to kick off the process.
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.
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.
- 1 Have you tried alternate cron? define('ALTERNATE_WP_CRON', true); codex.wordpress/Editing_wp-config.php#Alternative_Cron – Andrew Bartel Commented May 22, 2013 at 16:40
-
@AndrewBartel Hm, it is actually the current setting in
wp-config.php, I'd forgotten about it. Will try without it. – montrealist Commented May 22, 2013 at 16:48 - @AndrewBartel Just edited my answer with new info. – montrealist Commented May 22, 2013 at 18:26
2 Answers
Reset to default 2Cron 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.
According to the WP Plugin Handbook "Any tasks scheduled to be run will be run during that page load." Thus, it will always slow down the page load and should not be used to directly replace an actual CRON (or scheduled task) for large tasks that the user should not have to wait for.
A better solution is to set up a WP Cron task to fire off an asynchronous task that will do the work in the background - most easily done with a library such as this one. This way the user will not have to wait for the process to finish, and the process will not be killed if the user disconnects.
Alternatively, one could use a service like like AWS Lambda or CronJob to kick off the process.
本文标签: run a cron task without obstructing page load
版权声明:本文标题:run a cron task without obstructing page load? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749055571a2309288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp-config.php, I'd forgotten about it. Will try without it. – montrealist Commented May 22, 2013 at 16:48