feat(integration): 添加渲染工作器配置管理功能

- 新增 RenderWorkerConfigManager 类实现渲染工作器配置的管理功能
- 在 RenderWorkerRepository 中集成 RenderWorkerConfigManager
- 添加方法 getWorkerConfigManager 获取渲染工作器配置管理器实例
- 优化 getWorkerByAccessKey 和 getWorker 方法,使用集成服务获取工作器信息
This commit is contained in:
2025-09-05 14:49:19 +08:00
parent 933818d458
commit b2a95ed862
2 changed files with 136 additions and 16 deletions

View File

@@ -0,0 +1,82 @@
package com.ycwl.basic.integration.common.manager;
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
import java.util.List;
/**
* 渲染工作器配置管理器
* 基于通用ConfigManager实现渲染工作器配置的管理功能
*/
public class RenderWorkerConfigManager extends ConfigManager<RenderWorkerConfigV2DTO> {
public RenderWorkerConfigManager(List<RenderWorkerConfigV2DTO> configs) {
super(configs);
}
@Override
protected String getConfigKey(RenderWorkerConfigV2DTO config) {
return config != null ? config.getConfigKey() : null;
}
@Override
protected Object getConfigValue(RenderWorkerConfigV2DTO config) {
return config != null ? config.getConfigValue() : null;
}
/**
* 获取渲染工作器配置类型
*/
public String getConfigType(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getConfigType() : null;
}
/**
* 获取渲染工作器配置描述
*/
public String getConfigDescription(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getDescription() : null;
}
/**
* 获取渲染工作器配置ID
*/
public Long getConfigId(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getId() : null;
}
/**
* 获取工作器ID
*/
public Long getWorkerId(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getWorkerId() : null;
}
/**
* 检查配置是否启用
*/
public boolean isActive(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null && config.getIsActive() != null && config.getIsActive() == 1;
}
/**
* 获取配置的创建时间
*/
public String getCreateTime(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getCreateTime() : null;
}
/**
* 获取配置的更新时间
*/
public String getUpdateTime(String key) {
RenderWorkerConfigV2DTO config = findConfigByKey(key);
return config != null ? config.getUpdateTime() : null;
}
}

View File

@@ -4,6 +4,12 @@ import com.ycwl.basic.utils.JacksonUtil;
import com.ycwl.basic.mapper.RenderWorkerMapper;
import com.ycwl.basic.model.pc.renderWorker.entity.RenderWorkerEntity;
import com.ycwl.basic.model.task.req.ClientStatusReqVo;
import com.ycwl.basic.integration.render.dto.worker.RenderWorkerV2DTO;
import com.ycwl.basic.integration.render.service.RenderWorkerIntegrationService;
import com.ycwl.basic.integration.render.service.RenderWorkerConfigIntegrationService;
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
import com.ycwl.basic.integration.common.manager.RenderWorkerConfigManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@@ -11,40 +17,41 @@ import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class RenderWorkerRepository {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public static final String RENDER_WORKER_CACHE_KEY = "render_worker:%s";
public static final String RENDER_WORKER_STATUS_CACHE_KEY = "render_worker:host_status:%s";
public static final String RENDER_WORKER_CONFIG_CACHE_KEY = "render_worker:%s:config";
@Autowired
private RenderWorkerMapper mapper;
@Autowired
private RenderWorkerIntegrationService renderWorkerIntegrationService;
@Autowired
private RenderWorkerConfigIntegrationService renderWorkerConfigIntegrationService;
public RenderWorkerEntity getWorkerByAccessKey(String accessKey) {
String key = String.format(RENDER_WORKER_CACHE_KEY, accessKey);
if (redisTemplate.hasKey(key)) {
return JacksonUtil.parseObject(redisTemplate.opsForValue().get(key), RenderWorkerEntity.class);
}
RenderWorkerEntity renderWorker = mapper.findByAccessKey(accessKey);
if (renderWorker != null) {
redisTemplate.opsForValue().set(key, JacksonUtil.toJSONString(renderWorker), 1, TimeUnit.HOURS);
redisTemplate.opsForValue().set(String.format(RENDER_WORKER_CACHE_KEY, renderWorker.getId()), JacksonUtil.toJSONString(renderWorker), 1, TimeUnit.HOURS);
RenderWorkerV2DTO workerDTO = renderWorkerIntegrationService.getWorkerByKey(accessKey);
if (workerDTO == null) {
return null;
}
RenderWorkerEntity renderWorker = convertToEntity(workerDTO);
return renderWorker;
}
public RenderWorkerEntity getWorker(Long id) {
String key = String.format(RENDER_WORKER_CACHE_KEY, id);
if (redisTemplate.hasKey(key)) {
return JacksonUtil.parseObject(redisTemplate.opsForValue().get(key), RenderWorkerEntity.class);
}
RenderWorkerEntity renderWorker = mapper.getById(id);
if (renderWorker != null) {
redisTemplate.opsForValue().set(key, JacksonUtil.toJSONString(renderWorker), 1, TimeUnit.HOURS);
redisTemplate.opsForValue().set(String.format(RENDER_WORKER_CACHE_KEY, renderWorker.getAccessKey()), JacksonUtil.toJSONString(renderWorker), 1, TimeUnit.HOURS);
RenderWorkerV2DTO workerDTO = renderWorkerIntegrationService.getWorker(id);
if (workerDTO == null) {
return null;
}
RenderWorkerEntity renderWorker = convertToEntity(workerDTO);
return renderWorker;
}
@@ -89,4 +96,35 @@ public class RenderWorkerRepository {
}
redisTemplate.delete(String.format(RENDER_WORKER_STATUS_CACHE_KEY, id));
}
private RenderWorkerEntity convertToEntity(RenderWorkerV2DTO dto) {
if (dto == null) {
return null;
}
RenderWorkerEntity entity = new RenderWorkerEntity();
entity.setId(dto.getId());
entity.setName(dto.getName());
entity.setAccessKey(dto.getKey());
entity.setStatus(dto.getIsActive());
entity.setCreateAt(dto.getCreateTime());
entity.setUpdateAt(dto.getUpdateTime());
return entity;
}
/**
* 获取渲染工作器配置管理器
*
* @param workerId 工作器ID
* @return RenderWorkerConfigManager实例,如果获取失败返回null
*/
public RenderWorkerConfigManager getWorkerConfigManager(Long workerId) {
try {
List<RenderWorkerConfigV2DTO> configList = renderWorkerConfigIntegrationService.getWorkerConfigs(workerId);
return new RenderWorkerConfigManager(configList);
} catch (Exception e) {
log.error("获取渲染工作器配置管理器失败, workerId: {}", workerId, e);
return null;
}
}
}