v2board/app/Jobs/StatServerJob.php

55 lines
1.3 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;
protected $statistic;
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
*/
public function __construct(array $statistic)
{
$this->onQueue('stat_server');
$this->statistic = $statistic;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$statistic = $this->statistic;
2021-09-21 18:11:14 +08:00
$data = StatServer::where('record_at', $statistic['record_at'])
2020-12-20 16:57:33 +08:00
->where('server_id', $statistic['server_id'])
->first();
if ($data) {
try {
$data->update($statistic);
} catch (\Exception $e) {
abort(500, '节点统计数据更新失败');
}
} else {
2021-09-21 18:11:14 +08:00
if (!StatServer::create($statistic)) {
2020-12-20 16:57:33 +08:00
abort(500, '节点统计数据创建失败');
}
}
}
}