add request log command
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Model\ApiRequestLog;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
#[Command]
|
||||
class CleanRequestLogsCommand extends HyperfCommand
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('log:clean-requests');
|
||||
}
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
$this->setDescription('清理过期的 API 请求日志');
|
||||
$this->addOption('days', 'd', InputOption::VALUE_OPTIONAL, '保留天数', 90);
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$days = (int) $this->input->getOption('days');
|
||||
$cutoff = Carbon::now()->subDays($days);
|
||||
|
||||
$deleted = ApiRequestLog::query()
|
||||
->where('created_at', '<', $cutoff)
|
||||
->delete();
|
||||
|
||||
$this->info("已清理 {$deleted} 条超过 {$days} 天的请求日志。");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user