Skip to content

Commit 1ea5cbf

Browse files
committed
fix 失败任务的条件
1 parent eb22b2e commit 1ea5cbf

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

laravel/app/Http/Repository/ApiRepository.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use App\Models\ApiExcel;
1313
use App\Models\ApiExcelLogs;
14+
use Illuminate\Support\Facades\DB;
1415

1516
class ApiRepository
1617
{
@@ -70,14 +71,30 @@ public function handleAutoDelete()
7071
public function autoFailed()
7172
{
7273
// 查询数据库已完成的任务,判断过期条件
73-
$excels = ApiExcel::where('state', 1)->get(['id', 'auto_delete', 'created_at', 'updated_at']);
74+
// $excels = ApiExcel::where('state', 1)->get(['id', 'auto_delete', 'created_at', 'updated_at']);
75+
76+
// New: 2020-05-31 失败任务的条件
77+
// 1. 完成率 96% -- 5 秒不再增加
78+
// 2. 完成率 50% -- 1 分钟不再增加
79+
// 3. 完成率 10% -- 5 分钟不再增加
80+
// 4. 完成率 1% -- 10 分钟不再增加
81+
$excels = DB::connection()->select('SELECT ae.id,ae.api_param_id,ae.state,ae.total_excel,ael.api_excel_id,ael.sort_index,ael.created_at FROM `boss_api_excel` ae LEFT JOIN boss_api_excel_logs ael ON ae.id=ael.api_excel_id AND ael.id=(SELECT c.id FROM boss_api_excel_logs c WHERE ael.api_excel_id=c.api_excel_id ORDER BY sort_index DESC LIMIT 1)
82+
WHERE ae.state=1 AND ae.`deleted_at` IS NULL ');
7483

7584
foreach ($excels as $excel) {
7685
// 开启任务后 10 分钟未查询出结果=》失败
77-
if ($excel['auto_delete'] > 0 && strtotime($excel['updated_at']) + 600 < time()) {
78-
ApiExcel::where('id', $excel['id'])->update(['state' => 5]);
86+
// if ($excel['auto_delete'] > 0 && strtotime($excel['updated_at']) + 600 < time()) {
87+
// ApiExcel::where('id', $excel['id'])->update(['state' => 5]);
88+
// }
89+
if (!$excel->sort_index || !$excel->created_at) {
90+
continue;
91+
}
92+
// 记录完成率
93+
$finish = ($excel->total_excel / ($excel->sort_index + 1)) * 100;
94+
$finish = sprintf("%.2f", $finish);
95+
if (($finish > '96' && strtotime($excel->created_at) + 5 < time()) || ($finish > '50' && strtotime($excel->created_at) + 60 < time()) || ($finish > '10' && strtotime($excel->created_at) + 300 < time()) || ($finish > '1' && strtotime($excel->created_at) + 600 < time())) {
96+
ApiExcel::where('id', $excel->id)->update(['state' => 5]);
7997
}
80-
8198
}
8299
}
83100

0 commit comments

Comments
 (0)