Skip to content

Commit

Permalink
improve cleanup jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Nov 6, 2024
1 parent 2267169 commit f1af33c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 61 deletions.
28 changes: 22 additions & 6 deletions app/Jobs/CalculateUserSeedBonus.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function handle()
$logFile = getLogFile("seed-bonus-points");
do_log("$logPrefix, [GET_UID_REAL], count: " . count($results) . ", logFile: $logFile");
$fd = fopen($logFile, 'a');
$seedPointsUpdates = $seedPointsPerHourUpdates = $seedBonusUpdates = [];
$logStr = "";
foreach ($results as $userInfo)
{
$uid = $userInfo['id'];
Expand Down Expand Up @@ -115,27 +117,41 @@ public function handle()
$dividend = 3600 / $autoclean_interval_one;
$all_bonus = $all_bonus / $dividend;
$seed_points = $seedBonusResult['seed_points'] / $dividend;
$updatedAt = now()->toDateTimeString();
$sql = "update users set seed_points = ifnull(seed_points, 0) + $seed_points, seed_points_per_hour = {$seedBonusResult['seed_points']}, seedbonus = seedbonus + $all_bonus, seed_points_updated_at = '$updatedAt' where id = $uid limit 1";
do_log("$bonusLog, query: $sql");
NexusDB::statement($sql);
// $updatedAt = now()->toDateTimeString();
// $sql = "update users set seed_points = ifnull(seed_points, 0) + $seed_points, seed_points_per_hour = {$seedBonusResult['seed_points']}, seedbonus = seedbonus + $all_bonus, seed_points_updated_at = '$updatedAt' where id = $uid limit 1";
// do_log("$bonusLog, query: $sql");
// NexusDB::statement($sql);
$seedPointsUpdates[] = sprintf("case %d then ifnull(seed_points, 0) + %d", $uid, $seed_points);
$seedPointsPerHourUpdates[] = sprintf("case %d then %d", $uid, $seedBonusResult['seed_points']);
$seedBonusUpdates[] = sprintf("case %d then %d", $uid, $all_bonus);
if ($fd) {
$log = sprintf(
'%s|%s|%s|%s|%s|%s|%s|%s',
date('Y-m-d H:i:s'), $uid,
$userInfo['seed_points'], number_format($seed_points, 1, '.', ''), number_format($userInfo['seed_points'] + $seed_points, 1, '.', ''),
$userInfo['seedbonus'], number_format($all_bonus, 1, '.', ''), number_format($userInfo['seedbonus'] + $all_bonus, 1, '.', '')
);
fwrite($fd, $log . PHP_EOL);
// fwrite($fd, $log . PHP_EOL);
$logStr .= $log . PHP_EOL;
} else {
do_log("logFile: $logFile is not writeable!", 'error');
}
}
$nowStr = now()->toDateTimeString();
$sql = sprintf(
"update users set seed_points = case id %s end, seed_points_per_hour = case id %s end, seedbonus = case id %s end, seed_points_updated_at = '%s' where id in (%s)",
implode(" ", $seedPointsUpdates), implode(" ", $seedPointsPerHourUpdates), implode(" ", $seedBonusUpdates), $nowStr, $idStr
);
$result = sql_query($sql);
if ($delIdRedisKey) {
NexusDB::cache_del($this->idRedisKey);
}
fwrite($fd, $logStr);
$costTime = time() - $beginTimestamp;
do_log("$logPrefix, [DONE], cost time: $costTime seconds");
do_log(sprintf(
"$logPrefix, [DONE], update user count: %s, result: %s, cost time: %s seconds",
count($seedPointsUpdates), var_export($result, true), $costTime
));
}

