v2board/app/Jobs/TrafficFetchJob.php

63 lines
1.5 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;
2021-09-02 18:58:04 +08:00
use Illuminate\Support\Facades\DB;
2021-08-31 23:55:07 +08:00
class TrafficFetchJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $u;
protected $d;
protected $userId;
protected $server;
protected $protocol;
public $tries = 3;
public $timeout = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($u, $d, $userId, $server, $protocol)
{
$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 18:58:04 +08:00
$user = User::find($this->userId);
2021-08-31 23:55:07 +08:00
if (!$user) return;
2021-09-02 18:58:04 +08:00
try {
$user->update([
't' => time(),
'u' => DB::raw("u+{$this->u}"),
'd' => DB::raw("d+{$this->d}")
]);
} catch (\Exception $e) {
throw new \Exception('流量更新失败');
}
2021-08-31 23:55:07 +08:00
$mailService = new MailService();
2021-09-02 18:58:04 +08:00
$mailService->remindTraffic($user->first());
2021-08-31 23:55:07 +08:00
}
}