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 5fc02e5f
authored
Aug 16, 2021
by
xmh
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
代码优化+重构
1 parent
9ce51e6a
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
233 additions
and
154 deletions
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/service/RedisCacheService.java → fanxing-commons/src/main/java/com/viontech/fanxing/commons/service/RedisService.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/controller/VAServerController.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/model/vaserver/VATask.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/model/vaserver/VaServerInfo.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/repository/TaskDataRedisRepository.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/repository/VAServerRedisRepository.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/runner/TaskRunner.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/service/TaskService.java
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/service/VAServerService.java
fanxing-task-scheduling/src/test/java/com/viontech/fanxing/task/scheduling/service/VAServerHttpServiceTest.java
fanxing-
task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/service/RedisCache
Service.java
→
fanxing-
commons/src/main/java/com/viontech/fanxing/commons/service/Redis
Service.java
View file @
5fc02e5
package
com
.
viontech
.
fanxing
.
task
.
scheduling
.
service
;
package
com
.
viontech
.
fanxing
.
commons
.
service
;
import
com.viontech.fanxing.commons.constant.RedisKeys
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo
;
import
org.redisson.api.*
;
import
org.springframework.stereotype.Service
;
...
...
@@ -17,7 +15,7 @@ import java.util.concurrent.TimeUnit;
*/
@Service
public
class
Redis
Cache
Service
{
public
class
RedisService
{
@Resource
private
RedissonClient
redissonClient
;
...
...
@@ -35,13 +33,6 @@ public class RedisCacheService {
return
redissonClient
.
getScoredSortedSet
(
RedisKeys
.
SCHEDULING_TO_BE_TERMINATED_TASK_UNID_SET
);
}
public
RMap
<
String
,
TaskData
>
getTaskDataMap
()
{
return
redissonClient
.
getMap
(
RedisKeys
.
SCHEDULING_TASK_DATA_MAP
);
}
public
RMap
<
String
,
VaServerInfo
>
getVaServerMap
()
{
return
redissonClient
.
getMap
(
RedisKeys
.
SCHEDULING_VASERVER_MAP
);
}
public
<
T
>
RBucket
<
T
>
getValue
(
String
key
)
{
return
redissonClient
.
getBucket
(
key
);
...
...
@@ -59,4 +50,7 @@ public class RedisCacheService {
return
lock
;
}
public
RedissonClient
getClient
()
{
return
redissonClient
;
}
}
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/controller/VAServerController.java
View file @
5fc02e5
...
...
@@ -5,11 +5,11 @@ import com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo;
import
com.viontech.fanxing.task.scheduling.service.VAServerService
;
import
com.viontech.keliu.util.JsonMessageUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.redisson.api.RMap
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
import
javax.annotation.Resource
;
import
java.util.Collection
;
import
java.util.HashMap
;
/**
...
...
@@ -39,7 +39,7 @@ public class VAServerController {
log
.
info
(
"收到注册消息:{}"
,
JSON
.
toJSONString
(
vaServerInfo
));
vaServerService
.
registeVAServer
(
vaServerInfo
);
vaServerService
.
registe
r
VAServer
(
vaServerInfo
);
HashMap
<
String
,
Object
>
result
=
new
HashMap
<>();
result
.
put
(
"code"
,
200
);
...
...
@@ -61,8 +61,8 @@ public class VAServerController {
@GetMapping
(
"/vaServerInfo"
)
public
Object
getVaServerInfo
()
{
Collection
<
VaServerInfo
>
vaServerInfo
=
vaServerService
.
getVaServerInfo
();
return
JsonMessageUtil
.
getSuccessJsonMsg
(
vaServerInfo
);
RMap
<
String
,
VaServerInfo
>
vaServerInfoMap
=
vaServerService
.
getVaServerRedisRepository
().
getVaServerInfoMap
();
return
JsonMessageUtil
.
getSuccessJsonMsg
(
vaServerInfo
Map
.
values
()
);
}
/**
...
...
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/model/vaserver/VATask.java
View file @
5fc02e5
package
com
.
viontech
.
fanxing
.
task
.
scheduling
.
model
.
vaserver
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.viontech.fanxing.commons.model.Task
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
lombok.Getter
;
...
...
@@ -27,7 +29,7 @@ public class VATask {
private
String
channel_unid
;
private
String
stream_path
;
private
Integer
stream_type
;
private
String
scene
;
private
JSONArray
scene
;
public
VATask
(
TaskData
taskData
)
{
Task
task
=
taskData
.
getTask
();
...
...
@@ -38,6 +40,6 @@ public class VATask {
this
.
channel_unid
=
task
.
getChannelUnid
();
this
.
stream_path
=
task
.
getStreamPath
();
this
.
stream_type
=
task
.
getStreamType
();
this
.
scene
=
task
.
getScene
(
);
this
.
scene
=
JSON
.
parseArray
(
task
.
getScene
()
);
}
}
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/model/vaserver/VaServerInfo.java
View file @
5fc02e5
...
...
@@ -23,7 +23,7 @@ public class VaServerInfo implements Serializable {
private
String
proxy
;
/** 可用资源数量 */
private
Float
availableResources
;
/** 状态 1在线,
2
离线 */
/** 状态 1在线,
0
离线 */
private
int
status
=
1
;
public
VaServerInfo
setVideoResource
(
Float
videoResource
)
{
...
...
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/repository/TaskDataRedisRepository.java
0 → 100644
View file @
5fc02e5
package
com
.
viontech
.
fanxing
.
task
.
scheduling
.
repository
;
import
com.viontech.fanxing.commons.constant.RedisKeys
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
com.viontech.fanxing.commons.service.RedisService
;
import
org.redisson.api.RMap
;
import
org.springframework.stereotype.Repository
;
/**
* .
*
* @author 谢明辉
* @date 2021/8/16
*/
@Repository
public
class
TaskDataRedisRepository
{
private
final
RMap
<
String
,
TaskData
>
taskDataRMap
;
private
final
RedisService
redisService
;
public
TaskDataRedisRepository
(
RedisService
redisService
)
{
this
.
taskDataRMap
=
redisService
.
getClient
().
getMap
(
RedisKeys
.
SCHEDULING_TASK_DATA_MAP
);
this
.
redisService
=
redisService
;
}
public
TaskData
getTaskDataByUnid
(
String
unid
)
{
return
taskDataRMap
.
get
(
unid
);
}
public
void
addOrUpdateTaskData
(
TaskData
data
)
{
taskDataRMap
.
put
(
data
.
getTask
().
getUnid
(),
data
);
}
public
TaskData
remove
(
String
unid
)
{
return
taskDataRMap
.
remove
(
unid
);
}
}
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/repository/VAServerRedisRepository.java
0 → 100644
View file @
5fc02e5
package
com
.
viontech
.
fanxing
.
task
.
scheduling
.
repository
;
import
com.viontech.fanxing.commons.constant.RedisKeys
;
import
com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo
;
import
com.viontech.fanxing.commons.service.RedisService
;
import
org.redisson.api.RBucket
;
import
org.redisson.api.RMap
;
import
org.springframework.stereotype.Repository
;
import
java.util.Date
;
import
java.util.Map
;
/**
* .
*
* @author 谢明辉
* @date 2021/8/16
*/
@Repository
public
class
VAServerRedisRepository
{
private
final
RMap
<
String
,
VaServerInfo
>
vaServerMap
;
private
final
RedisService
redisService
;
public
VAServerRedisRepository
(
RedisService
redisService
)
{
this
.
vaServerMap
=
redisService
.
getClient
().
getMap
(
RedisKeys
.
SCHEDULING_VASERVER_MAP
);
this
.
redisService
=
redisService
;
}
public
VaServerInfo
getVAServerInfoById
(
String
devId
)
{
VaServerInfo
vaServerInfo
=
vaServerMap
.
get
(
devId
);
if
(!
online
(
devId
))
{
vaServerInfo
.
setStatus
(
0
);
addOrUpdate
(
devId
,
vaServerInfo
);
}
return
vaServerInfo
;
}
public
RMap
<
String
,
VaServerInfo
>
getVaServerInfoMap
()
{
for
(
Map
.
Entry
<
String
,
VaServerInfo
>
entry
:
vaServerMap
.
entrySet
())
{
String
devId
=
entry
.
getKey
();
if
(!
online
(
devId
))
{
VaServerInfo
value
=
entry
.
getValue
();
value
.
setStatus
(
0
);
addOrUpdate
(
devId
,
value
);
}
}
return
vaServerMap
;
}
public
void
addOrUpdate
(
String
devId
,
VaServerInfo
vaServerInfo
)
{
vaServerMap
.
put
(
devId
,
vaServerInfo
);
}
public
boolean
online
(
String
devId
)
{
RBucket
<
Date
>
bucket
=
redisService
.
getValue
(
RedisKeys
.
getVAServerKeepAliveKey
(
devId
));
return
bucket
.
isExists
();
}
}
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/runner/TaskRunner.java
View file @
5fc02e5
...
...
@@ -4,24 +4,20 @@ import com.viontech.fanxing.commons.constant.TaskStatus;
import
com.viontech.fanxing.commons.model.Task
;
import
com.viontech.fanxing.commons.vo.TaskVo
;
import
com.viontech.fanxing.task.scheduling.feign.TaskClient
;
import
com.viontech.fanxing.task.scheduling.model.RuntimeConfig
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo
;
import
com.viontech.fanxing.
task.scheduling.service.RedisCache
Service
;
import
com.viontech.fanxing.
commons.service.Redis
Service
;
import
com.viontech.fanxing.task.scheduling.service.TaskService
;
import
com.viontech.fanxing.task.scheduling.service.VAServerService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.tuple.ImmutablePair
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RMap
;
import
org.redisson.api.RScoredSortedSet
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.Collection
;
import
java.util.concurrent.TimeUnit
;
/**
* .
...
...
@@ -35,9 +31,7 @@ import java.util.concurrent.TimeUnit;
public
class
TaskRunner
{
@Resource
private
RedissonClient
redissonClient
;
@Resource
private
RedisCacheService
redisCacheService
;
private
RedisService
redisService
;
@Resource
private
VAServerService
vaServerService
;
@Resource
...
...
@@ -47,33 +41,44 @@ public class TaskRunner {
@Scheduled
(
fixedDelay
=
5000
)
public
void
executedTaskListener
()
{
RLock
lock
=
redissonClient
.
getLock
(
"lock:executedTaskListener"
);
boolean
isLock
;
try
{
isLock
=
lock
.
tryLock
(
30
,
25
,
TimeUnit
.
SECONDS
);
}
catch
(
InterruptedException
e
)
{
return
;
}
if
(
isLock
)
{
RLock
jobLock
=
redisService
.
getLockMust
(
"lock:executedTaskListener"
);
RLock
devLock
=
null
;
try
{
RScoredSortedSet
<
String
>
set
=
redisCacheService
.
getToBeExecutedTaskUnidSet
();
RMap
<
String
,
TaskData
>
taskDataMap
=
redisCacheService
.
getTaskDataMap
();
RMap
<
String
,
VaServerInfo
>
vaServerMap
=
redisCacheService
.
getVaServerMap
();
RScoredSortedSet
<
String
>
set
=
redisService
.
getToBeExecutedTaskUnidSet
();
RMap
<
String
,
VaServerInfo
>
vaServerMap
=
vaServerService
.
getVaServerRedisRepository
().
getVaServerInfoMap
();
Collection
<
String
>
entryCollection
=
set
.
valueRange
(
0
,
true
,
System
.
currentTimeMillis
(),
true
);
for
(
String
taskUnid
:
entryCollection
)
{
log
.
info
(
"开始任务 : {}"
,
taskUnid
);
TaskData
taskData
=
taskDataMap
.
get
(
taskUnid
);
TaskData
taskData
=
taskService
.
getRepository
().
getTaskDataByUnid
(
taskUnid
);
if
(
taskData
==
null
)
{
log
.
info
(
"找不到对应任务,移除所有:{}"
,
taskUnid
);
taskService
.
removeTaskDataAll
(
taskUnid
);
continue
;
}
Task
task
=
taskData
.
getTask
();
String
taskVaType
=
task
.
getVaType
();
Float
resourceNeed
=
task
.
getResourceNeed
();
Collection
<
VaServerInfo
>
vaServerInfos
=
vaServerMap
.
values
();
Collection
<
String
>
vaServerIdSet
=
vaServerMap
.
keySet
();
// todo 暂时先找有可用资源的vaserver,以后再进行算法优化
VaServerInfo
server
=
null
;
for
(
VaServerInfo
vaServerInfo
:
vaServerInfos
)
{
if
(
vaServerInfo
.
getAvailableResources
()
>
resourceNeed
)
{
server
=
vaServerInfo
;
for
(
String
devId
:
vaServerIdSet
)
{
VaServerInfo
temp
=
vaServerMap
.
get
(
devId
);
// 不在线
if
(
temp
.
getStatus
()
==
0
)
{
continue
;
}
if
(
temp
.
getAvailableResources
()
>
resourceNeed
)
{
devLock
=
redisService
.
getLockMust
(
"lock:vaserver:"
+
devId
);
temp
=
vaServerMap
.
get
(
devId
);
if
(
temp
.
getAvailableResources
()
>
resourceNeed
)
{
server
=
temp
;
break
;
}
else
{
devLock
.
forceUnlock
();
devLock
=
null
;
}
}
}
...
...
@@ -98,57 +103,42 @@ public class TaskRunner {
}
catch
(
Exception
e
)
{
log
.
error
(
""
,
e
);
}
finally
{
try
{
lock
.
unlock
();
}
catch
(
Exception
ignore
)
{
}
if
(
devLock
!=
null
)
{
devLock
.
forceUnlock
();
}
jobLock
.
forceUnlock
();
}
}
@Scheduled
(
fixedDelay
=
5000
)
public
void
terminatedTaskListener
()
{
RLock
lock
=
redissonClient
.
getLock
(
"lock:terminatedTaskListener"
);
boolean
isLock
;
try
{
isLock
=
lock
.
tryLock
(
30
,
25
,
TimeUnit
.
SECONDS
);
}
catch
(
InterruptedException
e
)
{
return
;
}
if
(
isLock
)
{
RLock
jobLock
=
redisService
.
getLockMust
(
"lock:terminatedTaskListener"
);
try
{
RScoredSortedSet
<
String
>
set
=
redisCacheService
.
getToBeTerminatedTaskUnidSet
();
RMap
<
String
,
TaskData
>
taskDataMap
=
redisCacheService
.
getTaskDataMap
();
RScoredSortedSet
<
String
>
set
=
redisService
.
getToBeTerminatedTaskUnidSet
();
Collection
<
String
>
entryCollection
=
set
.
valueRange
(
0
,
true
,
System
.
currentTimeMillis
(),
true
);
for
(
String
taskUnid
:
entryCollection
)
{
log
.
info
(
"停止任务 : {}"
,
taskUnid
);
TaskData
taskData
=
taskDataMap
.
get
(
taskUnid
);
RuntimeConfig
runtimeConfig
=
taskData
.
getRuntimeConfig
();
TaskData
taskData
=
taskService
.
getRepository
().
getTaskDataByUnid
(
taskUnid
);
if
(
taskData
==
null
)
{
log
.
info
(
"找不到对应任务,移除所有:{}"
,
taskUnid
);
taskService
.
removeTaskDataAll
(
taskUnid
);
continue
;
}
// 获取可用的 vaserver ,执行任务终止动作,如果成功,解除 taskData和 vaServer 的关联,并且从 zset 中移除任务,恢复vaserver资源数,计算下次任务执行时间,放入zset中
boolean
success
=
vaServerService
.
terminateTask
(
taskUnid
);
if
(
success
)
{
ImmutablePair
<
Long
,
Long
>
nextTime
=
runtimeConfig
.
getNextTimeOfExecutionAndTerminal
();
Long
nextExecuteTime
=
nextTime
.
left
;
Long
nextTerminateTime
=
nextTime
.
right
;
if
(
nextExecuteTime
!=
null
)
{
taskService
.
addExecutableTaskData
(
taskUnid
,
nextExecuteTime
);
taskService
.
addTerminatableTaskData
(
taskUnid
,
nextTerminateTime
);
}
boolean
b
=
taskService
.
distributeTask
(
taskData
);
}
set
.
remove
(
taskUnid
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
""
,
e
);
}
finally
{
try
{
lock
.
unlock
();
}
catch
(
Exception
ignore
)
{
}
jobLock
.
forceUnlock
();
}
}
}
}
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/service/TaskService.java
View file @
5fc02e5
package
com
.
viontech
.
fanxing
.
task
.
scheduling
.
service
;
import
com.viontech.fanxing.commons.model.Task
;
import
com.viontech.fanxing.commons.service.RedisService
;
import
com.viontech.fanxing.task.scheduling.model.RuntimeConfig
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo
;
import
com.viontech.fanxing.task.scheduling.repository.TaskDataRedisRepository
;
import
org.apache.commons.lang3.tuple.ImmutablePair
;
import
org.redisson.api.RMap
;
import
org.redisson.api.RScoredSortedSet
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
tech.powerjob.client.PowerJobClient
;
import
tech.powerjob.common.request.http.SaveJobInfoRequest
;
import
javax.annotation.Resource
;
...
...
@@ -24,9 +23,11 @@ import javax.annotation.Resource;
public
class
TaskService
{
@Resource
private
Redis
CacheService
redisCache
Service
;
private
Redis
Service
redis
Service
;
@Resource
private
VAServerService
vaServerService
;
@Resource
private
TaskDataRedisRepository
taskDataRedisRepository
;
public
boolean
distributeTask
(
TaskData
taskData
)
{
RuntimeConfig
runtimeConfig
=
taskData
.
getRuntimeConfig
();
...
...
@@ -35,10 +36,10 @@ public class TaskService {
Long
nextExecuteTime
=
nextTime
.
left
;
Long
nextTerminateTime
=
nextTime
.
right
;
if
(
nextExecuteTime
!=
null
)
{
RScoredSortedSet
<
String
>
toBeExecutedTaskUnidSet
=
redis
Cache
Service
.
getToBeExecutedTaskUnidSet
();
RScoredSortedSet
<
String
>
toBeExecutedTaskUnidSet
=
redisService
.
getToBeExecutedTaskUnidSet
();
toBeExecutedTaskUnidSet
.
add
(
nextExecuteTime
,
taskUnid
);
if
(
nextTerminateTime
!=
null
)
{
RScoredSortedSet
<
String
>
toBeTerminatedTaskUnidSet
=
redis
Cache
Service
.
getToBeTerminatedTaskUnidSet
();
RScoredSortedSet
<
String
>
toBeTerminatedTaskUnidSet
=
redisService
.
getToBeTerminatedTaskUnidSet
();
toBeTerminatedTaskUnidSet
.
add
(
nextTerminateTime
,
taskUnid
);
}
return
true
;
...
...
@@ -47,31 +48,20 @@ public class TaskService {
}
}
public
void
addTaskData
(
TaskData
taskData
)
{
RMap
<
String
,
TaskData
>
taskDataMap
=
redisCacheService
.
getTaskDataMap
();
taskDataMap
.
put
(
taskData
.
getTask
().
getUnid
(),
taskData
);
}
public
TaskData
getTaskDataByUnid
(
String
taskUnid
)
{
RMap
<
String
,
TaskData
>
taskDataMap
=
redisCacheService
.
getTaskDataMap
();
return
taskDataMap
.
get
(
taskUnid
);
}
public
void
removeTaskDataAll
(
String
taskUnid
)
{
RScoredSortedSet
<
String
>
toBeExecutedTaskUnidSet
=
redisCacheService
.
getToBeExecutedTaskUnidSet
();
RScoredSortedSet
<
String
>
toBeTerminatedTaskUnidSet
=
redisCacheService
.
getToBeTerminatedTaskUnidSet
();
RMap
<
String
,
TaskData
>
taskDataMap
=
redisCacheService
.
getTaskDataMap
();
RScoredSortedSet
<
String
>
toBeExecutedTaskUnidSet
=
redisService
.
getToBeExecutedTaskUnidSet
();
RScoredSortedSet
<
String
>
toBeTerminatedTaskUnidSet
=
redisService
.
getToBeTerminatedTaskUnidSet
();
toBeExecutedTaskUnidSet
.
remove
(
taskUnid
);
toBeTerminatedTaskUnidSet
.
remove
(
taskUnid
);
taskData
Map
.
remove
(
taskUnid
);
taskData
RedisRepository
.
remove
(
taskUnid
);
}
public
VaServerInfo
taskRunOn
(
String
taskUnid
)
{
RMap
<
String
,
String
>
taskVaServerMap
=
redis
Cache
Service
.
getTaskVaServerMap
();
RMap
<
String
,
String
>
taskVaServerMap
=
redisService
.
getTaskVaServerMap
();
String
devId
=
taskVaServerMap
.
get
(
taskUnid
);
return
devId
==
null
?
null
:
vaServerService
.
getVaServerInfoByDevId
(
devId
);
return
devId
==
null
?
null
:
vaServerService
.
getVaServerRedisRepository
().
getVAServerInfoById
(
devId
);
}
/**
...
...
@@ -82,7 +72,7 @@ public class TaskService {
* <li>right <code>devId</code></li>
*/
public
ImmutablePair
<
String
,
String
>
unlinkTaskAndVaServer
(
String
taskUnid
)
{
RMap
<
String
,
String
>
taskVaServerMap
=
redis
Cache
Service
.
getTaskVaServerMap
();
RMap
<
String
,
String
>
taskVaServerMap
=
redisService
.
getTaskVaServerMap
();
String
devId
=
taskVaServerMap
.
get
(
taskUnid
);
taskVaServerMap
.
remove
(
taskUnid
);
return
ImmutablePair
.
of
(
taskUnid
,
devId
);
...
...
@@ -90,11 +80,13 @@ public class TaskService {
public
void
updateTask
(
Task
task
)
{
RMap
<
String
,
TaskData
>
taskDataMap
=
redisCacheService
.
getTaskDataMap
();
TaskData
taskData
=
new
TaskData
(
task
);
// 需要更新taskData,并且向vaServer更新任务信息
taskData
Map
.
put
(
task
.
getUnid
(),
taskData
);
taskData
RedisRepository
.
addOrUpdateTaskData
(
taskData
);
vaServerService
.
updateTask
(
taskData
);
}
public
TaskDataRedisRepository
getRepository
()
{
return
taskDataRedisRepository
;
}
}
fanxing-task-scheduling/src/main/java/com/viontech/fanxing/task/scheduling/service/VAServerService.java
View file @
5fc02e5
...
...
@@ -3,17 +3,17 @@ package com.viontech.fanxing.task.scheduling.service;
import
com.viontech.fanxing.commons.constant.RedisKeys
;
import
com.viontech.fanxing.commons.exception.FanXingException
;
import
com.viontech.fanxing.commons.model.Task
;
import
com.viontech.fanxing.commons.service.RedisService
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo
;
import
com.viontech.fanxing.task.scheduling.repository.VAServerRedisRepository
;
import
org.redisson.api.RBucket
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RMap
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
/**
...
...
@@ -27,7 +27,9 @@ import java.util.concurrent.TimeUnit;
public
class
VAServerService
{
@Resource
private
RedisCacheService
redisCacheService
;
private
RedisService
redisService
;
@Resource
private
VAServerRedisRepository
vaServerRedisRepository
;
@Resource
private
TaskService
taskService
;
@Resource
...
...
@@ -36,39 +38,17 @@ public class VAServerService {
/**
* 设备注册
*/
public
void
registeVAServer
(
VaServerInfo
vaServerInfo
)
{
public
void
registe
r
VAServer
(
VaServerInfo
vaServerInfo
)
{
String
devId
=
vaServerInfo
.
getDevID
();
RMap
<
String
,
VaServerInfo
>
map
=
redisCacheService
.
getVaServerMap
();
RBucket
<
Date
>
bucket
=
redisCacheService
.
getValue
(
RedisKeys
.
getVAServerKeepAliveKey
(
devId
));
bucket
.
set
(
new
Date
());
bucket
.
expire
(
2
,
TimeUnit
.
MINUTES
);
map
.
put
(
devId
,
vaServerInfo
);
}
public
VaServerInfo
getVaServerInfoByDevId
(
String
devId
)
{
RMap
<
String
,
VaServerInfo
>
map
=
redisCacheService
.
getVaServerMap
();
return
map
.
get
(
devId
);
}
public
Collection
<
VaServerInfo
>
getVaServerInfo
()
{
RMap
<
String
,
VaServerInfo
>
map
=
redisCacheService
.
getVaServerMap
();
for
(
Map
.
Entry
<
String
,
VaServerInfo
>
entry
:
map
.
entrySet
())
{
String
devId
=
entry
.
getKey
();
RBucket
<
Date
>
bucket
=
redisCacheService
.
getValue
(
RedisKeys
.
getVAServerKeepAliveKey
(
devId
));
if
(!
bucket
.
isExists
())
{
VaServerInfo
value
=
entry
.
getValue
();
value
.
setStatus
(
0
);
map
.
put
(
devId
,
value
);
}
}
return
map
.
values
();
keepalive
(
devId
);
vaServerRedisRepository
.
addOrUpdate
(
devId
,
vaServerInfo
);
}
/**
* 设备心跳
*/
public
void
keepalive
(
String
devId
)
{
RBucket
<
Date
>
bucket
=
redis
Cache
Service
.
getValue
(
RedisKeys
.
getVAServerKeepAliveKey
(
devId
));
RBucket
<
Date
>
bucket
=
redisService
.
getValue
(
RedisKeys
.
getVAServerKeepAliveKey
(
devId
));
bucket
.
set
(
new
Date
());
bucket
.
expire
(
2
,
TimeUnit
.
MINUTES
);
}
...
...
@@ -83,11 +63,10 @@ public class VAServerService {
vaServerHttpService
.
addTask
(
taskData
,
server
);
RMap
<
String
,
String
>
map
=
redis
Cache
Service
.
getTaskVaServerMap
();
RMap
<
String
,
String
>
map
=
redisService
.
getTaskVaServerMap
();
map
.
put
(
task
.
getUnid
(),
server
.
getDevID
());
server
.
setAvailableResources
(
server
.
getAvailableResources
()
-
task
.
getResourceNeed
());
RMap
<
String
,
VaServerInfo
>
vaServerMap
=
redisCacheService
.
getVaServerMap
();
vaServerMap
.
put
(
server
.
getDevID
(),
server
);
modifyVAServerResource
(
server
.
getDevID
(),
-
task
.
getResourceNeed
());
return
true
;
}
...
...
@@ -99,31 +78,41 @@ public class VAServerService {
* 删除任务
*/
public
boolean
terminateTask
(
String
taskUnid
)
{
TaskData
taskData
=
taskService
.
getTaskDataByUnid
(
taskUnid
);
TaskData
taskData
=
taskService
.
getRepository
().
getTaskDataByUnid
(
taskUnid
);
if
(
taskData
==
null
)
{
return
false
;
}
Task
task
=
taskData
.
getTask
();
RMap
<
String
,
String
>
map
=
redis
Cache
Service
.
getTaskVaServerMap
();
RMap
<
String
,
String
>
map
=
redisService
.
getTaskVaServerMap
();
String
vaServerId
=
map
.
get
(
taskUnid
);
// 如果vaServerId不为空,需要终止任务
if
(
vaServerId
!=
null
)
{
RMap
<
String
,
VaServerInfo
>
vaServerMap
=
redisCacheService
.
getVaServerMap
();
VaServerInfo
vaServerInfo
=
vaServerMap
.
get
(
vaServerId
);
VaServerInfo
vaServerInfo
=
vaServerRedisRepository
.
getVAServerInfoById
(
vaServerId
);
// 下发终止任务请求
vaServerHttpService
.
rmTask
(
taskUnid
,
vaServerInfo
);
// 解除任务和 vaServer 关联, 恢复资源数量
map
.
remove
(
taskUnid
);
RLock
vaServerLock
=
redisCacheService
.
getLockMust
(
"lock:vaserver:"
+
vaServerId
);
modifyVAServerResource
(
vaServerId
,
task
.
getResourceNeed
());
return
true
;
}
return
true
;
}
public
void
modifyVAServerResource
(
String
devId
,
float
param
)
{
RLock
vaServerLock
=
redisService
.
getLockMust
(
"lock:vaserver:"
+
devId
);
try
{
vaServerInfo
.
setAvailableResources
(
vaServerInfo
.
getAvailableResources
()
+
task
.
getResourceNeed
());
vaServerMap
.
put
(
vaServerId
,
vaServerInfo
);
VaServerInfo
vaServerInfo
=
vaServerRedisRepository
.
getVAServerInfoById
(
devId
);
if
(
vaServerInfo
==
null
)
{
return
;
}
vaServerInfo
.
setAvailableResources
(
vaServerInfo
.
getAvailableResources
()
+
param
);
vaServerRedisRepository
.
addOrUpdate
(
devId
,
vaServerInfo
);
}
finally
{
vaServerLock
.
forceUnlock
();
}
return
true
;
}
return
true
;
}
/**
...
...
@@ -183,7 +172,7 @@ public class VAServerService {
* 获取 VAServer 运行状态配置参数等
*/
public
Object
getStatus
(
String
devId
)
{
VaServerInfo
vaServerInfo
=
getVaServerInfoByDev
Id
(
devId
);
VaServerInfo
vaServerInfo
=
vaServerRedisRepository
.
getVAServerInfoBy
Id
(
devId
);
if
(
vaServerInfo
!=
null
)
{
return
vaServerHttpService
.
status
(
vaServerInfo
);
}
else
{
...
...
@@ -227,5 +216,7 @@ public class VAServerService {
}
}
public
VAServerRedisRepository
getVaServerRedisRepository
()
{
return
vaServerRedisRepository
;
}
}
fanxing-task-scheduling/src/test/java/com/viontech/fanxing/task/scheduling/service/VAServerHttpServiceTest.java
View file @
5fc02e5
...
...
@@ -4,20 +4,21 @@ import com.alibaba.fastjson.JSON;
import
com.viontech.fanxing.commons.constant.RedisKeys
;
import
com.viontech.fanxing.commons.model.StoreConfig
;
import
com.viontech.fanxing.commons.model.Task
;
import
com.viontech.fanxing.commons.service.RedisService
;
import
com.viontech.fanxing.task.scheduling.feign.TaskClient
;
import
com.viontech.fanxing.task.scheduling.model.TaskData
;
import
com.viontech.fanxing.task.scheduling.model.vaserver.VaServerInfo
;
import
com.viontech.fanxing.task.scheduling.repository.VAServerRedisRepository
;
import
com.viontech.keliu.util.DateUtil
;
import
com.viontech.keliu.util.JsonMessageUtil
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.runner.RunWith
;
import
org.redisson.api.RMap
;
import
org.redisson.api.RedissonClient
;
import
org.redisson.api.RScoredSortedSet
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
javax.annotation.Resource
;
import
java.text.ParseException
;
/**
* .
...
...
@@ -33,7 +34,9 @@ class VAServerHttpServiceTest {
@Resource
VAServerHttpService
vaServerHttpService
;
@Resource
RedisCacheService
redisCacheService
;
RedisService
redisService
;
@Resource
private
VAServerRedisRepository
vaServerRedisRepository
;
@Resource
private
TaskClient
taskClient
;
...
...
@@ -42,8 +45,7 @@ class VAServerHttpServiceTest {
@BeforeEach
public
void
before
()
{
RMap
<
String
,
VaServerInfo
>
map
=
redisCacheService
.
getVaServerMap
();
this
.
vaServerInfo
=
map
.
get
(
"xxx-xx"
);
this
.
vaServerInfo
=
vaServerRedisRepository
.
getVAServerInfoById
(
"xxx-xx"
);
this
.
taskData
=
new
TaskData
();
Task
task
=
new
Task
();
task
.
setUnid
(
TASK_UNID
);
...
...
@@ -73,7 +75,7 @@ class VAServerHttpServiceTest {
@Test
void
updateAlternate
()
{
vaServerHttpService
.
updateRotationStatus
(
TASK_UNID
,
1
,
vaServerInfo
);
vaServerHttpService
.
updateRotationStatus
(
TASK_UNID
,
1
,
vaServerInfo
);
}
@Test
...
...
@@ -94,14 +96,19 @@ class VAServerHttpServiceTest {
}
@Test
void
test
()
throws
ParseException
{
RMap
<
String
,
String
>
map
=
redisCacheService
.
getTaskVaServerMap
();
map
.
put
(
"0a263320bd274b529e5185e3b05aa157"
,
"xxx-xx"
);
void
testJsonArray
()
{
}
@Test
void
test
()
throws
Exception
{
// RMap<String, String> map = redisCacheService.getTaskVaServerMap();
// map.put("0a263320bd274b529e5185e3b05aa157", "xxx-xx");
// RScoredSortedSet<String> set = redissonClient
.getScoredSortedSet(RedisKeys.SCHEDULING_TO_BE_EXECUTED_TASK_UNID_SET);
// RScoredSortedSet<String> set2 = redissonClient
.getScoredSortedSet(RedisKeys.SCHEDULING_TO_BE_TERMINATED_TASK_UNID_SET);
RScoredSortedSet
<
String
>
set
=
redisService
.
getClient
()
.
getScoredSortedSet
(
RedisKeys
.
SCHEDULING_TO_BE_EXECUTED_TASK_UNID_SET
);
RScoredSortedSet
<
String
>
set2
=
redisService
.
getClient
()
.
getScoredSortedSet
(
RedisKeys
.
SCHEDULING_TO_BE_TERMINATED_TASK_UNID_SET
);
//
// set.add(DateUtil.parse("yyyy-MM-dd HH:mm:ss", "2021-07-22 16:15
:00").getTime(), "tttttttttttttttttt");
// set2.add(DateUtil.parse("yyyy-MM-dd HH:mm:ss", "2021-07-22 16:16
:00").getTime(), "tttttttttttttttttt");
set
.
add
(
DateUtil
.
parse
(
"yyyy-MM-dd HH:mm:ss"
,
"2021-08-11 13:18
:00"
).
getTime
(),
"tttttttttttttttttt"
);
set2
.
add
(
DateUtil
.
parse
(
"yyyy-MM-dd HH:mm:ss"
,
"2021-08-11 13:20
:00"
).
getTime
(),
"tttttttttttttttttt"
);
}
}
\ No newline at end of file
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