0

Been a long time since I've been here but I'm stumped on this one. Unclear if this is a codeignter thing or not but I have this function that pulls a lot of data and takes longer then 60seconds. In dev it allows me to go over 60 seconds but on prod it's a hard stop at 60 seconds.

I've tried changing the execution time in the php.ini... nothing. ini_set('max_execution_time', '300'); woks on some functions but not on my function... I'm lost here and my work is coming down on me. What is the deal?

Here's the function, that ini_set won't work on. Even here, set to zero still times out at 60seconds on prod:

public function cron_clever_import()
    {   ini_set('max_execution_time', 0);

        $this->load->model('Clever_model', 'clevermodel');
        $this->load->model('Licenses_model', 'licenses');
        $this->load->model('Users_model', 'users');
        $this->load->model('Groups_model', 'groups');
        $this->load->model('Userslicenses_model', 'userLicenses');
        $this->load->library('Clever');

        $adny_group_schools = array();
        $adny_group_distric = array();
        $special_da_emails = array();
                $getChildHierarchy = $this->groups->getChildHierarchy(7699);
                foreach($getChildHierarchy as $child_id) {
                    $adny_group = $this->groups->get($child_id);
                    if($adny_group->group_type == 'S') {
                        array_push($adny_group_schools, $child_id);
                    }
                    if($adny_group->group_type == 'D'){
                        array_push($adny_group_distric, $child_id);
                    }
                }
                $imploded_adny_groups = implode(",",$adny_group_distric);
                $current_da_adny_users = $this->users->getActiveDaAdny($imploded_adny_groups);
                foreach($current_da_adny_users as $current_da_adny_user)
                {
                    array_push($special_da_emails, $current_da_adny_user->email);
                }

      
        foreach($adny_group_schools as $groupID) {
            //arrays for checking our system against clever.  
            $current_active_emails = array();
            $current_clever_emails = array();
            $current_in_system = $this->users->getByGroupID($groupID);
            //fill array with current active users in otis system
            foreach($current_in_system as $current) {
                if($current->is_active == 1 && !in_array($current->email, $special_da_emails)) {
                    array_push($current_active_emails, $current->email);
                }
            }

            //get the entry id and oauth toke to gain access to clever, then grab the data and license info

                $is_in_mapping = $this->clevermodel->getCurrentMapByGroupId($groupID, 7699);
                if (!empty($is_in_mapping)) {

                    $clever_teachers = $this->clever->clever_get_school_users($is_in_mapping->clever_school_key, $is_in_mapping->district_oauthtoken, 'teacher');
                    $clever_staff = $this->clever->clever_get_school_users($is_in_mapping->clever_school_key, $is_in_mapping->district_oauthtoken, 'staff');
                    $teachers_decoded = json_decode($clever_teachers);
                    $staff_decoded = json_decode($clever_staff);
                }
                else {
                    $group_settings = $this->clevermodel->getSchoolSettings($groupID);
                    $oauth_token = $this->clevermodel->grabOauthToken($group_settings->district_oauthtoken_id);
                    //calls for teacher and staff(or school admin)
                    $clever_teachers = $this->clever->clever_get_school_users($group_settings->entry_id, $oauth_token, 'teacher');
                    $clever_staff = $this->clever->clever_get_school_users($group_settings->entry_id, $oauth_token, 'staff');
                    $groupMappingBySchoolId = $this->clevermodel->getCurrentMapBySchoolId($group_settings->entry_id);
                    if($groupMappingBySchoolId->otis_group_id < 1) {
                        $teachers_decoded = array();
                        $staff_decoded = array();
                    }
                    else {
                        $teachers_decoded = json_decode($clever_teachers);
                        $staff_decoded = json_decode($clever_staff);
                    }
                }
           

            //fill data into coherent array I can work with
            foreach ($teachers_decoded->data as $teacher_data) {

                if (is_null($teacher_data->data->email) || in_array($teacher_data->data->email, $special_da_emails)) {
                    //do not add to import users list
                } else {

                    $import_users[$teacher_data->data->id] = array(
                        'first_name' => $teacher_data->data->name->first,
                        'last_name' => $teacher_data->data->name->last,
                        'email' => $teacher_data->data->email,
                        'role_id' => '2'
                    );

                    //push array of clever emails so I can check aginst current in otis
                    array_push($current_clever_emails, $teacher_data->data->email);
                }
            }
            foreach ($staff_decoded->data as $staff_data) {

                if (is_null($staff_data->data->email) || in_array($staff_data->data->email, $special_da_emails)) {
                    //do not add to import users list
                } else {
                    $import_users[$staff_data->data->id] = array(
                        'first_name' => $staff_data->data->name->first,
                        'last_name' => $staff_data->data->name->last,
                        'email' => $staff_data->data->email,
                        'role_id' => '3'
                    );

                    array_push($current_clever_emails, $staff_data->data->email);
                }
            }
            //if user active in our system but not existing in clever, make them inactive in otis
            foreach($current_active_emails as $current_otis) {
                if(!in_array($current_otis, $current_clever_emails)) {
                    $this->users->makeUserInactiveClever($current_otis);
                }

            }


            //do the actual import of users.  If they exist and are not active make them active, if they do not exists add them to group and apply license
            foreach ($import_users as $clever_users) {
                $user_exists = $this->users->getByEmail($clever_users['email']);
                if (!empty($user_exists)) {
                    if ($user_exists->is_active == 0 || $user_exists->is_deleted == 1 || $user_exists->is_locked == 1) {
                        $this->users->updateInactiveCleverUser($user_exists->id);
                    }
                } else {
                    $cleverData = array(
                        'first_name' => $clever_users['first_name'],
                        'last_name' => $clever_users['last_name'],
                        'email' => $clever_users['email'],
                        'role_id' => $clever_users['role_id'],
                        'password' => 'hidden',
                        'confirm_password' => 'hidden',
                        'group_id' => $groupID
                    );
                    $saved = $this->save($cleverData);
                    $new_user = $this->users->getByEmail($clever_users['email']);
                    if (!empty($new_user)) {
                        $current_group_license = $this->licenses->getByGroupIDAndTypeForClever(7699, 'opd');
                        
                        $current_available_licenses = ($current_group_license[0]->license_count - $current_group_license[0]->total_users);
                        if($current_available_licenses > 0)
                        {
                            $add_license = $this->users->addLicenceCleverImport($saved, $current_group_license[0]->id);
                            $this->userLicenses->add(array(
                                'license_id' => $current_group_license[0]->id,
                                'user_id' => $saved,
                                'start_date' => date('Y-m-d H:i:s'),
                                'end_date' => $current_group_license[0]->expiration_date,
                                'expiration_date' => $current_group_license[0]->expiration_date,
                                'is_active' => 1,
                                'create_date' => date('Y-m-d H:i:s')
                            ));
                        }
                    }
                }
            }  
Dylano236
  • 305
  • 3
  • 15
  • well, without knowing your models, it is not possible to discover your bottle-neck at a 1st glance. I'd start debugging controller function / model function until you reach the part hindering execution time. Then try to optimize the queries involved, by adding indexes, changing the sequence how joins are executed, even consider changing column types into indexable ones (like text into varchar) – Vickel May 05 '22 at 21:10
  • Well all my model functions are pretty simple calls in this function. What's slowing it down is the api call to clever. That is taking the time sorting through all that data. Maybe I could refactor the code a bit better that's fair but my issue is the server timing out at 60 seconds and ini_set or set_time_limit seemingly doing nothing – Dylano236 May 05 '22 at 21:14
  • but have you set php's [set_time_limit()](https://www.php.net/manual/en/function.set-time-limit.php) to `set_time_limit(0); // set no limit.` ? – Vickel May 05 '22 at 21:28
  • I tried that yes. That also did not work. I also tried to change max_execution_time in php.ini file. I think it may be an Apache issue at this point. I don't want to mess with altering the Apache server while my team is out but tomorrow I'm going to try to set up that tomorrow morning. – Dylano236 May 05 '22 at 21:45
  • Check this if you are using mod_fcgi. https://serverfault.com/a/348738/595157 – Mohammad Salehi May 07 '22 at 06:23

0 Answers0