onQueue('stat'); $this->data = $data; $this->server = $server; $this->protocol = $protocol; $this->recordType = $recordType; } /** * Execute the job. */ public function handle(): void { $recordAt = $this->recordType === 'm' ? strtotime(date('Y-m-01')) : strtotime(date('Y-m-d')); // Aggregate traffic data $u = $d = 0; foreach ($this->data as $traffic) { $u += $traffic[0]; $d += $traffic[1]; } try { DB::transaction(function () use ($u, $d, $recordAt) { $affected = StatServer::where([ 'record_at' => $recordAt, 'server_id' => $this->server['id'], 'server_type' => $this->protocol, 'record_type' => $this->recordType, ])->update([ 'u' => DB::raw('u + ' . $u), 'd' => DB::raw('d + ' . $d), ]); if (!$affected) { StatServer::create([ 'record_at' => $recordAt, 'server_id' => $this->server['id'], 'server_type' => $this->protocol, 'record_type' => $this->recordType, 'u' => $u, 'd' => $d, ]); } }, 3); } catch (\Exception $e) { Log::error('StatServerJob failed for server ' . $this->server['id'] . ': ' . $e->getMessage()); throw $e; } } }