update: traffic fetch job

This commit is contained in:
tokumeikoi 2021-09-02 19:58:04 +09:00
parent 0c14652ff7
commit decbae1413
2 changed files with 17 additions and 6 deletions

View File

@ -9,6 +9,7 @@ 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
{ {
@ -44,13 +45,18 @@ class TrafficFetchJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
$user = User::lockForUpdate()->find($this->userId); $user = User::find($this->userId);
if (!$user) return; if (!$user) return;
$user->t = time(); try {
$user->u = $user->u + $this->u; $user->update([
$user->d = $user->d + $this->d; 't' => time(),
if (!$user->save()) throw new \Exception('流量更新失败'); 'u' => DB::raw("u+{$this->u}"),
'd' => DB::raw("d+{$this->d}")
]);
} catch (\Exception $e) {
throw new \Exception('流量更新失败');
}
$mailService = new MailService(); $mailService = new MailService();
$mailService->remindTraffic($user); $mailService->remindTraffic($user->first());
} }
} }

View File

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