WP Cron jobs loops infinitely

I am writing a script to add a named Cron job that updates a single user, that runs every 5 minutes or so.My problem is

I am writing a script to add a named Cron job that updates a single user, that runs every 5 minutes or so.

My problem is that the job runs for every user over and over again every second or so. Here is the code that I have placed inside my functions.php file.

This is my first foray into the WP Cron functionality with WordPress and would like to know if I set up the jobs correctly.

function so_custom_cron_schedule( $schedules ) {
    $schedules['every_5_minutes'] = array(
        'interval' => 300,
        'display'  => __( 'Every 5 minutes' ),
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'so_custom_cron_schedule' );

function update_social_user($user_id){
  $user = get_userdata($user_id);
  if(!$user){
    return;
  }
  var_error_log('running for '.$user_id);
}

function assign_cron(){
  $users = get_users([ 'role__in' => [ 'administrator', 'seller'] ]);
  $args = array(false);
  foreach($users as $user){
    $hook_name = 'update_fb_'.$user->ID;
    add_action($hook_name,'update_social_user');
    if(!wp_next_scheduled($hook_name,$args)){
      wp_schedule_event(time(),'every_5_minutes',$hook_name,array($user->ID));
    }else{
      var_error_log('Already set');
    }
  }
}

assign_cron();

I am writing a script to add a named Cron job that updates a single user, that runs every 5 minutes or so.

My problem is that the job runs for every user over and over again every second or so. Here is the code that I have placed inside my functions.php file.

This is my first foray into the WP Cron functionality with WordPress and would like to know if I set up the jobs correctly.

function so_custom_cron_schedule( $schedules ) {
    $schedules['every_5_minutes'] = array(
        'interval' => 300,
        'display'  => __( 'Every 5 minutes' ),
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'so_custom_cron_schedule' );

function update_social_user($user_id){
  $user = get_userdata($user_id);
  if(!$user){
    return;
  }
  var_error_log('running for '.$user_id);
}

function assign_cron(){
  $users = get_users([ 'role__in' => [ 'administrator', 'seller'] ]);
  $args = array(false);
  foreach($users as $user){
    $hook_name = 'update_fb_'.$user->ID;
    add_action($hook_name,'update_social_user');
    if(!wp_next_scheduled($hook_name,$args)){
      wp_schedule_event(time(),'every_5_minutes',$hook_name,array($user->ID));
    }else{
      var_error_log('Already set');
    }
  }
}

assign_cron();
Share Improve this question asked Feb 9, 2020 at 16:15 frank Collinsfrank Collins 534 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I usually define a IF state and use wp_clear_scheduled_hook function. See example:

if (get_option('atenasupervisor_scheduler') <> 'Stop') {
   echo 'Active ('. get_option('atenasupervisor_scheduler').')</strong></div>';
} else {
    wp_clear_scheduled_hook( 'atenasupervisor_my_email' );
    echo 'Not active ('. get_option('atenasupervisor_scheduler').')</strong></div>';
}
``

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744764484a4592373.html

相关推荐

  • WP Cron jobs loops infinitely

    I am writing a script to add a named Cron job that updates a single user, that runs every 5 minutes or so.My problem is

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信