How to do batch by batch processing for all users?

Question

I created a dashboard for users where they can see their daily Instagram data. So I need to run a cron job daily to iterate each registered users and get their instagram data. This is my code:

function ps_update_users_data() {
global $wpdb;

$table_name = $wpdb->prefix . PS_IG_TABLE;

// Check if database table exists
if (!$wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
    return;
}

// get all subscriber role users
$users = get_users(array('role' => 'subscriber'));

$curl = curl_init();

foreach ($users as $user) {
    $ig_username = get_user_meta($user->ID, 'instagram_username', true);
    sleep(3);

    if (empty($ig_username)) return;

    // $instagram = get_user_ig_data($ig_username);

    $instagram = false;

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://api-here.com?username=" . $ig_username,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => [
            "x-rapidapi-host: api.com",
            "x-rapidapi-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        ],
    ]);

    $response = curl_exec($curl);
    $err = curl_error($curl);

    if (!$err) {
        $instagram = json_decode($response);
    }


    $followers = $instagram->edge_followed_by->count;
    $following = $instagram->edge_follow->count;
    $media_uploads = $instagram->edge_owner_to_timeline_media->count;

    $followers_gained = 0;
    $following_gained = 0;

    // Get previous data
    $previous_data = $wpdb->get_row("SELECT followers, following FROM {$table_name} WHERE user_id = {$user->ID} ORDER BY date DESC");
    if (!empty($previous_data)) {
        $followers_gained = $followers - $previous_data->followers;
        $following_gained = $following - $previous_data->following;
    }

    $wpdb->insert($table_name, array(
        'user_id' => $user->ID,
        'followers' => $followers,
        'followers_gained' => $followers_gained,
        'following' => $following,
        'following_gained' => $following_gained,
        'media_uploads' => $media_uploads,
    ), '%d');
}

curl_close($curl);
}
add_action('ps_update_users_data_daily', 'ps_update_users_data');

My problem is that it stops randomly at index 13-16. I think this is a timeout issue with the cron job. How can I process this by batch so I won’t get a timeout?

0
marccaps 8 months 2020-12-06T23:10:21-05:00 0 Answers 10 views 0

Leave an answer

Browse
Browse