How can I ensure that each WP Cron scheduled task is completed?
-
I'm planning to create a plugin that modifies posts in the background using WP Cron and a large number of tasks will be scheduled. As I look into the wp-cron.php file, cron tasks are performed in a loop. foreach ( $crons as $timestamp => $cronhooks ) { if ( $timestamp > $gmt_time ) break; foreach ( $cronhooks as $hook => $keys ) { foreach ( $keys as $k => $v ) { $schedule = $v['schedule']; if ( $schedule != false ) { $new_args = array($timestamp, $schedule, $hook, $v['args']); call_user_func_array('wp_reschedule_event', $new_args); } wp_unschedule_event( $timestamp, $hook, $v['args'] ); /** * Fires scheduled events. * * @since 2.1.0 * * @param string $hook Name of the hook that was scheduled to be fired. * @param array $v['args'] The arguments to be passed to the hook. */ do_action_ref_array( $hook, $v['args'] ); // If the hook ran too long and another cron process stole the lock, quit. if ( _get_cron_lock() != $doing_wp_cron ) return; } } } Say I have 1000 separate tasks (each task modifies a post) scheduled in a short period of time, how can I ensure that each task is completed? I suspect that it is possible for the script to hit the maximum execution time during the loop. If that happens, there will be some posts that are not modified.
-
Answer:
You did not say how often you would need to modify posts in the background, or if you needed those posts to be all modified immediately, but let's assume that "soon" instead of is good enough. If yes then do your process into multiple steps. Schedule your cron task to run once per minute and each time do a small number of task, maybe 25 and just process them 25 at a time. That means that as long as your are getting at least one visitor a minute your 1000 tasks will be completed in less than 1 hour. However, if your plugin only knows what it needs to generate tasks at the time it generates all 1000 tasks then divide them into batches of task with maybe 25 tasks per batch. Then for each batch store the information you need to be able to modify those posts into an array, and use update_option() to serialize your data to the wp_options tables with option names (something) like "myplugin_batch1", "myplugin_batch2", "myplugin_batch3" and so on. Then when your cron fires each minute do a MySQL query of the wp_options table looking for the first record that has option_name LIKE 'myplugin_batch%' assuming the query sorts by option_name in ascending order. Use get_option() to retrieve and unserialize the value, run your updates, and then delete that option with delete_option(). Instead of using the wp_options table you could also use a "Task Batch" custom post type to store your "batches" and store your task information in post meta instead of a serialize array in the options table, your preference. However, if you must make all 1000 updates as quickly as possible you could do the first 25, then store all the information you need to update all the remaining 975 posts as a serialized array in a single option in the wp_options tables, schedule a new task to execute immediately, and then redirect to /wp-cron.php using wp_safe_redirect(). Your new scheduled task will then execute and it should load the serialized array from the wp_options table, pull out 25 tasks, do them, save the remaining tasks back to the single option in the wp_options tables, schedule another new task to execute immediately, and finally redirect to /wp-cron.php. If your new task loads and there is not anything left in your single option in the wp_options table then you are done and no need to redirect to /wp-cron.php. Hope this helps. Please let me know if it does.
Mike Schinkel at Quora Visit the source
Related Q & A:
- How can I find an online job that I can start for free?Best solution by Yahoo! Answers
- How can I ask my mom if I can have a boyfriend?Best solution by Yahoo! Answers
- I can not send out my emails! How can I fix it.
- What exactly can I do with Facebook mobile and how can I do it?Best solution by Yahoo! Answers
- How can I get Bahrain police clearance and were can I apply for it in Australia?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.