119 lines
4.7 KiB
PHP
119 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace app\common\service\crontab;
|
|
|
|
use think\facade\Db;
|
|
|
|
class CrontabAlertService
|
|
{
|
|
private static bool $tableEnsured = false;
|
|
|
|
public static function ensureTable(): void
|
|
{
|
|
if (self::$tableEnsured) {
|
|
return;
|
|
}
|
|
|
|
$prefix = config('database.connections.mysql.prefix');
|
|
$table = $prefix . 'crontab_alert';
|
|
Db::execute(
|
|
"CREATE TABLE IF NOT EXISTS `{$table}` (
|
|
`id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
`crontab_id` INT NOT NULL DEFAULT 0,
|
|
`crontab_log_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
|
|
`task_name` VARCHAR(100) NOT NULL DEFAULT '',
|
|
`command` VARCHAR(100) NOT NULL DEFAULT '',
|
|
`params` VARCHAR(255) NOT NULL DEFAULT '',
|
|
`alert_type` VARCHAR(50) NOT NULL DEFAULT '',
|
|
`http_status` INT NOT NULL DEFAULT 0,
|
|
`detail` LONGTEXT NULL,
|
|
`dedupe_key` VARCHAR(191) NOT NULL DEFAULT '',
|
|
`status` VARCHAR(20) NOT NULL DEFAULT 'open',
|
|
`first_seen_at` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`last_seen_at` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`last_notified_at` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`resolved_at` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`notify_count` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`create_time` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
`update_time` INT UNSIGNED NOT NULL DEFAULT 0,
|
|
UNIQUE KEY `uk_dedupe_key` (`dedupe_key`),
|
|
KEY `idx_status_notify` (`status`, `last_notified_at`),
|
|
KEY `idx_crontab_status` (`crontab_id`, `status`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='定时任务告警'"
|
|
);
|
|
self::$tableEnsured = true;
|
|
}
|
|
|
|
public static function resolveByCrontabId(int $crontabId): void
|
|
{
|
|
self::ensureTable();
|
|
$now = time();
|
|
Db::name('crontab_alert')
|
|
->where('crontab_id', $crontabId)
|
|
->where('status', 'open')
|
|
->update([
|
|
'status' => 'resolved',
|
|
'resolved_at' => $now,
|
|
'update_time' => $now,
|
|
]);
|
|
}
|
|
|
|
public static function upsertCommandException(array $item, string $errorMessage, int $crontabLogId = 0): void
|
|
{
|
|
self::ensureTable();
|
|
$now = time();
|
|
$dedupeKey = self::buildDedupeKey((int) ($item['id'] ?? 0), 'command_exception');
|
|
$detail = json_encode([
|
|
'summary_text' => mb_substr($errorMessage, 0, 1000),
|
|
'error_message' => mb_substr($errorMessage, 0, 1000),
|
|
'output_excerpt' => mb_substr($errorMessage, 0, 2000),
|
|
], JSON_UNESCAPED_UNICODE);
|
|
|
|
$existing = Db::name('crontab_alert')->where('dedupe_key', $dedupeKey)->find();
|
|
if ($existing) {
|
|
$update = [
|
|
'crontab_log_id' => $crontabLogId,
|
|
'task_name' => mb_substr((string) ($item['name'] ?? ''), 0, 100),
|
|
'command' => mb_substr((string) ($item['command'] ?? ''), 0, 100),
|
|
'params' => mb_substr((string) ($item['params'] ?? ''), 0, 255),
|
|
'alert_type' => 'command_exception',
|
|
'http_status' => 0,
|
|
'detail' => $detail,
|
|
'last_seen_at' => $now,
|
|
'update_time' => $now,
|
|
];
|
|
if (($existing['status'] ?? '') === 'resolved') {
|
|
$update['status'] = 'open';
|
|
$update['first_seen_at'] = $now;
|
|
$update['last_notified_at'] = 0;
|
|
$update['resolved_at'] = 0;
|
|
$update['notify_count'] = 0;
|
|
}
|
|
Db::name('crontab_alert')->where('id', $existing['id'])->update($update);
|
|
return;
|
|
}
|
|
|
|
Db::name('crontab_alert')->insert([
|
|
'crontab_id' => (int) ($item['id'] ?? 0),
|
|
'crontab_log_id' => $crontabLogId,
|
|
'task_name' => mb_substr((string) ($item['name'] ?? ''), 0, 100),
|
|
'command' => mb_substr((string) ($item['command'] ?? ''), 0, 100),
|
|
'params' => mb_substr((string) ($item['params'] ?? ''), 0, 255),
|
|
'alert_type' => 'command_exception',
|
|
'http_status' => 0,
|
|
'detail' => $detail,
|
|
'dedupe_key' => $dedupeKey,
|
|
'status' => 'open',
|
|
'first_seen_at' => $now,
|
|
'last_seen_at' => $now,
|
|
'create_time' => $now,
|
|
'update_time' => $now,
|
|
]);
|
|
}
|
|
|
|
private static function buildDedupeKey(int $crontabId, string $alertType): string
|
|
{
|
|
return 'crontab:' . $crontabId . ':' . $alertType;
|
|
}
|
|
}
|