mirror of
https://gitee.com/mateos/mateclaw.git
synced 2026-09-15 20:08:18 +08:00
fix(cron): include agent-less cron jobs in dashboard run views (#134)
This commit is contained in:
parent
3b9b4d79d5
commit
51c922f395
@ -4,8 +4,6 @@ import io.swagger.v3.oas.annotations.Operation;
|
|||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.*;
|
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.common.result.R;
|
||||||
import vip.mate.cron.model.CronJobEntity;
|
import vip.mate.cron.model.CronJobEntity;
|
||||||
import vip.mate.cron.repository.CronJobMapper;
|
import vip.mate.cron.repository.CronJobMapper;
|
||||||
@ -32,7 +30,6 @@ public class DashboardController {
|
|||||||
private final DashboardService dashboardService;
|
private final DashboardService dashboardService;
|
||||||
private final CronJobRunService cronJobRunService;
|
private final CronJobRunService cronJobRunService;
|
||||||
private final CronJobMapper cronJobMapper;
|
private final CronJobMapper cronJobMapper;
|
||||||
private final AgentService agentService;
|
|
||||||
|
|
||||||
@Operation(summary = "获取概览统计")
|
@Operation(summary = "获取概览统计")
|
||||||
@GetMapping("/overview")
|
@GetMapping("/overview")
|
||||||
@ -58,15 +55,14 @@ public class DashboardController {
|
|||||||
@PathVariable Long cronJobId,
|
@PathVariable Long cronJobId,
|
||||||
@RequestHeader(value = "X-Workspace-Id", required = false) Long workspaceId,
|
@RequestHeader(value = "X-Workspace-Id", required = false) Long workspaceId,
|
||||||
@RequestParam(defaultValue = "20") int limit) {
|
@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);
|
CronJobEntity job = cronJobMapper.selectById(cronJobId);
|
||||||
if (job != null && job.getAgentId() != null) {
|
if (job != null && job.getWorkspaceId() != null) {
|
||||||
AgentEntity agent = agentService.getAgent(job.getAgentId());
|
long wsId = workspaceId != null ? workspaceId : 1L;
|
||||||
if (agent != null && agent.getWorkspaceId() != null) {
|
if (!job.getWorkspaceId().equals(wsId)) {
|
||||||
long wsId = workspaceId != null ? workspaceId : 1L;
|
throw new MateClawException("err.common.wrong_workspace", "资源不属于当前工作区");
|
||||||
if (!agent.getWorkspaceId().equals(wsId)) {
|
|
||||||
throw new MateClawException("err.common.wrong_workspace", "资源不属于当前工作区");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return R.ok(cronJobRunService.listByJobId(cronJobId, Math.min(limit, 100)));
|
return R.ok(cronJobRunService.listByJobId(cronJobId, Math.min(limit, 100)));
|
||||||
|
|||||||
@ -3,8 +3,6 @@ package vip.mate.dashboard.service;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
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.model.CronJobEntity;
|
||||||
import vip.mate.cron.repository.CronJobMapper;
|
import vip.mate.cron.repository.CronJobMapper;
|
||||||
import vip.mate.dashboard.model.ActiveCronRunVO;
|
import vip.mate.dashboard.model.ActiveCronRunVO;
|
||||||
@ -30,7 +28,6 @@ public class CronJobRunService {
|
|||||||
|
|
||||||
private final CronJobRunMapper runMapper;
|
private final CronJobRunMapper runMapper;
|
||||||
private final CronJobMapper cronJobMapper;
|
private final CronJobMapper cronJobMapper;
|
||||||
private final AgentMapper agentMapper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 记录一次执行开始
|
* 记录一次执行开始
|
||||||
@ -95,26 +92,21 @@ public class CronJobRunService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询指定 workspace 关联的最近执行记录
|
* 查询指定 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) {
|
public List<CronJobRunEntity> listRecentByWorkspace(Long workspaceId, int limit) {
|
||||||
// 1. workspace 下的 agent IDs
|
// 1. workspace 下的 cronJob 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
|
|
||||||
List<CronJobEntity> jobs = cronJobMapper.selectList(
|
List<CronJobEntity> jobs = cronJobMapper.selectList(
|
||||||
new LambdaQueryWrapper<CronJobEntity>()
|
new LambdaQueryWrapper<CronJobEntity>()
|
||||||
.in(CronJobEntity::getAgentId, agentIds)
|
.eq(CronJobEntity::getWorkspaceId, workspaceId)
|
||||||
.select(CronJobEntity::getId));
|
.select(CronJobEntity::getId));
|
||||||
if (jobs.isEmpty()) return Collections.emptyList();
|
if (jobs.isEmpty()) return Collections.emptyList();
|
||||||
Set<Long> jobIds = jobs.stream().map(CronJobEntity::getId).collect(Collectors.toSet());
|
Set<Long> jobIds = jobs.stream().map(CronJobEntity::getId).collect(Collectors.toSet());
|
||||||
|
|
||||||
// 3. 这些 cronJob 的执行记录
|
// 2. 这些 cronJob 的执行记录
|
||||||
return runMapper.selectList(
|
return runMapper.selectList(
|
||||||
new LambdaQueryWrapper<CronJobRunEntity>()
|
new LambdaQueryWrapper<CronJobRunEntity>()
|
||||||
.in(CronJobRunEntity::getCronJobId, jobIds)
|
.in(CronJobRunEntity::getCronJobId, jobIds)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user