Laravel上传解析生物识别文件时服务器无响应其他请求问题咨询
生物识别模块上传流程阻塞问题咨询
项目当前进展
正在为自研系统搭建生物识别解析模块,已完成基础能力开发:支持从生物识别设备读取原始数据并存入数据库。
遇到的核心问题
只要系统内有用户上传生物识别原始文件,服务器在整个上传、解析流程期间都无法响应其他页面的访问或请求,希望了解这类场景的通用优化处理原则。
现有实现说明
设备导出文件格式
生物识别设备导出的文件每行对应1条独立识别记录,单文件共包含1700余条数据,文件内容格式如下:
129 2019-06-23 18:13:10 1 0 0 0 98 2019-06-23 18:51:46 1 0 0 0 101 2019-06-23 19:07:29 1 1 0 0 103 2019-06-23 19:07:37 1 1 0 0 85 2019-06-23 21:14:01 1 1 0 0 132 2019-06-23 23:48:54 1 1 0 0 203 2019-06-24 03:45:44 1 0 0 0 138 2019-06-24 04:10:06 1 0 0 0 204 2019-06-24 05:12:57 1 0 0 0 157 2019-06-24 05:58:07 1 0 0 0
现有代码逻辑
目前在控制器层实现逐行校验、解析规则,将有效解析结果存入数据库,同时创建生物识别批次记录用于追踪上传数据,对应的控制器核心代码如下:
public function store(Request $request) { if(Auth::check()){ $repeatedRecords =0; // 校验文件是否接收成功 set_time_limit(360); if($request->hasFile('BiometricDataFile')){ $biometricDataLogFile = file($request->BiometricDataFile); foreach($biometricDataLogFile as $logFile){ $BiometricDBRecord = new Biometric; $biometricRecord = explode("\t", $logFile); $BiometricDBRecord->employee_id = $biometricRecord[0]; $BiometricDBRecord->location = $request->location; $BiometricDBRecord->logDate = Carbon::parse($biometricRecord[1]); $date = explode(' ', $biometricRecord[1]); $BiometricDBRecord->bioDate = $date[0]; $BiometricDBRecord->bioTime = $date[1]; $BiometricDBRecord->uploaded_by = Auth::user()->employee_id; if(Biometric::where('logDate','=', $biometricRecord[1])->count() <= 0){ $BiometricDBRecord->save(); }else{ $repeatedRecords++; } } $newBatch = new BiometricBatch(); // 准备返回视图的相关数据 $timeNow = Carbon::now('GMT+8')->toDateTimeString(); $thirtyMinEarlier = Carbon::now('GMT+8')->subHours(12)->toDateTimeString(); $biometricDataSaved = Biometric::whereBetween('created_at', [$thirtyMinEarlier, $timeNow])->where('uploaded_by', Auth::user()->employee_id)->get(); $biometricDataSaved->sortBy('logDate'); $sortedFirst = $biometricDataSaved->first(); $sortedLast = $biometricDataSaved->last(); $newBatch->employee_id = Auth::user()->employee->id; $newBatch->batch_upload_date = Carbon::now(); $newBatch->batch_start_date = $sortedFirst->logDate; $newBatch->batch_end_date = $sortedLast->logDate; $newBatch->isProcessed = false; $newBatch->save(); }else{ // 未接收到文件的情况 return "<h1> File not found </h1>"; } $activityLog = new ActivityLog(); $activityLog->employee_id = Auth::user()->employee_id; $activityLog->action = 'Has uploaded biometric data'; $activityLog->save(); return view('pages.employeeBiometric')->with('savedLogs', $biometricDataSaved, 'duplicates', $repeatedRecords); }else{ return view('errors.401'); } }
优化方向诉求
初步判断可以结合JavaScript或Ajax优化现有流程,但不清楚具体的入手方向和实现方式,希望能获得成熟的优化方案,从根源上避免服务器被长时间阻塞的问题。
内容的提问来源于stack exchange,提问作者Daniel Tejano
相关产品推荐
相关产品推荐

