improve cleanup

This commit is contained in:
xiaomlove
2022-11-05 18:43:49 +08:00
parent 4f4026aed1
commit 2f958f2a9c
15 changed files with 283 additions and 110 deletions

View File

@@ -2,8 +2,9 @@
namespace App\Console\Commands;
use App\Jobs\CalculateSeedBonus;
use App\Jobs\UpdateSeedingLeechingTime;
use App\Jobs\CalculateUserSeedBonus;
use App\Jobs\UpdateTorrentSeedersEtc;
use App\Jobs\UpdateUserSeedingLeechingTime;
use Illuminate\Console\Command;
class Cleanup extends Command
@@ -13,14 +14,14 @@ class Cleanup extends Command
*
* @var string
*/
protected $signature = 'cleanup {--action=} {--begin_uid=} {--end_uid=}';
protected $signature = 'cleanup {--action=} {--begin_id=} {--end_id=} {--request_id=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cleanup async job trigger, options: --begin_uid, --end_uid, --action (seed_bonus, seeding_leeching_time)';
protected $description = 'Cleanup async job trigger, options: --begin_id, --end_id, --request_id, --action (seed_bonus, seeding_leeching_time, seeders_etc)';
/**
* Execute the console command.
@@ -30,15 +31,18 @@ class Cleanup extends Command
public function handle()
{
$action = $this->option('action');
$beginUid = $this->option('begin_uid');
$endUid = $this->option('end_uid');
$this->info("beginUid: $beginUid, endUid: $endUid, action: $action");
$beginId = $this->option('begin_id');
$endId = $this->option('end_id');
$commentRequestId = $this->option('request_id');
$this->info("beginId: $beginId, endId: $endId, commentRequestId: $commentRequestId, action: $action");
if ($action == 'seed_bonus') {
CalculateSeedBonus::dispatch($beginUid, $endUid);
CalculateUserSeedBonus::dispatch($beginId, $endId, $commentRequestId);
} elseif ($action == 'seeding_leeching_time') {
UpdateSeedingLeechingTime::dispatch($beginUid, $endUid);
UpdateUserSeedingLeechingTime::dispatch($beginId, $endId, $commentRequestId);
}elseif ($action == 'seeders_etc') {
UpdateTorrentSeedersEtc::dispatch($beginId, $endId, $commentRequestId);
} else {
$msg = "Invalid action: $action";
$msg = "[$commentRequestId], Invalid action: $action";
do_log($msg, 'error');
$this->error($msg);
}

View File

@@ -6,7 +6,10 @@ use App\Filament\Resources\Section\CategoryResource\Pages;
use App\Filament\Resources\Section\CategoryResource\RelationManagers;
use App\Models\Category;
use App\Models\Icon;
use App\Models\NexusModel;
use App\Models\SearchBox;
use App\Repositories\SearchBoxRepository;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
@@ -14,6 +17,7 @@ use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Collection;
class CategoryResource extends Resource
{
@@ -89,10 +93,24 @@ class CategoryResource extends Resource
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
Tables\Actions\DeleteAction::make()->using(function (NexusModel $record) {
try {
$rep = new SearchBoxRepository();
$rep->deleteCategory($record->id);
} catch (\Exception $exception) {
Filament::notify('danger', $exception->getMessage() ?: class_basename($exception));
}
}),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\DeleteBulkAction::make()->using(function (Collection $records) {
try {
$rep = new SearchBoxRepository();
$rep->deleteCategory($records->pluck('id')->toArray());
} catch (\Exception $exception) {
Filament::notify('danger', $exception->getMessage() ?: class_basename($exception));
}
}),
]);
}

View File

@@ -9,4 +9,9 @@ use Filament\Resources\Pages\CreateRecord;
class CreateCategory extends CreateRecord
{
protected static string $resource = CategoryResource::class;
protected function afterCreate()
{
clear_category_cache();
}
}

View File

@@ -5,6 +5,7 @@ namespace App\Filament\Resources\Section\CategoryResource\Pages;
use App\Filament\Resources\Section\CategoryResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;
use Nexus\Database\NexusDB;
class EditCategory extends EditRecord
{
@@ -16,4 +17,12 @@ class EditCategory extends EditRecord
Actions\DeleteAction::make(),
];
}
/**
* @see functions.php::get_category_row()
*/
protected function afterSave()
{
clear_category_cache();
}
}

