mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-03 14:10:57 +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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->string("secret")->default("")->nullable(false)->change();
|
||||
$table->string("editsecret")->default("")->nullable(false)->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.10');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-03-21');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-03-23');
|
||||
defined('IN_TRACKER') || define('IN_TRACKER', false);
|
||||
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
|
||||
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
|
||||
|
||||
@@ -309,7 +309,8 @@ class Update extends Install
|
||||
/**
|
||||
* @since 1.8.3
|
||||
*/
|
||||
if (NexusDB::hasTable('settings')) {
|
||||
$hasTableSetting = NexusDB::hasTable('settings');
|
||||
if ($hasTableSetting) {
|
||||
$updateSettings = [];
|
||||
if (get_setting("system.meilisearch_enabled") == 'yes') {
|
||||
$updateSettings["enabled"] = "yes";
|
||||
@@ -322,6 +323,16 @@ class Update extends Install
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.8.10
|
||||
*/
|
||||
if ($hasTableSetting) {
|
||||
Setting::query()->firstOrCreate(
|
||||
["name" => "system.alarm_email_receiver"],
|
||||
["value" => User::query()->where("class", User::CLASS_STAFF_LEADER)->first(["id"])->id]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function runExtraMigrate()
|
||||
|
||||
@@ -8,4 +8,6 @@ return [
|
||||
'disable_user_not_parked' => 'Disable inactive user accounts, not parked.',
|
||||
'disable_user_parked' => 'Disable inactive user accounts, parked.',
|
||||
'destroy_disabled_account' => 'Timed physical deletion of disabled accounts',
|
||||
'alarm_email_subject' => '[:site_name] background cleanup task exception',
|
||||
'alarm_email_body' => 'Current time: :now_time, level :level, Last run time was: :last_time, it has been more than: :elapsed_seconds seconds since it was run, the set run interval is: :interval seconds, please check!',
|
||||
];
|
||||
|
||||
@@ -108,6 +108,8 @@ return [
|
||||
'is_invite_pre_email_and_username_help' => "Default: 'No'. If pre-booked, email and username may not be changed when the user registers.",
|
||||
'access_admin_class_min' => 'Minimum class for logging into admin backend',
|
||||
'access_admin_class_min_help' => 'Default: administrator, users with a user class greater than or equal to the set value can log into the admin backend',
|
||||
'alarm_email_receiver' => 'Alarm email receiver',
|
||||
'alarm_email_receiver_help' => "Fill in the UID of the user, separated by space, and the alarm email will be sent to the corresponding user's email address. If you don't fill it in, it will be written to the runtime log, and the log level will be error",
|
||||
],
|
||||
],
|
||||
'user' => [
|
||||
|
||||
@@ -8,4 +8,6 @@ return [
|
||||
'disable_user_not_parked' => '定时封禁未挂起的非活跃账号.',
|
||||
'disable_user_parked' => '定时封禁已挂起的非活跃账号.',
|
||||
'destroy_disabled_account' => '定时物理删除已封禁账号',
|
||||
'alarm_email_subject' => '[:site_name]后台清理任务异常',
|
||||
'alarm_email_body' => '当前时间::now_time, 级别 :level 上次运行时间是::last_time,已经超过::elapsed_seconds 秒没有运行,设置的运行间隔是::interval 秒,请检查!',
|
||||
];
|
||||
|
||||
@@ -108,6 +108,8 @@ return [
|
||||
'is_invite_pre_email_and_username_help' => "默认: 'No'。若预定,用户注册时不可修改邮箱和用户名",
|
||||
'access_admin_class_min' => '登录管理后台最小等级',
|
||||
'access_admin_class_min_help' => '默认:管理员,用户等级大于等于设定值的用户可以登录管理后台',
|
||||
'alarm_email_receiver' => '告警邮件接收者',
|
||||
'alarm_email_receiver_help' => '填写用户 UID,多个空格隔开,系统异常告警邮件将会发到对应用户的邮箱。如果不填会写到运行日志中,日志级别为 error',
|
||||
],
|
||||
],
|
||||
'user' => [
|
||||
|
||||
@@ -8,4 +8,6 @@ return [
|
||||
'disable_user_not_parked' => '定時封禁未掛起的非活躍賬號.',
|
||||
'disable_user_parked' => '定時封禁已掛起的非活躍賬號.',
|
||||
'destroy_disabled_account' => '定時物理刪除已封禁賬號',
|
||||
'alarm_email_subject' => '[:site_name]後臺清理任務異常',
|
||||
'alarm_email_body' => '當前時間::now_time, 級別 :level 上次運行時間是::last_time,已經超過::elapsed_seconds 秒沒有運行,設置的運行間隔是::interval 秒,請檢查!',
|
||||
];
|
||||
|
||||
@@ -108,6 +108,8 @@ return [
|
||||
'is_invite_pre_email_and_username_help' => "默認: 'No'。若預定,用戶註冊時不可修改郵箱和用戶名",
|
||||
'access_admin_class_min' => '登錄管理後臺最小等級',
|
||||
'access_admin_class_min_help' => '默認:管理員,用戶等級大於等於設定值的用戶可以登錄管理後臺',
|
||||
'alarm_email_receiver' => '告警郵件接收者',
|
||||
'alarm_email_receiver_help' => '填寫用戶 UID,多個空格隔開,系統異常告警郵件將會發到對應用戶的郵箱。如果不填會寫到運行日誌中,日誌級別為 error',
|
||||
],
|
||||
],
|
||||
'user' => [
|
||||
|
||||
Reference in New Issue
Block a user