Commit 2b39aaf6 by xmh

<fix> 任务处理添加分布式锁,防止互相干扰

1 parent 3778ad59
package com.viontech.fanxing.task.repository;
import com.viontech.fanxing.commons.constant.RedisKeys;
import com.viontech.fanxing.task.model.TaskData;
import com.viontech.fanxing.commons.service.RedisService;
import com.viontech.fanxing.task.model.TaskData;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.springframework.stereotype.Repository;
......@@ -37,4 +38,8 @@ public class TaskDataRedisRepository {
return taskDataRMap.remove(unid);
}
public RLock getTaskLock(String unid) {
return redisService.getLockMust("lock:task:" + unid);
}
}
......@@ -54,6 +54,8 @@ public class TaskRunner {
Collection<String> entryCollection = set.valueRange(0, true, System.currentTimeMillis(), true);
for (String taskUnid : entryCollection) {
RLock taskLock = taskDataService.getRepository().getTaskLock(taskUnid);
try {
TaskData taskData = taskDataService.getRepository().getTaskDataByUnid(taskUnid);
if (taskData == null) {
log.info("找不到对应任务,移除所有:{}", taskUnid);
......@@ -113,7 +115,9 @@ public class TaskRunner {
taskService.updateStatus(task.getId(), TaskStatus.RUNNING.val);
// 移除任务
set.remove(taskUnid);
} finally {
taskLock.forceUnlock();
}
}
} catch (Exception e) {
log.error("", e);
......
......@@ -3,6 +3,7 @@ package com.viontech.fanxing.task.service;
import com.alibaba.fastjson.JSONObject;
import com.viontech.fanxing.commons.config.VionConfig;
import com.viontech.fanxing.commons.constant.RedisKeys;
import com.viontech.fanxing.commons.constant.TaskStatus;
import com.viontech.fanxing.commons.exception.FanXingException;
import com.viontech.fanxing.commons.model.Task;
import com.viontech.fanxing.commons.service.RedisService;
......@@ -177,6 +178,9 @@ public class VAServerService {
if (taskData == null) {
throw new FanXingException("找不到对应的任务");
}
if (!TaskStatus.RUNNING.valEqual(taskData.getTask().getStatus())) {
throw new FanXingException("任务不在运行状态");
}
VaServerInfo vaServerInfo = taskDataService.taskRunOn(taskUnid);
if (vaServerInfo != null) {
if (vaServerInfo.getStatus() == 0) {
......
......@@ -26,6 +26,7 @@ import com.viontech.fanxing.task.service.VAServerService;
import com.viontech.fanxing.task.service.adapter.TaskService;
import com.viontech.fanxing.task.utils.SceneUtils;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -179,8 +180,13 @@ public class TaskServiceImpl extends BaseServiceImpl<Task> implements TaskServic
@Transactional(rollbackFor = Exception.class)
public void removeTask(Long id) {
Task task = selectByPrimaryKey(id);
RLock taskLock = taskDataService.getRepository().getTaskLock(task.getUnid());
try {
taskDataService.deleteTask(task.getUnid());
deleteByPrimaryKey(id);
} finally {
taskLock.forceUnlock();
}
}
/**
......@@ -208,9 +214,16 @@ public class TaskServiceImpl extends BaseServiceImpl<Task> implements TaskServic
if (task.getStoreConfigId() == null) {
throw new IllegalArgumentException("存储配置为空,无法执行");
}
RLock taskLock = taskDataService.getRepository().getTaskLock(task.getUnid());
try {
updateStatus(id, TaskStatus.PAUSE.val);
taskDataService.addTask(task);
opsClientService.addLog("启动任务:" + task.getName());
} finally {
taskLock.forceUnlock();
}
}
/**
......@@ -220,9 +233,14 @@ public class TaskServiceImpl extends BaseServiceImpl<Task> implements TaskServic
@Override
public void stopTask(Long id) {
Task task = selectByPrimaryKey(id);
RLock taskLock = taskDataService.getRepository().getTaskLock(task.getUnid());
try {
taskDataService.deleteTask(task.getUnid());
updateStatus(id, TaskStatus.PAUSE.val);
opsClientService.addLog("停止任务:" + task.getName());
} finally {
taskLock.forceUnlock();
}
}
@Override
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!