update: rollback

This commit is contained in:
tokumeikoi 2021-09-02 20:28:24 +09:00
parent decbae1413
commit 2a5e9ef079
4 changed files with 11 additions and 32 deletions

View File

@ -9,7 +9,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
class TrafficFetchJob implements ShouldQueue class TrafficFetchJob implements ShouldQueue
{ {
@ -45,18 +44,14 @@ class TrafficFetchJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
$user = User::find($this->userId); $user = User::lockForUpdate()->find($this->userId);
if (!$user) return; if (!$user) return;
try {
$user->update([ $user->t = time();
't' => time(), $user->u = $user->u + $this->u;
'u' => DB::raw("u+{$this->u}"), $user->d = $user->d + $this->d;
'd' => DB::raw("d+{$this->d}") if (!$user->save()) throw new \Exception('流量更新失败');
]);
} catch (\Exception $e) {
throw new \Exception('流量更新失败');
}
$mailService = new MailService(); $mailService = new MailService();
$mailService->remindTraffic($user->first()); $mailService->remindTraffic($user);
} }
} }

View File

@ -13,8 +13,4 @@ class ServerLog extends Model
'created_at' => 'timestamp', 'created_at' => 'timestamp',
'updated_at' => 'timestamp' 'updated_at' => 'timestamp'
]; ];
protected $fillable = [
'u',
'd'
];
} }

View File

@ -13,9 +13,4 @@ class User extends Model
'created_at' => 'timestamp', 'created_at' => 'timestamp',
'updated_at' => 'timestamp' 'updated_at' => 'timestamp'
]; ];
protected $fillable = [
'u',
'd',
't'
];
} }

View File

@ -9,7 +9,6 @@ use App\Models\Server;
use App\Models\ServerTrojan; use App\Models\ServerTrojan;
use App\Utils\CacheKey; use App\Utils\CacheKey;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class ServerService class ServerService
{ {
@ -269,18 +268,12 @@ class ServerService
->where('user_id', $userId) ->where('user_id', $userId)
->where('rate', $rate) ->where('rate', $rate)
->where('method', $method) ->where('method', $method)
->lockForUpdate()
->first(); ->first();
if ($serverLog) { if ($serverLog) {
try { $serverLog->u = $serverLog->u + $u;
$serverLog->update([ $serverLog->d = $serverLog->d + $d;
'u' => DB::raw("u+{$u}"), return $serverLog->save();
'd' => DB::raw("d+{$d}")
]);
return true;
} catch (\Exception $e) {
info($e);
return false;
}
} else { } else {
$serverLog = new ServerLog(); $serverLog = new ServerLog();
$serverLog->user_id = $userId; $serverLog->user_id = $userId;