fix: optimize stat jobs to prevent SQLite lock issues

This commit is contained in:
xboard 2025-01-26 01:07:42 +08:00
parent e433b5f7da
commit e858a7c6db
2 changed files with 64 additions and 36 deletions

View File

@ -10,6 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class StatServerJob implements ShouldQueue class StatServerJob implements ShouldQueue
{ {
@ -22,6 +23,15 @@ class StatServerJob implements ShouldQueue
public $tries = 3; public $tries = 3;
public $timeout = 60; public $timeout = 60;
public $maxExceptions = 3;
/**
* Calculate the number of seconds to wait before retrying the job.
*/
public function backoff(): array
{
return [1, 5, 10];
}
/** /**
* Create a new job instance. * Create a new job instance.
@ -40,7 +50,6 @@ class StatServerJob implements ShouldQueue
*/ */
public function handle(): void public function handle(): void
{ {
// Calculate record timestamp
$recordAt = $this->recordType === 'm' $recordAt = $this->recordType === 'm'
? strtotime(date('Y-m-01')) ? strtotime(date('Y-m-01'))
: strtotime(date('Y-m-d')); : strtotime(date('Y-m-d'));
@ -51,19 +60,20 @@ class StatServerJob implements ShouldQueue
$u += $traffic[0]; $u += $traffic[0];
$d += $traffic[1]; $d += $traffic[1];
} }
DB::transaction(function () use ($u, $d, $recordAt) {
$stat = StatServer::lockForUpdate()
->where('record_at', $recordAt)
->where('server_id', $this->server['id'])
->where('server_type', $this->protocol)
->where('record_type', $this->recordType)
->first();
if ($stat) { try {
$stat->u += $u; DB::transaction(function () use ($u, $d, $recordAt) {
$stat->d += $d; $affected = StatServer::where([
$stat->save(); 'record_at' => $recordAt,
} else { '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([ StatServer::create([
'record_at' => $recordAt, 'record_at' => $recordAt,
'server_id' => $this->server['id'], 'server_id' => $this->server['id'],
@ -73,6 +83,10 @@ class StatServerJob implements ShouldQueue
'd' => $d, 'd' => $d,
]); ]);
} }
}); }, 3);
} catch (\Exception $e) {
Log::error('StatServerJob failed for server ' . $this->server['id'] . ': ' . $e->getMessage());
throw $e;
}
} }
} }

View File

@ -10,6 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class StatUserJob implements ShouldQueue class StatUserJob implements ShouldQueue
{ {
@ -22,6 +23,15 @@ class StatUserJob implements ShouldQueue
public $tries = 3; public $tries = 3;
public $timeout = 60; public $timeout = 60;
public $maxExceptions = 3;
/**
* Calculate the number of seconds to wait before retrying the job.
*/
public function backoff(): array
{
return [1, 5, 10];
}
/** /**
* Create a new job instance. * Create a new job instance.
@ -40,24 +50,24 @@ class StatUserJob implements ShouldQueue
*/ */
public function handle(): void public function handle(): void
{ {
// Calculate record timestamp
$recordAt = $this->recordType === 'm' $recordAt = $this->recordType === 'm'
? strtotime(date('Y-m-01')) ? strtotime(date('Y-m-01'))
: strtotime(date('Y-m-d')); : strtotime(date('Y-m-d'));
foreach ($this->data as $uid => $v) { foreach ($this->data as $uid => $v) {
try {
DB::transaction(function () use ($uid, $v, $recordAt) { DB::transaction(function () use ($uid, $v, $recordAt) {
$stat = StatUser::lockForUpdate() $affected = StatUser::where([
->where('user_id', $uid) 'user_id' => $uid,
->where('server_rate', $this->server['rate']) 'server_rate' => $this->server['rate'],
->where('record_at', $recordAt) 'record_at' => $recordAt,
->where('record_type', $this->recordType) 'record_type' => $this->recordType,
->first(); ])->update([
if ($stat) { 'u' => DB::raw('u + ' . ($v[0] * $this->server['rate'])),
$stat->u += ($v[0] * $this->server['rate']); 'd' => DB::raw('d + ' . ($v[1] * $this->server['rate'])),
$stat->d += ($v[1] * $this->server['rate']); ]);
$stat->save();
} else { if (!$affected) {
StatUser::create([ StatUser::create([
'user_id' => $uid, 'user_id' => $uid,
'server_rate' => $this->server['rate'], 'server_rate' => $this->server['rate'],
@ -67,7 +77,11 @@ class StatUserJob implements ShouldQueue
'd' => ($v[1] * $this->server['rate']), 'd' => ($v[1] * $this->server['rate']),
]); ]);
} }
}); }, 3);
} catch (\Exception $e) {
Log::error('StatUserJob failed for user ' . $uid . ': ' . $e->getMessage());
throw $e;
}
} }
} }
} }