更新
This commit is contained in:
@@ -3,10 +3,25 @@
|
||||
namespace App\Services\System;
|
||||
|
||||
use App\Models\System\Task;
|
||||
use App\Services\System\NotificationService;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TaskService
|
||||
{
|
||||
/**
|
||||
* @var NotificationService
|
||||
*/
|
||||
protected $notificationService;
|
||||
|
||||
/**
|
||||
* TaskService constructor
|
||||
*/
|
||||
public function __construct(NotificationService $notificationService)
|
||||
{
|
||||
$this->notificationService = $notificationService;
|
||||
}
|
||||
|
||||
public function getList(array $params): array
|
||||
{
|
||||
$query = Task::query();
|
||||
@@ -144,6 +159,9 @@ class TaskService
|
||||
'last_output' => substr($output, 0, 10000),
|
||||
]);
|
||||
|
||||
// 发送任务执行结果通知
|
||||
$this->sendTaskNotification($task, $status, $errorMessage, $executionTime);
|
||||
|
||||
return [
|
||||
'status' => $status,
|
||||
'output' => $output,
|
||||
@@ -164,4 +182,110 @@ class TaskService
|
||||
'inactive' => $inactive,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送任务执行结果通知
|
||||
*
|
||||
* @param Task $task
|
||||
* @param string $status
|
||||
* @param string|null $errorMessage
|
||||
* @param int $executionTime
|
||||
* @return void
|
||||
*/
|
||||
protected function sendTaskNotification(Task $task, string $status, ?string $errorMessage, int $executionTime): void
|
||||
{
|
||||
try {
|
||||
// 只对失败的任务或重要的成功任务发送通知
|
||||
// 这里可以根据实际需求调整通知策略
|
||||
|
||||
if ($status === 'error') {
|
||||
// 任务失败通知
|
||||
$title = '任务执行失败: ' . $task->name;
|
||||
$content = sprintf(
|
||||
"任务 %s 执行失败,错误信息:%s\n执行时间:%d 毫秒",
|
||||
$task->name,
|
||||
$errorMessage ?: '未知错误',
|
||||
$executionTime
|
||||
);
|
||||
|
||||
// 获取管理员用户ID列表
|
||||
$adminUserIds = $this->getAdminUserIds();
|
||||
|
||||
if (!empty($adminUserIds)) {
|
||||
$this->notificationService->sendToUsers(
|
||||
$adminUserIds,
|
||||
$title,
|
||||
$content,
|
||||
\App\Models\System\Notification::TYPE_ERROR,
|
||||
\App\Models\System\Notification::CATEGORY_TASK,
|
||||
[
|
||||
'task_id' => $task->id,
|
||||
'task_name' => $task->name,
|
||||
'command' => $task->command,
|
||||
'error_message' => $errorMessage,
|
||||
'execution_time' => $executionTime,
|
||||
'last_run_at' => $task->last_run_at?->toDateTimeString()
|
||||
]
|
||||
);
|
||||
}
|
||||
} elseif ($executionTime > 60000) {
|
||||
// 执行时间超过1分钟的成功任务,发送通知
|
||||
$title = '任务执行完成: ' . $task->name;
|
||||
$content = sprintf(
|
||||
"任务 %s 执行成功,耗时:%.2f 秒",
|
||||
$task->name,
|
||||
$executionTime / 1000
|
||||
);
|
||||
|
||||
$adminUserIds = $this->getAdminUserIds();
|
||||
|
||||
if (!empty($adminUserIds)) {
|
||||
$this->notificationService->sendToUsers(
|
||||
$adminUserIds,
|
||||
$title,
|
||||
$content,
|
||||
\App\Models\System\Notification::TYPE_SUCCESS,
|
||||
\App\Models\System\Notification::CATEGORY_TASK,
|
||||
[
|
||||
'task_id' => $task->id,
|
||||
'task_name' => $task->name,
|
||||
'execution_time' => $executionTime,
|
||||
'last_run_at' => $task->last_run_at?->toDateTimeString()
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('发送任务通知失败', [
|
||||
'task_id' => $task->id,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员用户ID列表
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAdminUserIds(): array
|
||||
{
|
||||
try {
|
||||
// 获取拥有管理员权限的用户
|
||||
// 这里可以根据实际业务逻辑调整,例如获取特定角色的用户
|
||||
$adminUserIds = \App\Models\Auth\User::where('status', 1)
|
||||
->whereHas('roles', function ($query) {
|
||||
$query->where('name', 'admin');
|
||||
})
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
return $adminUserIds;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('获取管理员用户列表失败', [
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user