From 2a5e9ef079733b3ff2c0d4fb702526eaeb2af3ca Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Thu, 2 Sep 2021 20:28:24 +0900 Subject: [PATCH] update: rollback --- app/Jobs/TrafficFetchJob.php | 19 +++++++------------ app/Models/ServerLog.php | 4 ---- app/Models/User.php | 5 ----- app/Services/ServerService.php | 15 ++++----------- 4 files changed, 11 insertions(+), 32 deletions(-) diff --git a/app/Jobs/TrafficFetchJob.php b/app/Jobs/TrafficFetchJob.php index 0da5f375..a82cdc59 100644 --- a/app/Jobs/TrafficFetchJob.php +++ b/app/Jobs/TrafficFetchJob.php @@ -9,7 +9,6 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\DB; class TrafficFetchJob implements ShouldQueue { @@ -45,18 +44,14 @@ class TrafficFetchJob implements ShouldQueue */ public function handle() { - $user = User::find($this->userId); + $user = User::lockForUpdate()->find($this->userId); if (!$user) return; - try { - $user->update([ - 't' => time(), - 'u' => DB::raw("u+{$this->u}"), - 'd' => DB::raw("d+{$this->d}") - ]); - } catch (\Exception $e) { - throw new \Exception('流量更新失败'); - } + + $user->t = time(); + $user->u = $user->u + $this->u; + $user->d = $user->d + $this->d; + if (!$user->save()) throw new \Exception('流量更新失败'); $mailService = new MailService(); - $mailService->remindTraffic($user->first()); + $mailService->remindTraffic($user); } } diff --git a/app/Models/ServerLog.php b/app/Models/ServerLog.php index 0d5ef2c6..ef3590c2 100644 --- a/app/Models/ServerLog.php +++ b/app/Models/ServerLog.php @@ -13,8 +13,4 @@ class ServerLog extends Model 'created_at' => 'timestamp', 'updated_at' => 'timestamp' ]; - protected $fillable = [ - 'u', - 'd' - ]; } diff --git a/app/Models/User.php b/app/Models/User.php index 219c9efc..01c3eb51 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -13,9 +13,4 @@ class User extends Model 'created_at' => 'timestamp', 'updated_at' => 'timestamp' ]; - protected $fillable = [ - 'u', - 'd', - 't' - ]; } diff --git a/app/Services/ServerService.php b/app/Services/ServerService.php index cd040c10..cf6ef29c 100644 --- a/app/Services/ServerService.php +++ b/app/Services/ServerService.php @@ -9,7 +9,6 @@ use App\Models\Server; use App\Models\ServerTrojan; use App\Utils\CacheKey; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\DB; class ServerService { @@ -269,18 +268,12 @@ class ServerService ->where('user_id', $userId) ->where('rate', $rate) ->where('method', $method) + ->lockForUpdate() ->first(); if ($serverLog) { - try { - $serverLog->update([ - 'u' => DB::raw("u+{$u}"), - 'd' => DB::raw("d+{$d}") - ]); - return true; - } catch (\Exception $e) { - info($e); - return false; - } + $serverLog->u = $serverLog->u + $u; + $serverLog->d = $serverLog->d + $d; + return $serverLog->save(); } else { $serverLog = new ServerLog(); $serverLog->user_id = $userId;