|
@@ -108,6 +108,7 @@ public class ServiceController {
|
|
|
* 控制所有服务的启动或停止
|
|
|
*
|
|
|
* @param containerCode 容器代码,标识一组服务
|
|
|
+ * --0单一竞争、1并发运行
|
|
|
* @param type 操作类型,1表示启动服务,其他值表示停止服务
|
|
|
* @return 返回操作成功的服务数量
|
|
|
*/
|
|
@@ -187,6 +188,7 @@ public class ServiceController {
|
|
|
*
|
|
|
* @param serviceCode 服务代码,用于识别特定的服务
|
|
|
* @param controllerType 服务类型,表示服务的启动方式或状态
|
|
|
+ * operating_mode int 运行模式--1单一竞争、0并发运行
|
|
|
* @return 返回一个Result对象,包含操作结果如果操作失败,会包含失败信息
|
|
|
*/
|
|
|
private Result<Integer> controllerLocalService(String serviceCode,int controllerType,String containerCode) {
|
|
@@ -202,13 +204,13 @@ public class ServiceController {
|
|
|
// 根据服务类型处理不同的启动逻辑
|
|
|
if (serviceType == 0) { // WebApi接口类型,接收数据类型
|
|
|
// 更新数据库中的服务状态为运行中
|
|
|
- return dbExecutor.update(
|
|
|
+ return dbExecutor.dbUpdate(
|
|
|
systemEnvCache.getDbInfo(),
|
|
|
serviceTableName,
|
|
|
Map.of("dataContent", Map.of(
|
|
|
"data", Map.of("run_state", controllerType),
|
|
|
"conditions", Map.of("service_code", serviceCode)
|
|
|
- ))
|
|
|
+ ), "event", "UPDATE")
|
|
|
);
|
|
|
} else { // 非WebAPI接收数据类型
|
|
|
// 获取服务的运行模式和cron表达式
|
|
@@ -346,16 +348,16 @@ public class ServiceController {
|
|
|
switch (serviceType) {
|
|
|
case 1 -> // 固定频率模式
|
|
|
future = taskScheduler.scheduleAtFixedRate(
|
|
|
- () -> executeServiceTask(serviceCode,operatingMode,serviceType),
|
|
|
+ () -> executeServiceTask(serviceCode,operatingMode),
|
|
|
Duration.ofMillis(Long.parseLong(cron_express)));
|
|
|
case 2 -> // 固定延迟模式
|
|
|
future = taskScheduler.scheduleWithFixedDelay(
|
|
|
- () -> executeServiceTask(serviceCode,operatingMode,serviceType),
|
|
|
+ () -> executeServiceTask(serviceCode,operatingMode),
|
|
|
Duration.ofMillis(Long.parseLong(cron_express))
|
|
|
);
|
|
|
case 3 -> // CRON表达式模式
|
|
|
future = taskScheduler.schedule(
|
|
|
- () -> executeServiceTask(serviceCode,operatingMode,serviceType),
|
|
|
+ () -> executeServiceTask(serviceCode,operatingMode),
|
|
|
new CronTrigger(cron_express));
|
|
|
default -> {
|
|
|
// 当运行模式不被支持时,返回错误信息
|
|
@@ -410,12 +412,17 @@ public class ServiceController {
|
|
|
for (Map<String, Object> service : result.getData()) {
|
|
|
// 检查服务的运行模式,只有单一竞争模式的服务才被处理
|
|
|
Integer mode = (Integer) service.get("operating_mode");
|
|
|
- if (mode != 0) continue;
|
|
|
+ if (mode != 1) continue;
|
|
|
|
|
|
String serviceCode = (String) service.get("service_code");
|
|
|
logger.info("单一竞争模式服务处理:{}",serviceCode);
|
|
|
-
|
|
|
- // 获取服务的心跳时间和服务所在的容器代码
|
|
|
+ Integer operatingMode = (Integer) service.get("operating_mode");
|
|
|
+ String cronExpress = (String) service.get("cron_express");
|
|
|
+ Integer serviceType = (Integer) service.get("service_type");
|
|
|
+ String currentContainer = systemEnvCache.get("container_code", String.class);
|
|
|
+ controllerScheduledTask(serviceCode, operatingMode,cronExpress,serviceType,1,service, currentContainer);
|
|
|
+ startService(systemEnvCache.get("container_code", String.class),serviceCode);
|
|
|
+/* // 获取服务的心跳时间和服务所在的容器代码
|
|
|
LocalDateTime heartbeatTime = (LocalDateTime) service.get("heartbeat_time");
|
|
|
String heartbeat_container_code = (String) service.get("heartbeat_container_code");
|
|
|
|
|
@@ -433,7 +440,7 @@ public class ServiceController {
|
|
|
startService(systemEnvCache.get("container_code", String.class),serviceCode);
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -491,14 +498,13 @@ public class ServiceController {
|
|
|
*
|
|
|
* @param serviceCode 服务代码,标识了需要执行的具体服务任务
|
|
|
* @param operatingMode 0单一竞争、1并发运行
|
|
|
- * @param serviceType
|
|
|
*/
|
|
|
- private synchronized void executeServiceTask(String serviceCode, int operatingMode, int serviceType) {
|
|
|
+ private synchronized void executeServiceTask(String serviceCode, int operatingMode) {
|
|
|
try {
|
|
|
// 调用数据服务处理逻辑
|
|
|
logger.info("执行服务任务:{}", serviceCode);
|
|
|
- if (operatingMode == 0){
|
|
|
- if (singleContentionHandling(serviceCode, operatingMode, serviceType)){
|
|
|
+ if (operatingMode == 1){
|
|
|
+ if (singleContentionHandling(serviceCode, operatingMode)){
|
|
|
logger.info("单一竞争成功!");
|
|
|
dataServices.process(Map.of("service_code", serviceCode));
|
|
|
}else {
|
|
@@ -552,7 +558,7 @@ public class ServiceController {
|
|
|
}
|
|
|
/**
|
|
|
* 获取服务信息
|
|
|
- *
|
|
|
+ * operatingMode 运行模式--1单一竞争、0并发运行
|
|
|
* @param serviceCode 服务代码,如果为null,则获取所有启用的服务信息
|
|
|
* @param useCache 是否使用缓存,true表示使用缓存,false表示不使用缓存
|
|
|
* @return 返回服务信息的结果对象,包含服务代码、运行模式、服务类型和cron表达式等信息
|
|
@@ -597,7 +603,7 @@ public class ServiceController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private boolean singleContentionHandling(String serviceCode, int operatingMode, int serviceType){
|
|
|
+ private boolean singleContentionHandling(String serviceCode, int operatingMode){
|
|
|
String containerCode = systemEnvCache.get("container_code", String.class); // 获取容器编码
|
|
|
Result<List<Map<String, Object>>> result = getServiceInfo(serviceCode, operatingMode, false);
|
|
|
if (result.isSuccess()&&result.getData()!=null&&result.getData().size()>0) {
|
|
@@ -615,7 +621,7 @@ public class ServiceController {
|
|
|
}
|
|
|
}
|
|
|
// 否则如果当前时间减最后活跃时间超过8秒
|
|
|
- if (Objects.nonNull(last_time)&& new Date().getTime()/1000 - last_time/1000 > 8) {
|
|
|
+ if ((Objects.nonNull(last_time)&& new Date().getTime()/1000 - last_time/1000 > 8)||lastContainerCode.equals(containerCode)) {
|
|
|
// 执行抢占
|
|
|
int num = preemptive(containerCode, serviceCode, last_time, lastContainerCode);
|
|
|
if (num > 0) {
|