phpscript在第四次睡眠后停止

arknldoa  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(403)

我有一个csv文件包含大约3500个用户的数据。我想将这些用户导入我的数据库,并向他们发送一封电子邮件,说明他们已注册。我有以下代码:

public function importUsers()
    {
        $this->load->model('perk/engagement_model');
        $this->load->model('user/users_model');
        $this->load->model('acl/aclUserRoles_model');
        $allengagements = $this->engagement_model->getAll();

        $filename = base_url() . 'assets/overdracht_users.csv';
        $file = fopen($filename, "r");

        $count = 0;
        $totalImported = 0;
        $importFails = array();
        $mailFails = array();
        while (($mappedData = fgetcsv($file, 10000, ";")) !== FALSE)
        {
            $count++;
            //Skip first line because it is the header
            if ($count > 1) {
                if (!empty($mappedData[0])) {
                    $email = $mappedData[0];

                    $user = $this->users_model->getByEmail($email);
                    if (!$user) {
                        $user = new stdClass();
                        $user->email = $mappedData[0];
                        $user->first_name = $mappedData[1];
                        $user->family_name = $mappedData[2];
                        $user->address_line1 = $mappedData[3];
                        $user->address_postal_code = $mappedData[4];
                        $user->address_city = $mappedData[5];
                        $user->address_country = 'BE';
                        $user->volunteer_location = $mappedData[5];
                        $user->volunteer_location_max_distance = 50;
                        $user->phone = $mappedData[6];
                        if (!empty($mappedData[7])) {
                            $user->birthdate = $mappedData[7] . "-01-01 00:00:00";
                        } else {
                            $user->birthdate = null;
                        }
                        foreach ($allengagements as $eng) {
                            if ($eng->description == $mappedData[8]) {
                                $engagement = $eng->engagement_id;
                            }
                        }
                        $user->engagement = $engagement;
                        if (!empty($mappedData[9])) {
                            $date_created = str_replace('/', '-', $mappedData[9]);
                            $date_created = date('Y-m-d H:i:s', strtotime($date_created));
                        } else {
                            $date_created = date('Y-m-d H:i:s');
                        }
                        $user->created_at = $date_created;
                        if (!empty($mappedData[10])) {
                            $date_login = str_replace('/', '-', $mappedData[10]);
                            $date_login = date('Y-m-d H:i:s', strtotime($date_login));
                        } else {
                            $date_login = null;
                        }
                        $user->last_login = $date_login;
                        $user->auth_level = 1;
                        $user->is_profile_public = 1;
                        $user->is_account_active = 1;

                        $combinedname = $mappedData[1] . $mappedData[2];
                        $username = str_replace(' ', '', $combinedname);
                        if (!$this->users_model->isUsernameExists($username)) {
                            $uniqueUsername = $username;
                        } else {
                            $counter = 1;
                            while ($this->users_model->isUsernameExists($username . $counter)) {
                                $counter++;
                            }
                            $uniqueUsername = $username . $counter;
                        }

                        $user->username = $uniqueUsername;

                        $userid = $this->users_model->add($user);
                        if (!empty($userid)) {
                            $totalImported++;
                            //Add the user in the volunteer group in ACL
                            $aclData = [
                                'userID'      => $userid,
                                'roleID'      => 1,
                                'addDate'     => date('Y-m-d H:i:s')
                            ];
                            $this->aclUserRoles_model->add($aclData);

                            //Registration mail to volunteer
                            $mail_data['name'] = $user->first_name . ' ' . $user->family_name;
                            $mail_data['username'] = $user->username;

                            $this->email->from(GENERAL_MAIL, 'Test');
                            $this->email->to($user->email);
                            //$this->email->bcc(GENERAL_MAIL);
                            $this->email->subject('Test');
                            $message = $this->load->view('mail/register/registration',$mail_data,TRUE);
                            $this->email->message($message);

                            $mailsent = $this->email->send();
                            if (!$mailsent) {
                                array_push($mailFails, $mappedData);
                            }
                        } else {
                            array_push($importFails, $mappedData);
                        }

                        if ($count % 50 == 0) {
                            var_dump("count is " . $count);
                            var_dump("we are sleeping");
                            $min=20;
                            $max=40;
                            $randSleep = rand($min,$max);
                            sleep($randSleep);
                            var_dump("end of sleep (which is " . $randSleep . "seconds long)");
                        }
                        var_dump($user);
                    } else {
                        array_push($importFails, $mappedData);
                    }
                }
            }
        }
        var_dump("Totale aantal rijen in het bestand (met header) : " . $count);
        var_dump("Totale aantal geimporteerd in de database : " . $totalImported);
        var_dump("Totale aantal gefaalde imports in de database : " . count($importFails));
        var_dump("Deze zijn gefailed : ");
        var_dump($importFails);
    }

如果我不在数据库中添加用户,也不发送邮件,只在$user中添加var_dump(),我可以看到所有3500多个用户都被正确地创建在php对象中(因此应该能够正确地插入)。
问题是,我想加入一个随机睡眠,在每发送50封邮件后的20到40秒之间。
因此,我开始做一些测试,在注解了insert和mail代码之后,我开始运行脚本,注意到在一些量(不是50)之后,它只会停止一点,然后继续并向我显示底部if case中的var\u转储,它可以在下面的屏幕截图中显示。第一个屏幕截图显示代码停止了一段时间(注意,我只是在转储一些东西,我还没有在数据库中添加一些东西或发送电子邮件)。

此屏幕截图显示脚本达到200后发生的情况:

从这一点开始,脚本就完全停止了。我试了三次,每次都停在200。这里发生了什么??

wlzqhblo

wlzqhblo1#

很可能您达到了默认的php限制。临时删除php默认限制:

ini_set('memory_limit',-1);
set_time_limit(0);

然后,重新运行脚本并检查输出。
可能有多种原因,但要知道确切的原因,请使用启用错误输出。

ini_set('display_errors',1);
ini_set('display_startup_errors',1);

如果限制不是问题所在,错误会告诉你更多。
提示:使用日志仍然比向访问者输出错误要好,但我猜你是在你的计算机上测试这个。

相关问题