From 2721c96d27761f3d5e84656dd66b0a41dbce04e7 Mon Sep 17 00:00:00 2001 From: xiaomlove <1939737565@qq.com> Date: Fri, 8 Nov 2024 20:14:45 +0800 Subject: [PATCH] job add middleware --- app/Http/Middleware/TrustProxies.php | 2 +- app/Jobs/CalculateUserSeedBonus.php | 23 ++++++++++++++++++---- app/Jobs/UpdateTorrentSeedersEtc.php | 16 ++++++++++++++- app/Jobs/UpdateUserSeedingLeechingTime.php | 16 ++++++++++++++- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/app/Http/Middleware/TrustProxies.php b/app/Http/Middleware/TrustProxies.php index 3391630ec..00cfb6dcb 100644 --- a/app/Http/Middleware/TrustProxies.php +++ b/app/Http/Middleware/TrustProxies.php @@ -12,7 +12,7 @@ class TrustProxies extends Middleware * * @var array|string|null */ - protected $proxies; + protected $proxies = ['*']; /** * The headers that should be used to detect proxies. diff --git a/app/Jobs/CalculateUserSeedBonus.php b/app/Jobs/CalculateUserSeedBonus.php index 08293e1f1..ddc1dd235 100644 --- a/app/Jobs/CalculateUserSeedBonus.php +++ b/app/Jobs/CalculateUserSeedBonus.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; use Nexus\Database\NexusDB; use Nexus\Nexus; @@ -55,6 +56,16 @@ public function retryUntil() public $timeout = 3600; + /** + * 获取任务时,应该通过的中间件。 + * + * @return array + */ + public function middleware() + { + return [new WithoutOverlapping($this->idRedisKey)]; + } + /** * Execute the job. * @@ -63,7 +74,10 @@ public function retryUntil() public function handle() { $beginTimestamp = time(); - $logPrefix = sprintf("[CLEANUP_CLI_CALCULATE_SEED_BONUS_HANDLE_JOB], commonRequestId: %s, beginUid: %s, endUid: %s", $this->requestId, $this->beginUid, $this->endUid); + $logPrefix = sprintf( + "[CLEANUP_CLI_CALCULATE_SEED_BONUS_HANDLE_JOB], commonRequestId: %s, beginUid: %s, endUid: %s, idStr: %s, idRedisKey: %s", + $this->requestId, $this->beginUid, $this->endUid, $this->idStr, $this->idRedisKey + ); $haremAdditionFactor = Setting::get('bonus.harem_addition'); $officialAdditionFactor = Setting::get('bonus.official_addition'); $donortimes_bonus = Setting::get('bonus.donortimes'); @@ -114,6 +128,7 @@ public function handle() $all_bonus += $medalAddition; $bonusLog .= ", medalAdditionFactor: {$seedBonusResult['medal_additional_factor']}, medalBonus: {$seedBonusResult['medal_bonus']}, medalAddition: $medalAddition, all_bonus: $all_bonus"; } + do_log($bonusLog); $dividend = 3600 / $autoclean_interval_one; $all_bonus = $all_bonus / $dividend; $seed_points = $seedBonusResult['seed_points'] / $dividend; @@ -121,9 +136,9 @@ public function handle() // $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("when %d then ifnull(seed_points, 0) + %d", $uid, $seed_points); - $seedPointsPerHourUpdates[] = sprintf("when %d then %d", $uid, $seedBonusResult['seed_points']); - $seedBonusUpdates[] = sprintf("when %d then %d", $uid, $all_bonus); + $seedPointsUpdates[] = sprintf("when %d then ifnull(seed_points, 0) + %f", $uid, $seed_points); + $seedPointsPerHourUpdates[] = sprintf("when %d then %f", $uid, $seedBonusResult['seed_points']); + $seedBonusUpdates[] = sprintf("when %d then seedbonus + %f", $uid, $all_bonus); if ($fd) { $log = sprintf( '%s|%s|%s|%s|%s|%s|%s|%s', diff --git a/app/Jobs/UpdateTorrentSeedersEtc.php b/app/Jobs/UpdateTorrentSeedersEtc.php index 6efc3e88c..a23708f0b 100644 --- a/app/Jobs/UpdateTorrentSeedersEtc.php +++ b/app/Jobs/UpdateTorrentSeedersEtc.php @@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; use Nexus\Database\NexusDB; @@ -55,6 +56,16 @@ public function retryUntil() public $timeout = 1800; + /** + * 获取任务时,应该通过的中间件。 + * + * @return array + */ + public function middleware() + { + return [new WithoutOverlapping($this->idRedisKey)]; + } + /** * Execute the job. * @@ -63,7 +74,10 @@ public function retryUntil() public function handle() { $beginTimestamp = time(); - $logPrefix = sprintf("[CLEANUP_CLI_UPDATE_TORRENT_SEEDERS_ETC_HANDLE_JOB], commonRequestId: %s, beginTorrentId: %s, endTorrentId: %s", $this->requestId, $this->beginTorrentId, $this->endTorrentId); + $logPrefix = sprintf( + "[CLEANUP_CLI_UPDATE_TORRENT_SEEDERS_ETC_HANDLE_JOB], commonRequestId: %s, beginTorrentId: %s, endTorrentId: %s, idStr: %s, idRedisKey: %s", + $this->requestId, $this->beginTorrentId, $this->endTorrentId, $this->idStr, $this->idRedisKey + ); $idStr = $this->idStr; $delIdRedisKey = false; diff --git a/app/Jobs/UpdateUserSeedingLeechingTime.php b/app/Jobs/UpdateUserSeedingLeechingTime.php index 6cad28ad8..37d7bf0a0 100644 --- a/app/Jobs/UpdateUserSeedingLeechingTime.php +++ b/app/Jobs/UpdateUserSeedingLeechingTime.php @@ -10,6 +10,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; use Nexus\Database\NexusDB; @@ -55,6 +56,16 @@ public function retryUntil() public $timeout = 3600; + /** + * 获取任务时,应该通过的中间件。 + * + * @return array + */ + public function middleware() + { + return [new WithoutOverlapping($this->idRedisKey)]; + } + /** * Execute the job. * @@ -63,7 +74,10 @@ public function retryUntil() 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); + $logPrefix = sprintf( + "[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME_HANDLE_JOB], commonRequestId: %s, beginUid: %s, endUid: %s, idStr: %s, idRedisKey: %s", + $this->requestId, $this->beginUid, $this->endUid, $this->idStr, $this->idRedisKey, + ); $idStr = $this->idStr; $delIdRedisKey = false;