Using OS Scheduled Task Triggers, Properly Setting WordPress Timed Scheduled Tasks (cron job)
Those who often use WordPress for development may know that WordPress has its own set of scheduled tasks system (WP-Cron), this set of Cron system and Linux Cron system is very different.
- Linux Cron: Tasks are executed automatically as soon as the specified time is reached and the system is running.
- WP-Cron: It needs to rely on a website visit to trigger it, the exact process is:
- WordPress Stores Scheduled Task Information in a Database
- When a user visits the website, the system checks for pending tasks to be performed
- If a task exists that needs to be performed, it is performed immediately
Problems with WordPress Timed Task Scheduler
This design of WP Cron poses two major problems:
- Inaccurate implementation time:: Tasks may not be performed at the exact time set
- Possible missed implementation: If no user visits the site during the scheduled time, the task will not be triggered
This mechanism has less of an impact on heavily visited sites, but is not suitable for mission-critical tasks that need to be performed on time.
A real example of a timed task
Suppose we need to implement a feature that checks for CSV files in a specific directory every hour, imports them to the database if they exist and deletes the files. Using normal WP-Cron may result in the task not being executed on time.
Optimization: Combining System Cron to Trigger WP Cron
As we know, the scheduled tasks of the operating system, as long as they are set up, will be executed automatically when the time comes, without any other additional conditions. So to solve this problem, we need to set up an operating system level task schedule that simulates a user visiting the site at regular intervals.
In order to avoid conflicts between the operating system's task plan and WordPress's task plan, which can cause duplicate execution problems, we first disable WordPress's task plan system and then use the wp_schedule_event Scheduling our mission plan operations.
1. Disable the WP Cron timer system.
In wp-config.php, add the following definition code:
define('DISABLE_WP_CRON', true).;
2. Add-ons
First, define an hourly specified action by specifying the name of a function that needs to be executed, in the example below, update_db_hourly.
add_action( 'my_hourly_event', 'update_db_hourly' );
3. Adding timed task functions
Now, define the update_db_hourly function to schedule things using WordPress. If it's in a plugin, we can schedule the task when the plugin is active, (also, don't forget to remove scheduling when the plugin is disabled):.
public static function activate() {
wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
}
public static function deactivate() {
wp_clear_scheduled_hook('my_hourly_event');
}
Finally, define the function that performs the actual operation, which is update_db_hourly as specified in the first step.
public function update_db_hourly() {
// 1. Check if there is a file
// 2. If there is, import it and delete it.
// 3. If not, don't do anything.
}
4. Setting up scheduled tasks
Hosts based on cPanel or other panels generally have interfaces for customizing scheduled tasks, and we can set up scheduled tasks very easily through the Web interface. some servers are not in the graphical interface, and can only be set up through commands by accessing the command line. Linux systems can be set up through the crontab -e To edit a scheduled task, add the following command to the scheduled task file and save it.
*/15 * * * * * wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron
Or you can use curl for the same purpose.
*/15 * * * * * * curl --silent "https://yourdomain.com/wp-cron.php?doing_wp_cron" > /dev/null 2>&1
The above settings will be executed every 15 minutes, sending a request to the WordPress site to start the scheduled task we have set up.
Setting up scheduled tasks is not a very complicated process, but unless you are familiar with how the WordPress timed task system works, you will run into the problems we mentioned above when using it. Hopefully this article will help you understand how WordPress handles scheduled tasks and set up WordPress task schedules correctly when needed.
In addition to the common timed actions, we can also build on the WordPress Scheduled Tasks system to implement theAsynchronous PHP 和Scheduled Task Queue, those who need it can click on the link for details.