#feature: add Jenkins deploy monitor & log clean task

This commit is contained in:
2026-01-19 11:46:38 +08:00
parent 381d5e6e49
commit da3b05b7c0
22 changed files with 968 additions and 80 deletions

View File

@@ -0,0 +1,72 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Carbon\Carbon;
class CleanScheduledTaskLogsCommand extends Command
{
protected $signature = 'logs:clean-scheduled-tasks {--days=7 : 保留最近几天的日志}';
protected $description = '清理定时任务日志文件,删除指定天数之前的日志';
public function handle(): int
{
$days = (int) $this->option('days');
$logPath = storage_path('logs/scheduled-tasks');
if (!File::exists($logPath)) {
$this->info('日志目录不存在,无需清理');
return Command::SUCCESS;
}
$cutoffDate = Carbon::now()->subDays($days);
$this->info("开始清理 {$days} 天前的定时任务日志...");
$this->info("截止日期: {$cutoffDate->format('Y-m-d')}");
$files = File::files($logPath);
$deletedCount = 0;
$totalSize = 0;
foreach ($files as $file) {
$filename = $file->getFilename();
// 匹配日志文件名格式: task-name-YYYY-MM-DD.log
if (preg_match('/-(\d{4}-\d{2}-\d{2})\.log$/', $filename, $matches)) {
$fileDate = Carbon::parse($matches[1]);
if ($fileDate->lt($cutoffDate)) {
$fileSize = $file->getSize();
$totalSize += $fileSize;
File::delete($file->getPathname());
$deletedCount++;
$this->line("已删除: {$filename} (" . $this->formatBytes($fileSize) . ")");
}
}
}
if ($deletedCount > 0) {
$this->info("清理完成!共删除 {$deletedCount} 个日志文件,释放空间: " . $this->formatBytes($totalSize));
} else {
$this->info('没有需要清理的日志文件');
}
return Command::SUCCESS;
}
private function formatBytes(int $bytes): string
{
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
}
return $bytes . ' B';
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Console\Commands;
use App\Services\JenkinsMonitorService;
use Illuminate\Console\Command;
class JenkinsMonitorCommand extends Command
{
protected $signature = 'jenkins:monitor';
protected $description = '轮询 Jenkins 检查新构建并发送钉钉通知';
public function handle(JenkinsMonitorService $service): void
{
$this->info('开始检查 Jenkins 构建...');
$results = $service->checkAllProjects();
if (isset($results['skipped'])) {
$this->warn('跳过检查: ' . ($results['reason'] ?? 'unknown'));
return;
}
foreach ($results as $slug => $result) {
if (isset($result['skipped'])) {
$this->line(sprintf('[%s] 跳过: %s', $slug, $result['reason'] ?? 'unknown'));
continue;
}
$newBuilds = $result['new_builds'] ?? [];
if (empty($newBuilds)) {
$this->line(sprintf('[%s] 无新构建', $slug));
} else {
$this->info(sprintf('[%s] 发现 %d 个新构建: #%s', $slug, count($newBuilds), implode(', #', $newBuilds)));
}
}
$this->info('检查完成');
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands;
use App\Services\ScheduledTaskService;
use Illuminate\Console\Command;
class ScheduledTaskRefreshCommand extends Command
{
protected $signature = 'scheduled-task:refresh';
protected $description = '刷新定时任务列表,同步 console.php 中的任务配置到数据库';
public function handle(ScheduledTaskService $taskService): int
{
try {
$this->info('开始刷新定时任务列表...');
$tasks = $taskService->getAllTasks();
$this->info(sprintf('成功刷新 %d 个定时任务', count($tasks)));
// 显示任务列表
$this->table(
['任务名称', '描述', '执行频率', '状态'],
array_map(fn($task) => [
$task['name'],
$task['description'],
$task['frequency'],
$task['enabled'] ? '已启用' : '已禁用',
], $tasks)
);
return Command::SUCCESS;
} catch (\Exception $e) {
$this->error("刷新失败: {$e->getMessage()}");
return Command::FAILURE;
}
}
}