[wp-trac] [WordPress Trac] #60657: wp_schedule_event problem
WordPress Trac
noreply at wordpress.org
Thu Feb 29 08:59:37 UTC 2024
#60657: wp_schedule_event problem
--------------------------+------------------------------
Reporter: odoremieux | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Cron API | Version: 6.4.3
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Changes (by dd32):
* component: General => Cron API
Old description:
> In some cases there are some problems with wp_schedule_event. It schedule
> the event, if we use wp_next_scheduled, it gives a timestamp, but using
> WP Crontrol, it doesn't appear there.
> Below some code to reproduce it.
> If you click on the button, it will schedule 500 events. Wait a bit, that
> 100 or so are processed (look at the logs or use WP Control to check).
> Then click again. The logs show that they should have been schedules,
> there is a timestamp and the transient is created. But it's not really
> scheduled, it doesn't appear with WP Control, and the transient is not
> deleted (for the 2nd round)
>
> if (!class_exists("NSM_Test_Cron")) {
>
> class NSM_Test_Cron {
>
> function __construct() {
> add_action('admin_menu', array($this,
> 'my_custom_button_menu'));
> add_filter('cron_schedules', array($this, 'cron_schedules'));
> add_action('nsm_test_cron', array($this, 'nsm_test_cron'));
> }
>
> public function cron_schedules($schedules) {
> if (!isset($schedules['nsm_60_seconds'])) {
> $schedules['nsm_60_seconds'] = array('interval' => 60,
> 'display' => '60 Seconds');
> }
> return $schedules;
> }
>
> function my_custom_button_menu() {
> add_menu_page('My Custom Button', 'Custom Button',
> 'manage_options', 'my-custom-admin-button', array($this,
> 'my_custom_button_page'));
> }
>
> function my_custom_button_page() {
> error_log('my_custom_button_page');
> if (isset($_POST['action']) && $_POST['action'] ==
> 'myaction') {
> error_log('my_custom_button_page action');
> for ($index = 0; $index < 500; $index++) {
> $args = array(array('email' => 'toto at foo.org',
> 'user_id' => '1234', 'tags' => $index));
> if (!wp_next_scheduled('nsm_test_cron', $args)) {
> $error = wp_schedule_event(time(),
> 'nsm_60_seconds', 'nsm_test_cron', $args, true);
> set_transient( 'doing_cron_' . $index, $index );
> error_log('error -> ' . print_r($error, true));
> error_log("wp_next_scheduled ($index)-> " .
> print_r(wp_next_scheduled('nsm_test_cron', $args), true));
> }
> }
> }
> ?>
> <div class="wrap">
> <h2>My Custom Button</h2>
> <form action="" method="post">
> <input type="hidden" name="action"
> value="submit_my_custom_form">
> <?php wp_nonce_field('my_custom_form_nonce'); ?>
> <input type="text" name="my_custom_data"
> placeholder="Enter some data">
> <input type="hidden" name="action" value="myaction">
> <input type="submit" class="button button-primary"
> value="Submit">
> </form>
> </div>
> <?php
> }
>
> public function nsm_test_cron($args) {
> $error = wp_clear_scheduled_hook('nsm_test_cron',
> array($args));
> error_log('nsm_test_cron -> ' . $args['tags']);
> sleep(1);
> delete_transient( 'doing_cron_' . $args['tags']);
> }
> }
>
> }
> new NSM_Test_Cron();
New description:
In some cases there are some problems with wp_schedule_event. It schedule
the event, if we use wp_next_scheduled, it gives a timestamp, but using WP
Crontrol, it doesn't appear there.
Below some code to reproduce it.
If you click on the button, it will schedule 500 events. Wait a bit, that
100 or so are processed (look at the logs or use WP Control to check).
Then click again. The logs show that they should have been schedules,
there is a timestamp and the transient is created. But it's not really
scheduled, it doesn't appear with WP Control, and the transient is not
deleted (for the 2nd round)
{{{
if (!class_exists("NSM_Test_Cron")) {
class NSM_Test_Cron {
function __construct() {
add_action('admin_menu', array($this,
'my_custom_button_menu'));
add_filter('cron_schedules', array($this, 'cron_schedules'));
add_action('nsm_test_cron', array($this, 'nsm_test_cron'));
}
public function cron_schedules($schedules) {
if (!isset($schedules['nsm_60_seconds'])) {
$schedules['nsm_60_seconds'] = array('interval' => 60,
'display' => '60 Seconds');
}
return $schedules;
}
function my_custom_button_menu() {
add_menu_page('My Custom Button', 'Custom Button',
'manage_options', 'my-custom-admin-button', array($this,
'my_custom_button_page'));
}
function my_custom_button_page() {
error_log('my_custom_button_page');
if (isset($_POST['action']) && $_POST['action'] == 'myaction')
{
error_log('my_custom_button_page action');
for ($index = 0; $index < 500; $index++) {
$args = array(array('email' => 'toto at foo.org',
'user_id' => '1234', 'tags' => $index));
if (!wp_next_scheduled('nsm_test_cron', $args)) {
$error = wp_schedule_event(time(),
'nsm_60_seconds', 'nsm_test_cron', $args, true);
set_transient( 'doing_cron_' . $index, $index );
error_log('error -> ' . print_r($error, true));
error_log("wp_next_scheduled ($index)-> " .
print_r(wp_next_scheduled('nsm_test_cron', $args), true));
}
}
}
?>
<div class="wrap">
<h2>My Custom Button</h2>
<form action="" method="post">
<input type="hidden" name="action"
value="submit_my_custom_form">
<?php wp_nonce_field('my_custom_form_nonce'); ?>
<input type="text" name="my_custom_data"
placeholder="Enter some data">
<input type="hidden" name="action" value="myaction">
<input type="submit" class="button button-primary"
value="Submit">
</form>
</div>
<?php
}
public function nsm_test_cron($args) {
$error = wp_clear_scheduled_hook('nsm_test_cron',
array($args));
error_log('nsm_test_cron -> ' . $args['tags']);
sleep(1);
delete_transient( 'doing_cron_' . $args['tags']);
}
}
}
new NSM_Test_Cron();
}}}
--
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60657#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list