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

View File

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