View File

@@ -5,6 +5,7 @@ namespace App\Filament\Resources\Torrent;
use App\Filament\OptionsTrait;
use App\Filament\Resources\Torrent\TorrentResource\Pages;
use App\Filament\Resources\Torrent\TorrentResource\RelationManagers;
use App\Models\Category;
use App\Models\Setting;
use App\Models\Tag;
use App\Models\Torrent;
@@ -122,6 +123,9 @@ class TorrentResource extends Resource
return $query->when($data['owner'], fn (Builder $query, $owner) => $query->where("owner", $owner));
})
,
Tables\Filters\SelectFilter::make('category')
->options(Category::query()->pluck('name', 'id')->toArray())
->label(__('label.torrent.category')),
Tables\Filters\SelectFilter::make('visible')
->options(self::$yesOrNo)
@@ -381,7 +385,8 @@ class TorrentResource extends Resource
private static function shouldShowApproval(): bool
{
return Setting::get('torrent.approval_status_none_visible') == 'no' || Setting::get('torrent.approval_status_icon_enabled') == 'yes';
return false;
// return Setting::get('torrent.approval_status_none_visible') == 'no' || Setting::get('torrent.approval_status_icon_enabled') == 'yes';
}
}

View File

@@ -12,7 +12,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Nexus\Database\NexusDB;
class CalculateSeedBonus implements ShouldQueue
class CalculateUserSeedBonus implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@@ -20,15 +20,28 @@ class CalculateSeedBonus implements ShouldQueue
private int $endUid;
private string $requestId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(int $beginUid, int $endUid)
public function __construct(int $beginUid, int $endUid, string $requestId = '')
{
$this->beginUid = $beginUid;
$this->endUid = $endUid;
$this->requestId = $requestId;
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addSeconds(Setting::get('main.autoclean_interval_one'));
}
/**
@@ -39,7 +52,7 @@ class CalculateSeedBonus implements ShouldQueue
public function handle()
{
$beginTimestamp = time();
$logPrefix = sprintf("[CLEANUP_CLI_CALCULATE_SEED_BONUS], beginUid: %s, endUid: %s", $this->beginUid, $this->endUid);
$logPrefix = sprintf("[CLEANUP_CLI_CALCULATE_SEED_BONUS], commonRequestId: %s, beginUid: %s, endUid: %s", $this->requestId, $this->beginUid, $this->endUid);
$sql = sprintf("select userid from peers where userid > %s and userid <= %s and seeder = 'yes' group by userid", $this->beginUid, $this->endUid);
$results = NexusDB::select($sql);
$count = count($results);

View File

@@ -1,56 +0,0 @@
<?php
namespace App\Jobs;
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;
use Nexus\Database\NexusDB;
class UpdateSeedingLeechingTime implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private int $beginUid;
private int $endUid;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(int $beginUid, int $endUid)
{
$this->beginUid = $beginUid;
$this->endUid = $endUid;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$beginTimestamp = time();
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME], beginUid: %s, endUid: %s", $this->beginUid, $this->endUid);
$sql = sprintf("select id from users where id > %s and id <= %s and enabled = 'yes' and status = 'confirmed'", $this->beginUid, $this->endUid);
$results = NexusDB::select($sql);
do_log("$logPrefix, [GET_UID], sql: $sql, count: " . count($results));
foreach ($results as $arr) {
$uid = $arr['id'];
$sql = sprintf('select sum(seedtime) as st, sum(leechtime) as lt from snatched where userid = %s limit 1', $uid);
$row = NexusDB::select($sql);
if (is_numeric($row[0]['st'])) {
$sql = sprintf('update users set seedtime = %s, leechtime = %s where id = %s limit 1', $row[0]['st'], $row[0]['lt'], $uid);
NexusDB::statement($sql);
}
}
$costTime = time() - $beginTimestamp;
do_log("$logPrefix, [DONE], cost time: $costTime seconds");
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Jobs;
use App\Models\Setting;
use App\Models\User;
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;
use Nexus\Database\NexusDB;
class UpdateTorrentSeedersEtc implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private int $beginTorrentId;
private int $endTorrentId;
private string $requestId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(int $beginTorrentId, int $endTorrentId, string $requestId = '')
{
$this->beginTorrentId = $beginTorrentId;
$this->endTorrentId = $endTorrentId;
$this->requestId = $requestId;
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addSeconds(Setting::get('main.autoclean_interval_three'));
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$beginTimestamp = time();
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_TORRENT_SEEDERS_ETC], commonRequestId: %s, beginTorrentId: %s, endTorrentId: %s", $this->requestId, $this->beginTorrentId, $this->endTorrentId);
$sql = sprintf("update torrents set seeders = (select count(*) from peers where torrent = torrents.id and seeder = 'yes'), leechers = (select count(*) from peers where torrent = torrents.id and seeder = 'no'), comments = (select count(*) from comments where torrent = torrents.id) where id > %s and id <= %s",
$this->beginTorrentId, $this->endTorrentId
);
$result = NexusDB::statement($sql);
$costTime = time() - $beginTimestamp;
do_log(sprintf(
"$logPrefix, [DONE], sql: %s, result: %s, cost time: %s seconds",
preg_replace('/[\r\n\t]+/', ' ', $sql), var_export($result, true), $costTime
));
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Jobs;
use App\Models\Setting;
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;
use Nexus\Database\NexusDB;
class UpdateUserSeedingLeechingTime implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private int $beginUid;
private int $endUid;
private string $requestId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(int $beginUid, int $endUid, string $requestId = '')
{
$this->beginUid = $beginUid;
$this->endUid = $endUid;
$this->requestId = $requestId;
}
/**
* Determine the time at which the job should timeout.
*
* @return \DateTime
*/
public function retryUntil()
{
return now()->addSeconds(Setting::get('main.autoclean_interval_four'));
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$beginTimestamp = time();
$logPrefix = sprintf("[CLEANUP_CLI_UPDATE_SEEDING_LEECHING_TIME], commonRequestId: %s, beginUid: %s, endUid: %s", $this->requestId, $this->beginUid, $this->endUid);
$sql = sprintf(
"update users set seedtime = (select sum(seedtime) from snatched where userid = users.id), leechtime=(select sum(leechtime) from snatched where userid = users.id) where id > %s and id <= %s and status = 'confirmed' and enabled = 'yes'",
$this->beginUid, $this->endUid
);
$results = NexusDB::statement($sql);
$costTime = time() - $beginTimestamp;
do_log("$logPrefix, [DONE], sql: $sql, results: " . var_export($results, true) . " cost time: $costTime seconds");
}
}

View File

@@ -2,16 +2,21 @@
namespace App\Repositories;
use App\Exceptions\InsufficientPermissionException;
use App\Http\Middleware\Locale;
use App\Models\Category;
use App\Models\Icon;
use App\Models\NexusModel;
use App\Models\SearchBox;
use App\Models\SearchBoxField;
use App\Models\SecondIcon;
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\User;
use Elasticsearch\Endpoints\Search;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Nexus\Database\NexusDB;
use Filament\Forms;
@@ -260,5 +265,20 @@ class SearchBoxRepository extends BaseRepository
}
}
public function deleteCategory($id)
{
if (get_user_class() < User::CLASS_SYSOP) {
throw new InsufficientPermissionException();
}
$idArr = Arr::wrap($id);
$exists = Torrent::query()->whereHas('basic_category', function (\Illuminate\Database\Eloquent\Builder $query) use ($idArr) {
return $query->whereIn('id', $idArr);
})->exists();
if ($exists) {
throw new \RuntimeException("There are torrents that belong to this category and cannot be deleted!");
}
return Category::query()->whereIn('id', $idArr)->delete();
}
}