Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
谢明辉
/
fanxing3
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2b39aaf6
authored
Nov 17, 2021
by
xmh
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
<fix> 任务处理添加分布式锁,防止互相干扰
1 parent
3778ad59
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
2 deletions
fanxing-task/src/main/java/com/viontech/fanxing/task/repository/TaskDataRedisRepository.java
fanxing-task/src/main/java/com/viontech/fanxing/task/runner/TaskRunner.java
fanxing-task/src/main/java/com/viontech/fanxing/task/service/VAServerService.java
fanxing-task/src/main/java/com/viontech/fanxing/task/service/impl/TaskServiceImpl.java
fanxing-task/src/main/java/com/viontech/fanxing/task/repository/TaskDataRedisRepository.java
View file @
2b39aaf
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
);
}
}
fanxing-task/src/main/java/com/viontech/fanxing/task/runner/TaskRunner.java
View file @
2b39aaf
...
...
@@ -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
);
...
...
fanxing-task/src/main/java/com/viontech/fanxing/task/service/VAServerService.java
View file @
2b39aaf
...
...
@@ -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
)
{
...
...
fanxing-task/src/main/java/com/viontech/fanxing/task/service/impl/TaskServiceImpl.java
View file @
2b39aaf
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment