v2board/app/Jobs/StatServerJob.php

79 lines
2.0 KiB
PHP
Raw Normal View History

2020-12-20 16:57:33 +08:00
<?php
namespace App\Jobs;
use App\Models\StatServer;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class StatServerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2022-02-20 01:06:02 +08:00
protected $u;
protected $d;
protected $server;
protected $protocol;
protected $recordType;
2020-12-20 16:57:33 +08:00
public $tries = 3;
2022-01-22 02:06:53 +08:00
public $timeout = 60;
2020-12-20 16:57:33 +08:00
/**
* Create a new job instance.
*
* @return void
*/
2022-02-20 01:06:02 +08:00
public function __construct($u, $d, $server, $protocol, $recordType = 'd')
2020-12-20 16:57:33 +08:00
{
2022-02-20 01:06:02 +08:00
$this->onQueue('stat');
$this->u = $u;
$this->d = $d;
$this->server = $server;
$this->protocol = $protocol;
$this->recordType = $recordType;
2020-12-20 16:57:33 +08:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2022-02-20 01:06:02 +08:00
$recordAt = strtotime(date('Y-m-d'));
if ($this->recordType === 'm') {
//
}
2022-07-19 03:11:36 +08:00
$data = StatServer::lockForUpdate()
->where('record_at', $recordAt)
2022-07-29 22:02:00 +08:00
->where('server_id', $this->server['id'])
2022-06-09 15:42:26 +08:00
->where('server_type', $this->protocol)
2020-12-20 16:57:33 +08:00
->first();
if ($data) {
try {
2022-02-20 01:06:02 +08:00
$data->update([
'u' => $data['u'] + $this->u,
'd' => $data['d'] + $this->d
]);
2020-12-20 16:57:33 +08:00
} catch (\Exception $e) {
abort(500, '节点统计数据更新失败');
}
} else {
2022-02-20 01:06:02 +08:00
if (!StatServer::create([
2022-07-17 01:50:57 +08:00
'server_id' => $this->server['id'],
2022-02-20 01:06:02 +08:00
'server_type' => $this->protocol,
'u' => $this->u,
'd' => $this->d,
'record_type' => $this->recordType,
'record_at' => $recordAt
])) {
2020-12-20 16:57:33 +08:00
abort(500, '节点统计数据创建失败');
}
}
}
}