From b9118ae8f62c8d9966628d54ab1d822597d03ae7 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Mon, 31 Jul 2023 01:00:59 +0800 Subject: [PATCH] load pieces hash skip error torrent --- .../Commands/TorrentLoadPiecesHash.php | 2 +- app/Repositories/TorrentRepository.php | 27 +++++++++++-------- include/constants.php | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/Console/Commands/TorrentLoadPiecesHash.php b/app/Console/Commands/TorrentLoadPiecesHash.php index fa4d2a36..a7f76183 100644 --- a/app/Console/Commands/TorrentLoadPiecesHash.php +++ b/app/Console/Commands/TorrentLoadPiecesHash.php @@ -33,7 +33,7 @@ class TorrentLoadPiecesHash extends Command $rep = new TorrentRepository(); $this->info("id: $id, going to load pieces hash..."); $total = $rep->loadPiecesHashCache($id); - $this->info(sprintf("total: %s, cost time: %s seconds.", $total, time() - $begin)); + $this->info(sprintf("%s, total: %s, cost time: %s seconds.", nexus()->getRequestId(), $total, time() - $begin)); return Command::SUCCESS; } } diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 929aefe4..8fbcf304 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -825,17 +825,24 @@ HTML; } $pipe = NexusDB::redis()->multi(\Redis::PIPELINE); $piecesHashCaseWhen = $updateIdArr = []; + $count = 0; foreach ($list as $item) { - $piecesHash = $item->pieces_hash; - if (!$piecesHash) { - $torrentFile = $torrentDir . $item->id . ".torrent"; - $loadResult = Bencode::load($torrentFile); - $piecesHash = sha1($loadResult['info']['pieces']); - $piecesHashCaseWhen[] = sprintf("when %s then '%s'", $item->id, $piecesHash); - $updateIdArr[] = $item->id; - do_log(sprintf("torrent: %s no pieces hash, load from torrent file: %s, pieces hash: %s", $item->id, $torrentFile, $piecesHash)); + try { + $piecesHash = $item->pieces_hash; + if (!$piecesHash) { + $torrentFile = $torrentDir . $item->id . ".torrent"; + $loadResult = Bencode::load($torrentFile); + $piecesHash = sha1($loadResult['info']['pieces']); + $piecesHashCaseWhen[] = sprintf("when %s then '%s'", $item->id, $piecesHash); + $updateIdArr[] = $item->id; + do_log(sprintf("torrent: %s no pieces hash, load from torrent file: %s, pieces hash: %s", $item->id, $torrentFile, $piecesHash)); + } + $pipe->hSet(self::PIECES_HASH_CACHE_KEY, $piecesHash, $this->buildPiecesHashCacheValue($item->id, $piecesHash)); + $total++; + $count++; + } catch (\Exception $exception) { + do_log(sprintf("load pieces hash of torrent: %s error: %s", $item->id, $exception->getMessage()), 'error'); } - $pipe->hSet(self::PIECES_HASH_CACHE_KEY, $piecesHash, $this->buildPiecesHashCacheValue($item->id, $piecesHash)); } $pipe->exec(); if (!empty($piecesHashCaseWhen)) { @@ -846,8 +853,6 @@ HTML; ); NexusDB::statement($sql); } - $count = $list->count(); - $total += $count; do_log("success load page: $page, size: $size, count: $count"); $page++; } diff --git a/include/constants.php b/include/constants.php index aabe95cc..684306fd 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@