
private string $cachedProgressPercentKey = 'job_status:progress_percent'; // 进度百分比的缓存键
private int $cachedProgressPercentTtl = 24 * 3600; // 进度百分比的缓存过期时间
public const EXPORT_EXCEL_WRITE_PROGRESS_PERCENT = 99; // 导出 Excel 时写入文件所占用的百分比
private int $exportExcelMaxCount = 100000; // 限制最大导出行数为 100000
private int $exportExcelChunkCount = 1000; // 导出时分块数量为 1000
// 更新进度百分比
$orderShippingLogCount = min($orderShippingLogQueryBuilder->count(), $this->exportExcelMaxCount);
$chunkCount = (int)ceil($orderShippingLogCount / $this->exportExcelChunkCount); // 上舍入
$i = 0;
$uid = $jobStatus->uid;
Log::info(
'$orderShippingLog',
[
'$orderShippingLogCount' => $orderShippingLogCount,
'$chunkCount' => $chunkCount,
]
);
$orderShippingLogQueryBuilder->chunk($this->exportExcelChunkCount, function ($orderShippingLogs) use ($params, $excel, $columns, $chunkCount, $uid, &$i) {
// 限制最大导出行数为 100000
if ($i >= $chunkCount) {
return false;
}
$data = [];
foreach ($orderShippingLogs as $rowKey => $orderShippingLog) {
$data[] = static::toResource($orderShippingLog, $params['timezone']);
}
$excel->writeRows($data);
// 更新进度百分比
$i++;
$progressPercent = ($i / $chunkCount) * JobStatusService::EXPORT_EXCEL_WRITE_PROGRESS_PERCENT;
app(JobStatusService::class)->setProgressPercent($uid, $progressPercent);
Log::info(
'$orderShippingLogChunk',
[
'$i' => $i,
'$chunkCount' => $chunkCount,
'$progressPercent' => $progressPercent
]
);
return true;
});
// 更新进度百分比
$jobStatusService->setProgressPercent($this->jobStatus->uid, 100);
/**
* 设置进度百分比
* @param string $uid
* @param float $progressPercent
* @return void
*/
public function setProgressPercent(string $uid, float $progressPercent): void
{
$cachedKey = sprintf($this->cachedProgressPercentKey . ':%s', $uid);
Cache::store('redis')->put($cachedKey, round($progressPercent, 2), $this->cachedProgressPercentTtl);
}
/**
* 获取进度百分比
* @param string $uid
* @return float
* @throws InvalidArgumentException
*/
private function getProgressPercent(string $uid): float
{
$cachedKey = sprintf($this->cachedProgressPercentKey . ':%s', $uid);
$cachedProgressPercent = Cache::store('redis')->get($cachedKey);
if (!$cachedProgressPercent) {
return 0;
}
return (float)$cachedProgressPercent;
}
[2024-05-10 07:33:35] local.INFO: $orderShippingLog {"$orderShippingLogCount":4696,"$chunkCount":5}
[2024-05-10 07:33:37] local.INFO: $orderShippingLogChunk {"$i":1,"$chunkCount":5,"$progressPercent":19.8}
[2024-05-10 07:33:40] local.INFO: $orderShippingLogChunk {"$i":2,"$chunkCount":5,"$progressPercent":39.6}
[2024-05-10 07:33:41] local.INFO: $orderShippingLogChunk {"$i":3,"$chunkCount":5,"$progressPercent":59.4}
[2024-05-10 07:33:42] local.INFO: $orderShippingLogChunk {"$i":4,"$chunkCount":5,"$progressPercent":79.2}
[2024-05-10 07:33:43] local.INFO: $orderShippingLogChunk {"$i":5,"$chunkCount":5,"$progressPercent":99}
{
"label": "order",
"type": "export",
"status": "pending",
"file_path": "",
"progress_percent": 59.4
}
需要长期技术维护或远程问题排查?
我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。
如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:
- ✅ PHP / Laravel / Yii2 老项目无人维护
- ✅ Go / Gin 后端接口需要排查或优化
- ✅ WordPress 网站访问慢、报错或插件冲突
- ✅ Nginx / MySQL / Redis / Linux 服务器异常
- ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
- ✅ 需要长期远程技术支持或兼职维护
更多介绍请查看:关于我 & 合作
微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

发表回复