Skip to content

Commit

Permalink
load pieces hash return total and success
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Jul 30, 2023
1 parent b9118ae commit 565c0f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/TorrentLoadPiecesHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function handle()
$id = $this->option('id');
$rep = new TorrentRepository();
$this->info("id: $id, going to load pieces hash...");
$total = $rep->loadPiecesHashCache($id);
$this->info(sprintf("%s, total: %s, cost time: %s seconds.", nexus()->getRequestId(), $total, time() - $begin));
$result = $rep->loadPiecesHashCache($id);
$this->info(sprintf("%s, result: %s, cost time: %s seconds.", nexus()->getRequestId(), json_encode($result), time() - $begin));
return Command::SUCCESS;
}
}
18 changes: 10 additions & 8 deletions app/Repositories/TorrentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Illuminate\Support\Str;
use Nexus\Database\NexusDB;
use Rhilip\Bencode\Bencode;
use function Sodium\compare;

class TorrentRepository extends BaseRepository
{
Expand Down Expand Up @@ -803,15 +804,15 @@ public function getPiecesHashCache($piecesHash): array
return $out;
}

public function loadPiecesHashCache($id = 0): int
public function loadPiecesHashCache($id = 0): array
{
$page = 1;
$size = 1000;
$query = Torrent::query();
if ($id) {
$query = $query->whereIn("id", Arr::wrap($id));
}
$total = 0;
$total = $success = 0;
$torrentDir = sprintf(
"%s/%s/",
rtrim(ROOT_PATH, '/'),
Expand All @@ -825,8 +826,9 @@ public function loadPiecesHashCache($id = 0): int
}
$pipe = NexusDB::redis()->multi(\Redis::PIPELINE);
$piecesHashCaseWhen = $updateIdArr = [];
$count = 0;
$currentCount = 0;
foreach ($list as $item) {
$total++;
try {
$piecesHash = $item->pieces_hash;
if (!$piecesHash) {
Expand All @@ -838,8 +840,8 @@ public function loadPiecesHashCache($id = 0): int
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++;
$success++;
$currentCount++;
} catch (\Exception $exception) {
do_log(sprintf("load pieces hash of torrent: %s error: %s", $item->id, $exception->getMessage()), 'error');
}
Expand All @@ -853,11 +855,11 @@ public function loadPiecesHashCache($id = 0): int
);
NexusDB::statement($sql);
}
do_log("success load page: $page, size: $size, count: $count");
do_log("success load page: $page, size: $size, count: $currentCount");
$page++;
}
do_log("[DONE], total: $total");
return $total;
do_log("[DONE], total: $total, success: $success");
return compact('total', 'success');
}

}

0 comments on commit 565c0f1

Please sign in to comment.