/**
Expand Down
58 changes: 27 additions & 31 deletions app/Jobs/UpdateTorrentSeedersEtc.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,39 @@ public function handle()
return;
}
$torrentIdArr = explode(",", $idStr);
foreach ($torrentIdArr as $torrentId) {
if ($torrentId <= 0) {
continue;
}
$peerResult = NexusDB::table('peers')
->where('torrent', $torrentId)
->selectRaw("count(*) as count, seeder")
->groupBy('seeder')
->get()
;
$commentResult = NexusDB::table('comments')
->where('torrent',$torrentId)
->selectRaw("count(*) as count")
->first()
;
$update = [
'comments' => $commentResult && $commentResult->count !== null ? $commentResult->count : 0,
'seeders' => 0,
'leechers' => 0,
];
foreach ($peerResult as $item) {
if ($item->seeder == 'yes') {
$update['seeders'] = $item->count;
} elseif ($item->seeder == 'no') {
$update['leechers'] = $item->count;
}
}
NexusDB::table('torrents')->where('id', $torrentId)->update($update);
do_log("[CLEANUP_CLI_UPDATE_TORRENT_SEEDERS_ETC_HANDLE_TORRENT], [SUCCESS]: $torrentId => " . json_encode($update));
//批量取,简单化
$torrents = array();
$res = sql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder where torrent in ($idStr)");
while ($row = mysql_fetch_assoc($res)) {
if ($row["seeder"] == "yes")
$key = "seeders";
else
$key = "leechers";
$torrents[$row["torrent"]][$key] = $row["c"];
}

$res = sql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent where torrent in ($idStr)");
while ($row = mysql_fetch_assoc($res)) {
$torrents[$row["torrent"]]["comments"] = $row["c"];
}
$seedersUpdates = $leechersUpdates = $commentsUpdates = [];
foreach ($torrentIdArr as $id) {
$seedersUpdates[] = sprintf("when %d then %d", $id, $torrents[$id]["seeders"] ?? 0);
$leechersUpdates[] = sprintf("when %d then %d", $id, $torrents[$id]["leechers"] ?? 0);
$commentsUpdates[] = sprintf("when %d then %d", $id, $torrents[$id]["comments"] ?? 0);
}
$sql = sprintf(
"update torrents set seeders = case id %s end, leechers = case id %s end, comments = case id %s end where id in (%s)",
implode(" ", $seedersUpdates), implode(" ", $leechersUpdates), implode(" ", $commentsUpdates), $idStr
);
$result = sql_query($sql);
if ($delIdRedisKey) {
NexusDB::cache_del($this->idRedisKey);
}
$costTime = time() - $beginTimestamp;
do_log(sprintf(
"$logPrefix, [DONE], update torrent count: %s, cost time: %s seconds",
count($torrentIdArr), $costTime
"$logPrefix, [DONE], update torrent count: %s, result: %s, cost time: %s seconds",
count($torrentIdArr), var_export($result, true), $costTime
));
}

Expand Down
41 changes: 18 additions & 23 deletions app/Jobs/UpdateUserSeedingLeechingTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function handle()
$beginTimestamp = time();
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME_HANDLE_JOB], commonRequestId: %s, beginUid: %s, endUid: %s", $this->requestId, $this->beginUid, $this->endUid);

$count = 0;
$idStr = $this->idStr;
$delIdRedisKey = false;
if (empty($idStr) && !empty($this->idRedisKey)) {
Expand All @@ -76,33 +75,29 @@ public function handle()
do_log("$logPrefix, no idStr or idRedisKey", "error");
return;
}
$uidArr = explode(",", $idStr);
foreach ($uidArr as $uid) {
if ($uid <= 0) {
continue;
}
$sumInfo = NexusDB::table('snatched')
->selectRaw('sum(seedtime) as seedtime_sum, sum(leechtime) as leechtime_sum')
->where('userid', $uid)
->first();
if ($sumInfo && $sumInfo->seedtime_sum !== null) {
$update = [
'seedtime' => $sumInfo->seedtime_sum ?? 0,
'leechtime' => $sumInfo->leechtime_sum ?? 0,
'seed_time_updated_at' => Carbon::now()->toDateTimeString(),
];
NexusDB::table('users')
->where('id', $uid)
->update($update);
do_log("[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME_HANDLE_USER], [SUCCESS]: $uid => " . json_encode($update));
$count++;
}
//批量取,简单化
$res = sql_query("select userid, sum(seedtime) as seedtime_sum, sum(leechtime) as leechtime_sum from snatched group by userid where userid in ($idStr)");
$seedtimeUpdates = $leechTimeUpdates = [];
$nowStr = now()->toDateTimeString();
$count = 0;
while ($row = mysql_fetch_assoc($res)) {
$count++;
$seedtimeUpdates = sprintf("when %d then %d", $row['userid'], $row['seedtime_sum'] ?? 0);
$leechTimeUpdates = sprintf("when %d then %d", $row['userid'], $row['leechtime_sum'] ?? 0);
}
$sql = sprintf(
"update users set seedtime = case id %s end, leechtime = case id %s end, seed_time_updated_at = '%s' where id in (%s)",
implode(" ", $seedtimeUpdates), implode(" ", $leechTimeUpdates), $nowStr, $idStr
);
$result = sql_query($sql);
if ($delIdRedisKey) {
NexusDB::cache_del($this->idRedisKey);
}
$costTime = time() - $beginTimestamp;
do_log("$logPrefix, [DONE], user total count: " . count($uidArr) . ", success update count: $count, cost time: $costTime seconds");
do_log(sprintf(
"$logPrefix, [DONE], update user count: %s, result: %s, cost time: %s seconds",
$count, var_export($result, true), $costTime
));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Utils/ThirdPartyJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke(): void

public static function addBuyTorrent(int $userId, int $torrentId): void
{
$key = sprintf("%s:%s_%s_%s", self::$queueKey, convertNamespaceToSnake(__METHOD__), $userId, $torrentId);
$key = sprintf("%s:%s_%s", self::$queueKey, $userId, $torrentId);
if (NexusDB::redis()->set($key, now()->toDateTimeString(), ['nx', 'ex' => 3600])) {
$value = [
'name' => self::JOB_BUY_TORRENT,
Expand Down

0 comments on commit f1af33c

Please sign in to comment.