You've already forked FrameTour-BE
feat(integration): 添加渲染工作器配置管理功能
- 新增 RenderWorkerConfigManager 类实现渲染工作器配置的管理功能 - 在 RenderWorkerRepository 中集成 RenderWorkerConfigManager - 添加方法 getWorkerConfigManager 获取渲染工作器配置管理器实例 - 优化 getWorkerByAccessKey 和 getWorker 方法,使用集成服务获取工作器信息
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user