v2board/app/Jobs/TrafficFetchJob.php

58 lines
1.4 KiB
PHP
Raw Normal View History

2021-08-31 23:55:07 +08:00
<?php
namespace App\Jobs;
use App\Models\User;
use App\Services\MailService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class TrafficFetchJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $u;
protected $d;
protected $userId;
protected $server;
protected $protocol;
public $tries = 3;
2023-04-04 17:58:25 +08:00
public $timeout = 10;
2021-08-31 23:55:07 +08:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($u, $d, $userId, array $server, $protocol)
2021-08-31 23:55:07 +08:00
{
$this->onQueue('traffic_fetch');
$this->u = $u;
$this->d = $d;
$this->userId = $userId;
$this->server = $server;
$this->protocol = $protocol;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2021-09-02 19:28:24 +08:00
$user = User::lockForUpdate()->find($this->userId);
2021-08-31 23:55:07 +08:00
if (!$user) return;
2021-09-02 19:28:24 +08:00
$user->t = time();
$user->u = $user->u + ($this->u * $this->server['rate']);
$user->d = $user->d + ($this->d * $this->server['rate']);
2023-04-15 19:34:54 +08:00
if (!$user->save()) {
info("流量更新失败\n未记录用户ID:{$this->userId}\n未记录上行:{$user->u}\n未记录下行:{$user->d}");
}
2021-08-31 23:55:07 +08:00
}
}