first commit

This commit is contained in:
CN-JS-HuiBai
2026-04-07 16:54:24 +08:00
commit 2c6a38c80d
399 changed files with 42205 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Jobs;
use App\Models\User;
use App\Services\NodeSyncService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class NodeUserSyncJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 2;
public $timeout = 10;
public function __construct(
private readonly int $userId,
private readonly string $action,
private readonly ?int $oldGroupId = null
) {
$this->onQueue('node_sync');
}
public function handle(): void
{
$user = User::find($this->userId);
if ($this->action === 'updated' || $this->action === 'created') {
if ($this->oldGroupId) {
NodeSyncService::notifyUserRemovedFromGroup($this->userId, $this->oldGroupId);
}
if ($user) {
NodeSyncService::notifyUserChanged($user);
}
} elseif ($this->action === 'deleted') {
if ($this->oldGroupId) {
NodeSyncService::notifyUserRemovedFromGroup($this->userId, $this->oldGroupId);
}
}
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Jobs;
use App\Models\Order;
use App\Services\OrderService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class OrderHandleJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $order;
protected $tradeNo;
public $tries = 3;
public $timeout = 5;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($tradeNo)
{
$this->onQueue('order_handle');
$this->tradeNo = $tradeNo;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$order = Order::where('trade_no', $this->tradeNo)
->lockForUpdate()
->first();
if (!$order) return;
$orderService = new OrderService($order);
switch ($order->status) {
// cancel
case Order::STATUS_PENDING:
if ($order->created_at <= (time() - 3600 * 2)) {
$orderService->cancel();
}
break;
case Order::STATUS_PROCESSING:
$orderService->open();
break;
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Jobs;
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;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $params;
public $tries = 3;
public $timeout = 10;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($params, $queue = 'send_email')
{
$this->onQueue($queue);
$this->params = $params;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$mailLog = MailService::sendEmail($this->params);
if ($mailLog['error']) {
$this->release(); //发送失败将触发重试
}
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Jobs;
use App\Services\TelegramService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendTelegramJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $telegramId;
protected $text;
public $tries = 3;
public $timeout = 10;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(int $telegramId, string $text)
{
$this->onQueue('send_telegram');
$this->telegramId = $telegramId;
$this->text = $text;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$telegramService = new TelegramService();
$telegramService->sendMessage($this->telegramId, $this->text, 'markdown');
}
}

View File

@@ -0,0 +1,174 @@
<?php
namespace App\Jobs;
use App\Models\Server;
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;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class StatServerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected array $data;
protected array $server;
protected string $protocol;
protected string $recordType;
public $tries = 3;
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.
*/
public function __construct(array $server, array $data, $protocol, string $recordType = 'd')
{
$this->onQueue('stat');
$this->data = $data;
$this->server = $server;
$this->protocol = $protocol;
$this->recordType = $recordType;
}
public function handle(): void
{
$recordAt = $this->recordType === 'm'
? strtotime(date('Y-m-01'))
: strtotime(date('Y-m-d'));
$u = $d = 0;
foreach ($this->data as $traffic) {
$u += $traffic[0];
$d += $traffic[1];
}
try {
$this->processServerStat($u, $d, $recordAt);
$this->updateServerTraffic($u, $d);
} catch (\Exception $e) {
Log::error('StatServerJob failed for server ' . $this->server['id'] . ': ' . $e->getMessage());
throw $e;
}
}
protected function updateServerTraffic(int $u, int $d): void
{
DB::table('v2_server')
->where('id', $this->server['id'])
->incrementEach(
['u' => $u, 'd' => $d],
['updated_at' => Carbon::now()]
);
}
protected function processServerStat(int $u, int $d, int $recordAt): void
{
$driver = config('database.default');
if ($driver === 'sqlite') {
$this->processServerStatForSqlite($u, $d, $recordAt);
} elseif ($driver === 'pgsql') {
$this->processServerStatForPostgres($u, $d, $recordAt);
} else {
$this->processServerStatForOtherDatabases($u, $d, $recordAt);
}
}
protected function processServerStatForSqlite(int $u, int $d, int $recordAt): void
{
DB::transaction(function () use ($u, $d, $recordAt) {
$existingRecord = StatServer::where([
'record_at' => $recordAt,
'server_id' => $this->server['id'],
'server_type' => $this->protocol,
'record_type' => $this->recordType,
])->first();
if ($existingRecord) {
$existingRecord->update([
'u' => $existingRecord->u + $u,
'd' => $existingRecord->d + $d,
'updated_at' => time(),
]);
} else {
StatServer::create([
'record_at' => $recordAt,
'server_id' => $this->server['id'],
'server_type' => $this->protocol,
'record_type' => $this->recordType,
'u' => $u,
'd' => $d,
'created_at' => time(),
'updated_at' => time(),
]);
}
}, 3);
}
protected function processServerStatForOtherDatabases(int $u, int $d, int $recordAt): void
{
StatServer::upsert(
[
'record_at' => $recordAt,
'server_id' => $this->server['id'],
'server_type' => $this->protocol,
'record_type' => $this->recordType,
'u' => $u,
'd' => $d,
'created_at' => time(),
'updated_at' => time(),
],
['server_id', 'server_type', 'record_at', 'record_type'],
[
'u' => DB::raw("u + VALUES(u)"),
'd' => DB::raw("d + VALUES(d)"),
'updated_at' => time(),
]
);
}
/**
* PostgreSQL upsert with arithmetic increments using ON CONFLICT ... DO UPDATE
*/
protected function processServerStatForPostgres(int $u, int $d, int $recordAt): void
{
$table = (new StatServer())->getTable();
$now = time();
// Use parameter binding to avoid SQL injection and keep maintainability
$sql = "INSERT INTO {$table} (record_at, server_id, server_type, record_type, u, d, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (server_id, server_type, record_at)
DO UPDATE SET
u = {$table}.u + EXCLUDED.u,
d = {$table}.d + EXCLUDED.d,
updated_at = EXCLUDED.updated_at";
DB::statement($sql, [
$recordAt,
$this->server['id'],
$this->protocol,
$this->recordType,
$u,
$d,
$now,
$now,
]);
}
}

View File

@@ -0,0 +1,158 @@
<?php
namespace App\Jobs;
use App\Models\StatUser;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class StatUserJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected array $data;
protected array $server;
protected string $protocol;
protected string $recordType;
public $tries = 3;
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.
*/
public function __construct(array $server, array $data, string $protocol, string $recordType = 'd')
{
$this->onQueue('stat');
$this->data = $data;
$this->server = $server;
$this->protocol = $protocol;
$this->recordType = $recordType;
}
public function handle(): void
{
$recordAt = $this->recordType === 'm'
? strtotime(date('Y-m-01'))
: strtotime(date('Y-m-d'));
foreach ($this->data as $uid => $v) {
try {
$this->processUserStat($uid, $v, $recordAt);
} catch (\Exception $e) {
Log::error('StatUserJob failed for user ' . $uid . ': ' . $e->getMessage());
throw $e;
}
}
}
protected function processUserStat(int $uid, array $v, int $recordAt): void
{
$driver = config('database.default');
if ($driver === 'sqlite') {
$this->processUserStatForSqlite($uid, $v, $recordAt);
} elseif ($driver === 'pgsql') {
$this->processUserStatForPostgres($uid, $v, $recordAt);
} else {
$this->processUserStatForOtherDatabases($uid, $v, $recordAt);
}
}
protected function processUserStatForSqlite(int $uid, array $v, int $recordAt): void
{
DB::transaction(function () use ($uid, $v, $recordAt) {
$existingRecord = StatUser::where([
'user_id' => $uid,
'server_rate' => $this->server['rate'],
'record_at' => $recordAt,
'record_type' => $this->recordType,
])->first();
if ($existingRecord) {
$existingRecord->update([
'u' => $existingRecord->u + intval($v[0] * $this->server['rate']),
'd' => $existingRecord->d + intval($v[1] * $this->server['rate']),
'updated_at' => time(),
]);
} else {
StatUser::create([
'user_id' => $uid,
'server_rate' => $this->server['rate'],
'record_at' => $recordAt,
'record_type' => $this->recordType,
'u' => intval($v[0] * $this->server['rate']),
'd' => intval($v[1] * $this->server['rate']),
'created_at' => time(),
'updated_at' => time(),
]);
}
}, 3);
}
protected function processUserStatForOtherDatabases(int $uid, array $v, int $recordAt): void
{
StatUser::upsert(
[
'user_id' => $uid,
'server_rate' => $this->server['rate'],
'record_at' => $recordAt,
'record_type' => $this->recordType,
'u' => intval($v[0] * $this->server['rate']),
'd' => intval($v[1] * $this->server['rate']),
'created_at' => time(),
'updated_at' => time(),
],
['user_id', 'server_rate', 'record_at', 'record_type'],
[
'u' => DB::raw("u + VALUES(u)"),
'd' => DB::raw("d + VALUES(d)"),
'updated_at' => time(),
]
);
}
/**
* PostgreSQL upsert with arithmetic increments using ON CONFLICT ... DO UPDATE
*/
protected function processUserStatForPostgres(int $uid, array $v, int $recordAt): void
{
$table = (new StatUser())->getTable();
$now = time();
$u = intval($v[0] * $this->server['rate']);
$d = intval($v[1] * $this->server['rate']);
$sql = "INSERT INTO {$table} (user_id, server_rate, record_at, record_type, u, d, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (user_id, server_rate, record_at)
DO UPDATE SET
u = {$table}.u + EXCLUDED.u,
d = {$table}.d + EXCLUDED.d,
updated_at = EXCLUDED.updated_at";
DB::statement($sql, [
$uid,
$this->server['rate'],
$recordAt,
$this->recordType,
$u,
$d,
$now,
$now,
]);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
class TrafficFetchJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
protected $server;
protected $protocol;
protected $timestamp;
public $tries = 1;
public $timeout = 20;
public function __construct(array $server, array $data, $protocol, int $timestamp)
{
$this->onQueue('traffic_fetch');
$this->server = $server;
$this->data = $data;
$this->protocol = $protocol;
$this->timestamp = $timestamp;
}
public function handle(): void
{
$userIds = array_keys($this->data);
foreach ($this->data as $uid => $v) {
User::where('id', $uid)
->incrementEach(
[
'u' => $v[0] * $this->server['rate'],
'd' => $v[1] * $this->server['rate'],
],
['t' => time()]
);
}
if (!empty($userIds)) {
Redis::sadd('traffic:pending_check', ...$userIds);
}
}
}