Friends who often use WordPress for development may know that WordPress has its own scheduled task system (WP-Cron). This Cron system is very different from the Linux Cron system.
- Linux Cron: As long as the specified time is reached and the system is running, the task will be automatically executed.
- WP-Cron: It needs to be triggered by website access. The specific process is:
- WordPress stores scheduled task information in the database
- When a user accesses a website, the system checks to see if there are any pending tasks
- If there is a task that needs to be executed, execute it immediately
Issues with the WordPress scheduled task scheduling system
This design of WP Cron poses two main problems:
- Execution time is inaccurate: The task may not be executed at the exact time set
- May miss execution: If no user visits the website within the planned time, the task will not be triggered.
This mechanism has less impact on heavily visited websites, but is not suitable for critical tasks that need to be performed on time.
A real timed task example
Suppose we need to implement a function that checks CSV files in a specific directory every hour, and if the file exists, import it to the database and delete the file. Using plain WP-Cron may result in tasks not being executed on time.
Optimization plan: Combined with system Cron to trigger WP Cron
We know that as long as the scheduled tasks of the operating system are set up, they will be automatically executed when the time comes, with no other additional conditions. So to solve this problem, we need to set up an operating system-level task schedule to simulate users visiting the site regularly.
In order to avoid conflicts between the task schedule of the operating system and the task schedule of WordPress, causing repeated execution problems, we first disable the task schedule system of WordPress, and then usewp_schedule_eventSchedule our task planning operations.
1. Disable WP Cron scheduled task system
existwp-config.php, add the following definition code:
define('DISABLE_WP_CRON', true);
2. Add event operation
First, define an hourly action and specify a function name that needs to be executed. In the example below, it is update_db_hourly.
add_action( 'my_hourly_event', 'update_db_hourly' );
3. Add scheduled task function
Now, define the update_db_hourly function to use WordPress to schedule events. If it is in a plug-in, we can schedule the task when the plug-in is activated (also, don’t forget to remove the schedule when the plug-in 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 actual operation function, which is update_db_hourly specified in the first step.
public function update_db_hourly() {
// 1. 检查是否有文件
// 2. 如果有、导入,然后删除
// 3. 如果没有,不执行任何操作
}
4. Set up scheduled tasks
Hosts based on cPanel or other panels generally have an interface for customizing scheduled tasks. We can set scheduled tasks very conveniently through the Web interface. Some servers do not have a graphical interface and can only set scheduled tasks by accessing the command line and using commands. Linux systems can passcrontab -eTo edit the scheduled task, add the following instructions to the scheduled task file, and then save it.
*/15 * * * * wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron
Or you can use curl to achieve the same purpose.
*/15 * * * * curl --silent "https://yourdomain.com/wp-cron.php?doing_wp_cron" > /dev/null 2>&1
The above settings will execute every 15 minutes, making a request to the WordPress site, thus starting the scheduled task we set up.
Setting up a scheduled task is not a very complicated process, but unless you are familiar with how the WordPress scheduled task system works, you will encounter 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 common scheduled operations, we can also implement it based on the WordPress scheduled task systemAsynchronous PHPandscheduled task queue, friends in need can click on the link to learn more.
