mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 20:40:49 +08:00
cleanup check
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\CheckCleanup;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
@@ -37,6 +38,8 @@ class Kernel extends ConsoleKernel
|
||||
})->withoutOverlapping();
|
||||
$schedule->command('meilisearch:import')->weeklyOn(1, "03:00")->withoutOverlapping();
|
||||
$schedule->command('torrent:load_pieces_hash')->dailyAt("01:00")->withoutOverlapping();
|
||||
|
||||
$this->registerScheduleCleanup($schedule);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,4 +53,16 @@ class Kernel extends ConsoleKernel
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
|
||||
private function registerScheduleCleanup(Schedule $schedule): void
|
||||
{
|
||||
$interval = get_setting("main.autoclean_interval_one");
|
||||
if (!$interval || $interval < 60) {
|
||||
$interval = 7200;
|
||||
}
|
||||
$interval = 60;
|
||||
$schedule->job(new CheckCleanup())
|
||||
->cron(sprintf("*/%d * * * *", intval($interval/60)))
|
||||
->withoutOverlapping();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +161,10 @@ class EditSetting extends Page implements Forms\Contracts\HasForms
|
||||
->label(__('label.setting.system.access_admin_class_min'))
|
||||
->helperText(__('label.setting.system.access_admin_class_min_help'))
|
||||
,
|
||||
Forms\Components\TextInput::make('system.alarm_email_receiver')
|
||||
->label(__('label.setting.system.alarm_email_receiver'))
|
||||
->helperText(__('label.setting.system.alarm_email_receiver_help'))
|
||||
,
|
||||
])->columns(2);
|
||||
|
||||
$tabs = apply_filter('nexus_setting_tabs', $tabs);
|
||||
|
||||
@@ -59,4 +59,10 @@ class Locale
|
||||
return self::$languageMaps[$lang] ?? null;
|
||||
}
|
||||
|
||||
public static function getDefault()
|
||||
{
|
||||
$defaultLang = get_setting("main.defaultlang");
|
||||
return self::$languageMaps[$defaultLang] ?? null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
39
app/Jobs/CheckCleanup.php
Normal file
39
app/Jobs/CheckCleanup.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Console\Commands\Cleanup;
|
||||
use App\Repositories\CleanupRepository;
|
||||
use App\Repositories\ToolRepository;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class CheckCleanup implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
CleanupRepository::checkCleanup();
|
||||
do_log("CheckCleanup job run success.");
|
||||
}
|
||||
}
|
||||
8
app/Models/Avp.php
Normal file
8
app/Models/Avp.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class Avp extends NexusModel
|
||||
{
|
||||
|
||||
}
|
||||
@@ -218,7 +218,7 @@ class User extends Authenticatable implements FilamentUser, HasName
|
||||
'leechwarnuntil' => 'datetime',
|
||||
];
|
||||
|
||||
public static $commonFields = [
|
||||
public static array $commonFields = [
|
||||
'id', 'username', 'email', 'class', 'status', 'added', 'avatar', 'passkey',
|
||||
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
|
||||
'invited_by', 'enabled', 'seed_points', 'last_access', 'invites',
|
||||
|
||||
@@ -36,9 +36,6 @@ class EventServiceProvider extends ServiceProvider
|
||||
TorrentCreated::class => [
|
||||
FetchTorrentImdb::class,
|
||||
],
|
||||
UserDestroyed::class => [
|
||||
RemoveOauthTokens::class,
|
||||
],
|
||||
UserDisabled::class => [
|
||||
RemoveOauthTokens::class,
|
||||
],
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Http\Middleware\Locale;
|
||||
use App\Models\Avp;
|
||||
use App\Models\NexusModel;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
@@ -206,10 +209,80 @@ LUA;
|
||||
|
||||
private static function getCacheKeyLifeTime(): int
|
||||
{
|
||||
$four = get_setting("main.autoclean_interval_four");
|
||||
$three = get_setting("main.autoclean_interval_three");
|
||||
$one = get_setting("main.autoclean_interval_one");
|
||||
$four = self::getInterval("four");
|
||||
$three = self::getInterval("three");
|
||||
$one = self::getInterval("one");
|
||||
return intval($four) + intval($three) + intval($one);
|
||||
}
|
||||
|
||||
private static function getInterval($level): int
|
||||
{
|
||||
$name = sprintf("main.autoclean_interval_%s", $level);
|
||||
return intval(get_setting($name));
|
||||
}
|
||||
|
||||
public static function checkCleanup(): void
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$timestamp = $now->getTimestamp();
|
||||
$toolRep = new ToolRepository();
|
||||
$arvToLevel = [
|
||||
"lastcleantime" => "one",
|
||||
"lastcleantime2" => "two",
|
||||
"lastcleantime3" => "three",
|
||||
"lastcleantime4" => "four",
|
||||
"lastcleantime5" => "five",
|
||||
];
|
||||
$avps = Avp::query()->get()->keyBy("arg");
|
||||
foreach ($arvToLevel as $arg => $level) {
|
||||
/** @var NexusModel $value */
|
||||
$value = $avps->get($arg);
|
||||
$interval = self::getInterval($level);
|
||||
if ($interval <= 0) {
|
||||
do_log(sprintf("level: %s not set cleanup interval", $level), "error");
|
||||
continue;
|
||||
}
|
||||
$lastTime = 0;
|
||||
if ($value && $value->value_u) {
|
||||
$lastTime = $value->value_u;
|
||||
}
|
||||
if ($timestamp < $lastTime + $interval * 2) {
|
||||
continue;
|
||||
}
|
||||
$receiverUid = get_setting("system.alarm_email_receiver");
|
||||
if (empty($receiverUid)) {
|
||||
$locale = Locale::getDefault();
|
||||
$subject = self::getAlarmEmailSubject($locale);
|
||||
$msg = self::getAlarmEmailBody($now, $level, $lastTime, $interval, $locale);
|
||||
do_log(sprintf("%s - %s", $subject, $msg), "error");
|
||||
} else {
|
||||
$receiverUidArr = preg_split("/\s+/", $receiverUid);
|
||||
$users = User::query()->whereIn("id", $receiverUidArr)->get(User::$commonFields);
|
||||
foreach ($users as $user) {
|
||||
$locale = $user->locale;
|
||||
$subject = self::getAlarmEmailSubject($locale);
|
||||
$msg = self::getAlarmEmailBody($now, $level, $lastTime, $interval, $locale);
|
||||
$result = $toolRep->sendMail($user->email, $subject, $msg);
|
||||
do_log(sprintf("send msg: %s result: %s", $msg, var_export($result, true)), $result ? "info" : "error");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static function getAlarmEmailSubject(string|null $locale = null)
|
||||
{
|
||||
return nexus_trans("cleanup.alarm_email_subject", ["site_name" => get_setting("basic.SITENAME")], $locale);
|
||||
}
|
||||
|
||||
private static function getAlarmEmailBody(Carbon $now, string $level, int $lastTime, int $interval, string|null $locale = null)
|
||||
{
|
||||
return nexus_trans("cleanup.alarm_email_body", [
|
||||
"now_time" => $now->toDateTimeString(),
|
||||
"level" => $level,
|
||||
"last_time" => $lastTime > 0 ? Carbon::createFromTimestamp($lastTime)->toDateTimeString() : "",
|
||||
"elapsed_seconds" => $lastTime > 0 ? $now->getTimestamp() - $lastTime : "",
|
||||
"interval" => $interval,
|
||||
], $locale);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user