Jump to content

Recommended Posts

Hello everyone,

 

 I'm working on a project that is within a custom wordpress site/app that is using too much memory. Its a fitness app that tracks staff and teams fitness points/scores. I'm 99% sure the cron job is the issue but not quite sure what's off about it causing this app to use so much memory. There are roughly 300 people using this app and they do at least one exercise per day and input it within their profile. Below is the code for the cron job. Is there anything wrong with this code that would cause this app to use so much memory? As you can see its been modified from every 5 mins to every 30 so it doesn't use so much. 

 

function my_cron_schedules($schedules){
    if(!isset($schedules["5min"])){
        $schedules["5min"] = array(
            'interval' => 5*60,
            'display' => __('Once every 5 minutes'));
    }
    if(!isset($schedules["30min"])){
        $schedules["30min"] = array(
            'interval' => 30*60,
            'display' => __('Once every 30 minutes'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
 
add_action('thirty_minute_event', 'update_team_avg_points');
 
function my_activation() {
if ( !wp_next_scheduled( 'thirty_minute_event' ) ) {
wp_schedule_event( current_time( 'timestamp' ), '30min', 'thirty_minute_event');
}
}
add_action('wp', 'my_activation');
 
function update_team_avg_points() {
    
    
    // THIS UPDATES TOTAL USER POINTS. 
    $users = get_users(array(
        'blog_id' => 1,
    ));  
    
    foreach($users as $user){
        $userPoints = get_all_points_for_user($user->ID);
    update_user_meta($user->ID, '_ecfit_user_points', $userPoints);
    }
    
    // THIS UPDATES TEAM AVG POINT VALUE.
$staff_teams = ecfit_get_teams( 'staff' );
    $apprentice_teams = ecfit_get_teams( '2k2/Contractor/Intern' );
    $family_teams = ecfit_get_teams( 'family' );
    
    if($staff_teams){
    foreach( $staff_teams as $team ){
       $team_points = $team->get_average_points();
       $teamId = $team->get_id();
       update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
    }
    if($apprentice_teams){
    foreach( $apprentice_teams as $team ){
       $team_points = $team->get_average_points();
       $teamId = $team->get_id();
       update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
    }
    if($family_teams){
    foreach( $family_teams as $team ){
       $team_points = $team->get_average_points();
       $teamId = $team->get_id();
       update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
    }
    
    
 
 
}
Link to comment
https://forums.phpfreaks.com/topic/302545-cron-job-taking-up-too-much-memory/
Share on other sites

Please surround your code in


tags when posting it to preserve the indentation and make it more readable.

 

Why do you believe memory usage is an issue? What sort of symptoms or problems are you seeing? You need to provide some specific details if you want help, not just a brief theory and code dump.

Please surround your code in


tags when posting it to preserve the indentation and make it more readable.

 

Why do you believe memory usage is an issue? What sort of symptoms or problems are you seeing? You need to provide some specific details if you want help, not just a brief theory and code dump.

I'm sorry I didn't see an area where I could place the code but I see the <> in the text input tools now. 

 

As for more specifics the server we have this app on also has other high traffic sites on it as well. The hosting company has told us that we're exceeding our memory limit and its due to this one app with the code I posted above. Its hosted at liquid web on an apache server. 

 

 

function my_cron_schedules($schedules){
    if(!isset($schedules["5min"])){
        $schedules["5min"] = array(
            'interval' => 5*60,
            'display' => __('Once every 5 minutes'));
    }
    if(!isset($schedules["30min"])){
        $schedules["30min"] = array(
            'interval' => 30*60,
            'display' => __('Once every 30 minutes'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
 
add_action('thirty_minute_event', 'update_team_avg_points');
 
function my_activation() {
if ( !wp_next_scheduled( 'thirty_minute_event' ) ) {
wp_schedule_event( current_time( 'timestamp' ), '30min', 'thirty_minute_event');
}
}
add_action('wp', 'my_activation');
 
function update_team_avg_points() {
    
    
    // THIS UPDATES TOTAL USER POINTS. 
    $users = get_users(array(
        'blog_id' => 1,
    ));  
    
    foreach($users as $user){
        $userPoints = get_all_points_for_user($user->ID);
    update_user_meta($user->ID, '_ecfit_user_points', $userPoints);
    }
    
    // THIS UPDATES TEAM AVG POINT VALUE.
$staff_teams = ecfit_get_teams( 'staff' );
    $apprentice_teams = ecfit_get_teams( '2k2/Contractor/Intern' );
    $family_teams = ecfit_get_teams( 'family' );
    
    if($staff_teams){
    foreach( $staff_teams as $team ){
       $team_points = $team->get_average_points();
       $teamId = $team->get_id();
       update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
    }
    if($apprentice_teams){
    foreach( $apprentice_teams as $team ){
       $team_points = $team->get_average_points();
       $teamId = $team->get_id();
       update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
    }
    if($family_teams){
    foreach( $family_teams as $team ){
       $team_points = $team->get_average_points();
       $teamId = $team->get_id();
       update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
    }
    
    
 
 
}
 
I noticed that the edit post feature is missing for the original post above so I would just edit the code there but can't since I must not have permission set to edit posts. 
Edited by xoibsurferx

I have no knowledge of Wordpress or whatever plugins you may be using so the best I can really offer is this:

function my_cron_schedules($schedules){
    if(!isset($schedules["5min"])){
        $schedules["5min"] = array(
            'interval' => 5*60,
            'display' => __('Once every 5 minutes'));
    }
    if(!isset($schedules["30min"])){
        $schedules["30min"] = array(
            'interval' => 30*60,
            'display' => __('Once every 30 minutes'));
    }
    return $schedules;
}

add_filter('cron_schedules','my_cron_schedules');
add_action('thirty_minute_event', 'update_avg_points');
 
function my_activation() {
    if ( !wp_next_scheduled( 'thirty_minute_event' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), '30min', 'thirty_minute_event');
    }
}
add_action('wp', 'my_activation');

function update_team_avg_points($teamName){
    foreach (ecfit_get_teams($teamName) as $team){
        $team_points = $team->get_average_points();
        $teamId = $team->get_id();
        update_post_meta($teamId, '_ecfit_team_avg', $team_points);
    }
}

function update_user_points(){
    $users = get_users(array(
        'blog_id' => 1,
    ));  
    
    foreach($users as $user){
        $userPoints = get_all_points_for_user($user->ID);
        update_user_meta($user->ID, '_ecfit_user_points', $userPoints);
    }
}

function update_avg_points() {
    // THIS UPDATES TOTAL USER POINTS. 
    update_user_points();

    // THIS UPDATES TEAM AVG POINT VALUE.
    update_team_avg_points('staff');
    update_team_avg_points('2k2/Contractor/Intern');
    update_team_avg_points('family');
}
All your team updates use the same basic code so I split that code into a separate function that just takes the team name. I also split the user update into it's own function. Separating them out in this way allows the variables to go out of scope sooner allowing PHP to reclaim their memory and hopefully use less. For example the script previously required enough memory to hold all three team lists at once where as now it should only need enough for one at a time.

 

You may be able to do some of this by just issuing some direct UPDATE queries to the database rather than going through the WordPress API. You'll need to spend some time looking at the table structure and see what can be done.

Thank you for helping me! That makes sense and I can see how that would help. Newbie question but as you can see there's that IF statement at the beginning for 5min as well as the 30minute one under it. From my understand and I could be wrong but that 5min one is still running as well as the 30min one. Would that also be an issue?

Based on a quick scan of the WordPress documentation having both may be unnecessary but it shouldn't cause any issues. Having the 5-minute one defined might cause WordPress to check for work every 5 minutes but since that script only specifies work on the 30-minute schedule(wp_schedule_event) nothing would be found (unless some other plugin has work to do) and the job would simply exit.

Based on a quick scan of the WordPress documentation having both may be unnecessary but it shouldn't cause any issues. Having the 5-minute one defined might cause WordPress to check for work every 5 minutes but since that script only specifies work on the 30-minute schedule(wp_schedule_event) nothing would be found (unless some other plugin has work to do) and the job would simply exit.

Thanks for checking this out for me! That definitely makes sense. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.