第四次睡眠后,phpscript停止

时间:2018-05-21 10:59:05

标签: php mysql sleep

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

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);
    }

如果我没有在数据库中添加用户,或发送邮件,只有var_dump()$ user,我可以看到所有3500+用户都在php对象中正确创建(所以他们应该能够正确插入)。

问题是我想在我发送的每50封邮件之后的20到40秒之间添加随机睡眠。

所以我开始做一些测试,在注释掉插入和邮件代码之后,我开始运行脚本,注意到经过一些数量(完全不是50)之后,它只是停了一会儿,然后继续向我展示在我的if情况下底部的var_dumps,它可以在下面的屏幕截图中显示。 第一个屏幕截图显示代码停止了一点(请注意,我只是var_dumping东西,我没有在数据库中添加内容或发送电子邮件)。  first break

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

从这一点开始,脚本就完全停止了。我已经尝试了3次,每次都准确地停在200次。 这里发生了什么?

1 个答案:

答案 0 :(得分:3)

您很可能会达到默认的PHP限制。暂时删除PHP默认限制:

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

然后,重新运行脚本并检查输出。

可能有多种原因,但要知道确切的原因,请启用错误输出。

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

如果限制不是问题,错误会告诉你更多。

提示:使用日志仍然比向访问者输出错误更好,但我猜您是在计算机上对此进行测试。