mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-21 18:37:31 +08:00
Refactoring cleanup tasks to update user seed time and seed comments etc.
This commit is contained in:
@@ -6,45 +6,53 @@ use Nexus\Database\NexusDB;
|
||||
|
||||
class CleanupRepository extends BaseRepository
|
||||
{
|
||||
const USER_SEED_BONUS_BATCH_LIST_KEY = "batch_key:user_seed_bonus";
|
||||
const USER_SEEDING_LEECHING_TIME_BATCH_LIST_KEY = "batch_key:user_seeding_leeching_time";
|
||||
const TORRENT_SEEDERS_ETC_BATCH_LIST_KEY = "batch_key:torrent_seeders_etc";
|
||||
const USER_SEED_BONUS_BATCH_KEY = "batch_key:user_seed_bonus";
|
||||
const USER_SEEDING_LEECHING_TIME_BATCH_KEY = "batch_key:user_seeding_leeching_time";
|
||||
const TORRENT_SEEDERS_ETC_BATCH_KEY = "batch_key:torrent_seeders_etc";
|
||||
|
||||
public static function recordBatch(\Redis $redis, $uid, $torrentId)
|
||||
{
|
||||
self::doRecordBatch($redis, self::USER_SEED_BONUS_BATCH_LIST_KEY, $uid);
|
||||
self::doRecordBatch($redis, self::USER_SEEDING_LEECHING_TIME_BATCH_LIST_KEY, $uid);
|
||||
self::doRecordBatch($redis, self::TORRENT_SEEDERS_ETC_BATCH_LIST_KEY, $torrentId);
|
||||
}
|
||||
|
||||
private static function doRecordBatch(\Redis $redis, $batchListKey, $hashKey)
|
||||
{
|
||||
$batchKey = $redis->rPop($batchListKey);
|
||||
if ($batchKey === false) {
|
||||
//not exists
|
||||
$batchKey = date('YmdHis');
|
||||
$redis->lPush($batchListKey, $batchKey);
|
||||
$args = [
|
||||
self::USER_SEEDING_LEECHING_TIME_BATCH_KEY, self::TORRENT_SEEDERS_ETC_BATCH_KEY,
|
||||
$uid, $torrentId, self::getHashKeySuffix()
|
||||
];
|
||||
$result = $redis->eval(self::getAddRecordLuaScript(), $args, 2);
|
||||
$err = $redis->getLastError();
|
||||
if ($err) {
|
||||
do_log("[REDIS_LUA_ERROR]: $err", "error");
|
||||
}
|
||||
$redis->hSetNx($batchKey, $hashKey, date('YmdHis'));
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public static function updateUserLeechingSeedingTime($requestId)
|
||||
public static function runBatchJob($batchKey, $requestId)
|
||||
{
|
||||
global $Cache;
|
||||
$redis = $Cache->getRedis();
|
||||
|
||||
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_USER_SEEDING_LEECHING_TIME], commonRequestId: %s", $requestId);
|
||||
$redis = NexusDB::redis();
|
||||
$logPrefix = sprintf("[$batchKey], commonRequestId: %s", $requestId);
|
||||
$beginTimestamp = time();
|
||||
|
||||
$count = 0;
|
||||
$size = 1000;
|
||||
$batchListKey = self::USER_SEEDING_LEECHING_TIME_BATCH_LIST_KEY;
|
||||
$batch = $redis->lPop($batchListKey);
|
||||
if ($batch === false) {
|
||||
do_log("$logPrefix, batchListKey: $batchListKey, no batch...");
|
||||
$batch = self::getBatch($redis, $batchKey);
|
||||
if (!$batch) {
|
||||
do_log("$logPrefix, batchKey: $batchKey no batch...", 'error');
|
||||
return;
|
||||
}
|
||||
//update the batch key
|
||||
$redis->set($batchKey, $batchKey . self::getHashKeySuffix());
|
||||
$count = match ($batchKey) {
|
||||
self::USER_SEEDING_LEECHING_TIME_BATCH_KEY => self::updateUserLeechingSeedingTime($redis, $batch, $logPrefix),
|
||||
self::TORRENT_SEEDERS_ETC_BATCH_KEY => self::updateTorrentSeedersEtc($redis, $batch, $logPrefix),
|
||||
default => throw new \InvalidArgumentException("Invalid batchKey: $batchKey")
|
||||
};
|
||||
//remove this batch
|
||||
$redis->del($batch);
|
||||
$endTimestamp = time();
|
||||
do_log(sprintf("$logPrefix, [DONE], batch: $batch, count: $count, cost time: %d seconds", $endTimestamp - $beginTimestamp));
|
||||
}
|
||||
|
||||
|
||||
private static function updateUserLeechingSeedingTime(\Redis $redis, $batch, $logPrefix): int
|
||||
{
|
||||
$count = 0;
|
||||
$size = 1000;
|
||||
$it = NULL;
|
||||
/* Don't ever return an empty array until we're done iterating */
|
||||
$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
|
||||
@@ -68,28 +76,15 @@ class CleanupRepository extends BaseRepository
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
sleep(rand(10, 60));
|
||||
sleep(rand(1, 10));
|
||||
}
|
||||
$costTime = time() - $beginTimestamp;
|
||||
do_log("$logPrefix, [DONE] success update count: $count, cost time: $costTime seconds");
|
||||
return $count;
|
||||
}
|
||||
|
||||
public static function updateTorrentSeedersEtc($requestId)
|
||||
private static function updateTorrentSeedersEtc(\Redis $redis, $batch, $logPrefix)
|
||||
{
|
||||
global $Cache;
|
||||
$redis = $Cache->getRedis();
|
||||
|
||||
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_TORRENT_SEEDERS_ETC], commonRequestId: %s", $requestId);
|
||||
$beginTimestamp = time();
|
||||
|
||||
$count = 0;
|
||||
$size = 1000;
|
||||
$batchListKey = self::TORRENT_SEEDERS_ETC_BATCH_LIST_KEY;
|
||||
$batch = $redis->lPop($batchListKey);
|
||||
if ($batch === false) {
|
||||
do_log("$logPrefix, batchListKey: $batchListKey, no batch...");
|
||||
return;
|
||||
}
|
||||
$it = NULL;
|
||||
/* Don't ever return an empty array until we're done iterating */
|
||||
$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
|
||||
@@ -123,9 +118,59 @@ class CleanupRepository extends BaseRepository
|
||||
do_log("$logPrefix, [SUCCESS]: $torrentId => " . json_encode($update));
|
||||
$count++;
|
||||
}
|
||||
sleep(rand(10, 60));
|
||||
sleep(rand(1, 10));
|
||||
}
|
||||
$costTime = time() - $beginTimestamp;
|
||||
do_log("$logPrefix, [DONE] success update count: $count, cost time: $costTime seconds");
|
||||
return $count;
|
||||
}
|
||||
|
||||
private static function getBatch(\Redis $redis, $batchKey)
|
||||
{
|
||||
$batch = $redis->get($batchKey);
|
||||
if ($batch === false) {
|
||||
do_log("batchKey: $batchKey, no batch...", 'error');
|
||||
return false;
|
||||
}
|
||||
if (!$redis->exists($batch)) {
|
||||
do_log("batch: $batch, not exists...", 'error');
|
||||
return false;
|
||||
}
|
||||
return $batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* USER_SEEDING_LEECHING_TIME, TORRENT_SEEDERS_ETC, uid, torrentId, timeStr
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function getAddRecordLuaScript(): string
|
||||
{
|
||||
return <<<'LUA'
|
||||
local batchList = {KEYS[1], KEYS[2]}
|
||||
for k, v in pairs(batchList) do
|
||||
local batchKey = redis.call("GET", v)
|
||||
local isBatchKeyNew = false
|
||||
if batchKey == false then
|
||||
batchKey = v .. ARGV[3]
|
||||
redis.call("SET", v, batchKey, "EX", 2592000)
|
||||
isBatchKeyNew = true
|
||||
end
|
||||
local hashKey
|
||||
if k == 1 then
|
||||
hashKey = ARGV[1]
|
||||
else
|
||||
hashKey = ARGV[2]
|
||||
end
|
||||
redis.call("HSETNX", batchKey, hashKey, ARGV[3])
|
||||
if isBatchKeyNew then
|
||||
redis.call("EXPIRE", batchKey, 2592000)
|
||||
end
|
||||
end
|
||||
LUA;
|
||||
}
|
||||
|
||||
private static function getHashKeySuffix(): string
|
||||
{
|
||||
return ":" . date('Ymd_His');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user