Laravel应用程序冻结不';我再也没有反应了
我有一个laravel应用程序,它使用while循环逐行处理一个txt文件(6000行),并使用各种帮助程序来处理每个字符串。在操作结束时,它为每一行存储一条记录(以db为单位)。一切正常,应用程序在一分钟内处理整个文件并返回指定的视图。问题是,应用程序比冻结(不确定这是否是合适的术语)更频繁地运行,浏览器反复要求等待或关闭应用程序。 我在laravel服务器上也注意到了这条消息,如果它有帮助的话Laravel应用程序冻结不';我再也没有反应了,laravel,while-loop,freeze,laravel-livewire,line-by-line,Laravel,While Loop,Freeze,Laravel Livewire,Line By Line,我有一个laravel应用程序,它使用while循环逐行处理一个txt文件(6000行),并使用各种帮助程序来处理每个字符串。在操作结束时,它为每一行存储一条记录(以db为单位)。一切正常,应用程序在一分钟内处理整个文件并返回指定的视图。问题是,应用程序比冻结(不确定这是否是合适的术语)更频繁地运行,浏览器反复要求等待或关闭应用程序。 我在laravel服务器上也注意到了这条消息,如果它有帮助的话 [Wed Apr 21 10:28:26 2021] 127.0.0.1:50897 Closed
[Wed Apr 21 10:28:26 2021] 127.0.0.1:50897 Closed without sending a request; it was probably just an unused speculative preconnection
谢谢任何能帮助我的人
这是一个过程:
$log = new UserActivityHelper;
$log->create(Config::get('constants.user_activities.FTP_FILE_PROCESSED_START'), 'test');
$file = fopen(Storage::path("1-2021.txt"), 'r');
while(!feof($file))
{
$line = fgets($file);
//call the method that validate the string
$string = new ValidateStringHelper($line); //FIRST HELPER
$string->validate();
//check the result of the validation
if($string->validated == false){
dd("out");
} elseif ($string->empty == true){
continue;
}else{
//save the validated array of substrings as variable
$my_arr = $string->validatedChars;
//check if the province exists or create it
$province = new ProvinceExistsHelper($my_arr['district']); //SECOND HELPER
$province->check_if_exists_or_create();
if($province->new_province == true) {
$log = new UserActivityHelper;
$log->create(Config::get('constants.ftp_process.FTP_PROVINCE_CREATED'), "Creata nuova provincia con id {$province->district_id}");
self::message('yellow', "Attenzione. Nell'elaborazione sono state create nuove provincie, controllare le impostazioni.");
}
//manipolation of name and lastname
$name = ucwords(strtolower($my_arr['name_lastname']));
//check if the person already exists or create it (passing fiscal code, name and district_id)
$person = new PersonExistsHelper($my_arr['fiscal_code'], $name, $province->district_id); //THIRD HELPER
$person->check_if_exists_or_create();
$newMovement = new Movement;
$newMovement->person_id = $person->person_id;
if(array_key_exists('level', $my_arr)){
$newMovement->level = $my_arr['level'];
}
...
try {
$newMovement->save();
} catch (\Throwable $exception) {
report($exception);
return;
}
}
}
fclose($file);
$log = new UserActivityHelper;
$log->create(Config::get('constants.user_activities.FTP_FILE_PROCESSED_END'), 'test');
return view('ftp.test');
求助电话:
class ValidateStringHelper
{
public $line;
public $empty = false;
public $validated = false;
public $validationErrors = array();
public $validatedChars = array();
public function __construct($line){
$this->line = $line;
}
public function validate(){
if ($this->line!="")
{
if(strlen($this->line) >= 186)
{
//substrings
$district = trim(substr($this->line, 0, 2));
$trade_union_tax_code = trim(substr($this->line, 2, 3));
...
//validation
//check if validated
$this->validatedChars['district'] = $district;
$this->validatedChars['trade_union_tax_code'] = $trade_union_tax_code;
...
//filter all non empty string values and not all zero strings
$filter_arr = array_filter($this->validatedChars, fn($value) => $value !== '' && preg_filter('/^(?!0*$).*$/', '$0', $value));
$this->validatedChars = $filter_arr;
$this->validated = true;
return $this->validated;
//return $this->validatedChars;
//eturn $this->validated;
} else {
$this->validationErrors[] = "string_length";
return $this->validated;
}
} else {
$this->empty = true;
return $this->empty;
}
}
class PersonExistsHelper
{
public $name_lastname;
public $fiscal_code;
public $district_id;
public $new_person = false;
public $person_id;
public function __construct(string $fiscal_code, string $name_lastname, string $district_id){
$this->fiscal_code = $fiscal_code;
$this->name_lastname = $name_lastname;
$this->district_id = $district_id;
}
public function check_if_exists_or_create()
{
$person = Person::where('fiscal_code', '=', $this->fiscal_code)->first();
if($person == NULL)
{
$this->new_person = true;
$newPerson = new Person;
$newPerson->name_lastname = $this->name_lastname;
$newPerson->district_id = $this->district_id;
$newPerson->fiscal_code = $this->fiscal_code;
$newPerson->created_by = Config::get('constants.people_creation.FTP_PROCESS');
$newPerson->save();
$this->person_id = $newPerson->id;
return $this->person_id;
} else{
$this->person_id = $person->id;
return $this->person_id;
}
}
}
class ProvinceExistsHelper
{
public $district_id;
public $district_code;
public $new_province = false;
public function __construct($district_code){
$this->district_code = $district_code;
}
public function check_if_exists_or_create()
{
//check if the province is in the Provinces table
$province = Province::where('code', '=', $this->district_code)->first();
if(!isset($province))
{
$this->new_province = true;
$newProvince = new Province;
$newProvince->name = $this->district_code;
$newProvince->code = $this->district_code;
$newProvince->save();
$this->district_id = $newProvince->id;
return $this->district_id;
} else {
//if yes, just return the id of the province
$this->district_id = $province->id;
return $this->district_id;
}
}
}
“一切正常”,除了您的代码处理文件需要一分钟以上,整个应用程序运行期间,php工作人员仍在处理文件。或者至少你的代码还在运行。要么增加php工作人员的数量,要么简单地在后台使用job/queue来处理文件,以便应用程序保持不变online@N69S好的,我将尝试队列作业,看看它是否有效,谢谢!我同意@N69S,当你不得不做一些像这样的繁重任务时,试着总是使用
作业
。我可以与您分享的一个技巧是使用Guard子句
,并抛弃绝对可怕的if(true){main code}else{exit}
。如果您开始使用它们,您将拥有更好的代码!阅读更多内容和@N69S此外,我想说的是,记录的创建非常完美(这就是我所说的“一切正常”)但进程一直在运行,不知道是什么原因,可能是脚本中的错误。话虽如此,我已经实现了队列,但现在我需要配置supervisor来自动实现它。在这样做之前,我想让您了解这是否是绝对必要的,谢谢。队列代理将一直运行,直到中断/重新启动。它将自动处理动态的新工作。如果队列代理未运行(服务器重新启动。错误退出,…),则监控器将启动队列代理,这就是监控器被后台监控的原因