fix(cron): include agent-less cron jobs in dashboard run views (#134)

This commit is contained in:
matevip 2026-05-15 20:56:21 +08:00
parent 3b9b4d79d5
commit 51c922f395
2 changed files with 14 additions and 26 deletions

View File

@ -4,8 +4,6 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vip.mate.agent.AgentService;
import vip.mate.agent.model.AgentEntity;
import vip.mate.common.result.R;
import vip.mate.cron.model.CronJobEntity;
import vip.mate.cron.repository.CronJobMapper;
@ -32,7 +30,6 @@ public class DashboardController {
private final DashboardService dashboardService;
private final CronJobRunService cronJobRunService;
private final CronJobMapper cronJobMapper;
private final AgentService agentService;
@Operation(summary = "获取概览统计")
@GetMapping("/overview")
@ -58,15 +55,14 @@ public class DashboardController {
@PathVariable Long cronJobId,
@RequestHeader(value = "X-Workspace-Id", required = false) Long workspaceId,
@RequestParam(defaultValue = "20") int limit) {
// 校验 cronJobId 对应的 agent 属于当前 workspace
// Verify the cron job belongs to the caller's workspace. Checked
// against the job's own workspace_id so agent-less system jobs
// (e.g. wiki_process) verify the same way as agent-bound jobs.
CronJobEntity job = cronJobMapper.selectById(cronJobId);
if (job != null && job.getAgentId() != null) {
AgentEntity agent = agentService.getAgent(job.getAgentId());
if (agent != null && agent.getWorkspaceId() != null) {
long wsId = workspaceId != null ? workspaceId : 1L;
if (!agent.getWorkspaceId().equals(wsId)) {
throw new MateClawException("err.common.wrong_workspace", "资源不属于当前工作区");
}
if (job != null && job.getWorkspaceId() != null) {
long wsId = workspaceId != null ? workspaceId : 1L;
if (!job.getWorkspaceId().equals(wsId)) {
throw new MateClawException("err.common.wrong_workspace", "资源不属于当前工作区");
}
}
return R.ok(cronJobRunService.listByJobId(cronJobId, Math.min(limit, 100)));

View File

@ -3,8 +3,6 @@ package vip.mate.dashboard.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.mate.agent.model.AgentEntity;
import vip.mate.agent.repository.AgentMapper;
import vip.mate.cron.model.CronJobEntity;
import vip.mate.cron.repository.CronJobMapper;
import vip.mate.dashboard.model.ActiveCronRunVO;
@ -30,7 +28,6 @@ public class CronJobRunService {
private final CronJobRunMapper runMapper;
private final CronJobMapper cronJobMapper;
private final AgentMapper agentMapper;
/**
* 记录一次执行开始
@ -95,26 +92,21 @@ public class CronJobRunService {
/**
* 查询指定 workspace 关联的最近执行记录
* 路径workspace agents cronJobs cronJobRuns
* 路径workspace cronJobs cronJobRuns
* <p>
* Cron jobs are matched by their own workspace_id, so agent-less system
* jobs (e.g. wiki_process) are included alongside agent-bound jobs.
*/
public List<CronJobRunEntity> listRecentByWorkspace(Long workspaceId, int limit) {
// 1. workspace 下的 agent IDs
List<AgentEntity> agents = agentMapper.selectList(
new LambdaQueryWrapper<AgentEntity>()
.eq(AgentEntity::getWorkspaceId, workspaceId)
.select(AgentEntity::getId));
if (agents.isEmpty()) return Collections.emptyList();
Set<Long> agentIds = agents.stream().map(AgentEntity::getId).collect(Collectors.toSet());
// 2. 这些 agent 关联的 cronJob IDs
// 1. workspace 下的 cronJob IDs
List<CronJobEntity> jobs = cronJobMapper.selectList(
new LambdaQueryWrapper<CronJobEntity>()
.in(CronJobEntity::getAgentId, agentIds)
.eq(CronJobEntity::getWorkspaceId, workspaceId)
.select(CronJobEntity::getId));
if (jobs.isEmpty()) return Collections.emptyList();
Set<Long> jobIds = jobs.stream().map(CronJobEntity::getId).collect(Collectors.toSet());
// 3. 这些 cronJob 的执行记录
// 2. 这些 cronJob 的执行记录
return runMapper.selectList(
new LambdaQueryWrapper<CronJobRunEntity>()
.in(CronJobRunEntity::getCronJobId, jobIds)