mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 20:17:24 +08:00
improve cleanup jobs
This commit is contained in:
@@ -84,6 +84,8 @@ class CalculateUserSeedBonus implements ShouldQueue
|
|||||||
$logFile = getLogFile("seed-bonus-points");
|
$logFile = getLogFile("seed-bonus-points");
|
||||||
do_log("$logPrefix, [GET_UID_REAL], count: " . count($results) . ", logFile: $logFile");
|
do_log("$logPrefix, [GET_UID_REAL], count: " . count($results) . ", logFile: $logFile");
|
||||||
$fd = fopen($logFile, 'a');
|
$fd = fopen($logFile, 'a');
|
||||||
|
$seedPointsUpdates = $seedPointsPerHourUpdates = $seedBonusUpdates = [];
|
||||||
|
$logStr = "";
|
||||||
foreach ($results as $userInfo)
|
foreach ($results as $userInfo)
|
||||||
{
|
{
|
||||||
$uid = $userInfo['id'];
|
$uid = $userInfo['id'];
|
||||||
@@ -115,10 +117,13 @@ class CalculateUserSeedBonus implements ShouldQueue
|
|||||||
$dividend = 3600 / $autoclean_interval_one;
|
$dividend = 3600 / $autoclean_interval_one;
|
||||||
$all_bonus = $all_bonus / $dividend;
|
$all_bonus = $all_bonus / $dividend;
|
||||||
$seed_points = $seedBonusResult['seed_points'] / $dividend;
|
$seed_points = $seedBonusResult['seed_points'] / $dividend;
|
||||||
$updatedAt = now()->toDateTimeString();
|
// $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";
|
// $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");
|
// do_log("$bonusLog, query: $sql");
|
||||||
NexusDB::statement($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) {
|
if ($fd) {
|
||||||
$log = sprintf(
|
$log = sprintf(
|
||||||
'%s|%s|%s|%s|%s|%s|%s|%s',
|
'%s|%s|%s|%s|%s|%s|%s|%s',
|
||||||
@@ -126,16 +131,27 @@ class CalculateUserSeedBonus implements ShouldQueue
|
|||||||
$userInfo['seed_points'], number_format($seed_points, 1, '.', ''), number_format($userInfo['seed_points'] + $seed_points, 1, '.', ''),
|
$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, '.', '')
|
$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 {
|
} else {
|
||||||
do_log("logFile: $logFile is not writeable!", 'error');
|
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) {
|
if ($delIdRedisKey) {
|
||||||
NexusDB::cache_del($this->idRedisKey);
|
NexusDB::cache_del($this->idRedisKey);
|
||||||
}
|
}
|
||||||
|
fwrite($fd, $logStr);
|
||||||
$costTime = time() - $beginTimestamp;
|
$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
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -76,43 +76,39 @@ class UpdateTorrentSeedersEtc implements ShouldQueue
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$torrentIdArr = explode(",", $idStr);
|
$torrentIdArr = explode(",", $idStr);
|
||||||
foreach ($torrentIdArr as $torrentId) {
|
//批量取,简单化
|
||||||
if ($torrentId <= 0) {
|
$torrents = array();
|
||||||
continue;
|
$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)) {
|
||||||
$peerResult = NexusDB::table('peers')
|
if ($row["seeder"] == "yes")
|
||||||
->where('torrent', $torrentId)
|
$key = "seeders";
|
||||||
->selectRaw("count(*) as count, seeder")
|
else
|
||||||
->groupBy('seeder')
|
$key = "leechers";
|
||||||
->get()
|
$torrents[$row["torrent"]][$key] = $row["c"];
|
||||||
;
|
|
||||||
$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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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) {
|
if ($delIdRedisKey) {
|
||||||
NexusDB::cache_del($this->idRedisKey);
|
NexusDB::cache_del($this->idRedisKey);
|
||||||
}
|
}
|
||||||
$costTime = time() - $beginTimestamp;
|
$costTime = time() - $beginTimestamp;
|
||||||
do_log(sprintf(
|
do_log(sprintf(
|
||||||
"$logPrefix, [DONE], update torrent count: %s, cost time: %s seconds",
|
"$logPrefix, [DONE], update torrent count: %s, result: %s, cost time: %s seconds",
|
||||||
count($torrentIdArr), $costTime
|
count($torrentIdArr), var_export($result, true), $costTime
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ class UpdateUserSeedingLeechingTime implements ShouldQueue
|
|||||||
$beginTimestamp = time();
|
$beginTimestamp = time();
|
||||||
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME_HANDLE_JOB], commonRequestId: %s, beginUid: %s, endUid: %s", $this->requestId, $this->beginUid, $this->endUid);
|
$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;
|
$idStr = $this->idStr;
|
||||||
$delIdRedisKey = false;
|
$delIdRedisKey = false;
|
||||||
if (empty($idStr) && !empty($this->idRedisKey)) {
|
if (empty($idStr) && !empty($this->idRedisKey)) {
|
||||||
@@ -76,33 +75,29 @@ class UpdateUserSeedingLeechingTime implements ShouldQueue
|
|||||||
do_log("$logPrefix, no idStr or idRedisKey", "error");
|
do_log("$logPrefix, no idStr or idRedisKey", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$uidArr = explode(",", $idStr);
|
//批量取,简单化
|
||||||
foreach ($uidArr as $uid) {
|
$res = sql_query("select userid, sum(seedtime) as seedtime_sum, sum(leechtime) as leechtime_sum from snatched group by userid where userid in ($idStr)");
|
||||||
if ($uid <= 0) {
|
$seedtimeUpdates = $leechTimeUpdates = [];
|
||||||
continue;
|
$nowStr = now()->toDateTimeString();
|
||||||
}
|
$count = 0;
|
||||||
$sumInfo = NexusDB::table('snatched')
|
while ($row = mysql_fetch_assoc($res)) {
|
||||||
->selectRaw('sum(seedtime) as seedtime_sum, sum(leechtime) as leechtime_sum')
|
$count++;
|
||||||
->where('userid', $uid)
|
$seedtimeUpdates = sprintf("when %d then %d", $row['userid'], $row['seedtime_sum'] ?? 0);
|
||||||
->first();
|
$leechTimeUpdates = sprintf("when %d then %d", $row['userid'], $row['leechtime_sum'] ?? 0);
|
||||||
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++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$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) {
|
if ($delIdRedisKey) {
|
||||||
NexusDB::cache_del($this->idRedisKey);
|
NexusDB::cache_del($this->idRedisKey);
|
||||||
}
|
}
|
||||||
$costTime = time() - $beginTimestamp;
|
$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
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ final class ThirdPartyJob {
|
|||||||
|
|
||||||
public static function addBuyTorrent(int $userId, int $torrentId): 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])) {
|
if (NexusDB::redis()->set($key, now()->toDateTimeString(), ['nx', 'ex' => 3600])) {
|
||||||
$value = [
|
$value = [
|
||||||
'name' => self::JOB_BUY_TORRENT,
|
'name' => self::JOB_BUY_TORRENT,
|
||||||
|
|||||||
Reference in New Issue
Block a user