Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit c7e4b8bc
authored
Nov 29, 2023
by
Ren Ping
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:自动任务截止时间优化
1 parent
0eafd292
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
18 deletions
project-dispatch/src/main/java/com/dituhui/pea/dispatch/quartz/dispatch/AutoDispatchJob.java
project-dispatch/src/main/java/com/dituhui/pea/dispatch/service/SchedulerService.java
project-dispatch/src/main/java/com/dituhui/pea/dispatch/service/impl/SchedulerServiceImpl.java
project-dispatch/src/main/resources/application-dev.yaml
project-dispatch/src/main/java/com/dituhui/pea/dispatch/quartz/dispatch/AutoDispatchJob.java
View file @
c7e4b8b
package
com
.
dituhui
.
pea
.
dispatch
.
quartz
.
dispatch
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.date.TimeInterval
;
import
com.dituhui.pea.dispatch.service.SchedulerService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.quartz.DisallowConcurrentExecution
;
import
org.quartz.JobExecutionContext
;
import
org.quartz.JobExecutionException
;
import
org.quartz.JobKey
;
import
org.quartz.*
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.quartz.QuartzJobBean
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.Date
;
/**
* 自动派工任务
...
...
@@ -28,20 +29,29 @@ public class AutoDispatchJob extends QuartzJobBean {
@Resource
private
SchedulerService
schedulerService
;
@Autowired
private
Scheduler
scheduler
;
@Override
protected
void
executeInternal
(
JobExecutionContext
jobExecutionContext
)
throws
JobExecutionException
{
try
{
JobKey
jobKey
=
jobExecutionContext
.
getJobDetail
().
getKey
();
CronTrigger
cronTrigger
=
(
CronTrigger
)
scheduler
.
getTrigger
(
new
TriggerKey
(
jobKey
.
getName
(),
jobKey
.
getName
()));
String
name
=
jobKey
.
getName
();
String
teamId
=
name
.
substring
(
TEAM_JOB_PREFIX
.
length
());
long
start
=
System
.
currentTimeMillis
();
// 获取任务的下次执行时间
Date
nextFireTime
=
cronTrigger
.
getFireTimeAfter
(
new
Date
());
log
.
info
(
">>> teamId:{},下次触发时间:{}, {}"
,
teamId
,
nextFireTime
.
getTime
(),
DateUtil
.
format
(
nextFireTime
,
"yyyy-MM-dd HH:mm:ss"
));
TimeInterval
timeInterval
=
new
TimeInterval
();
log
.
info
(
">>> 自动派工(teamId:{}) 自动任务开始"
,
teamId
);
/*RedissonUtil.lockOperation(AutoDispatchJob.TEAM_JOB_PREFIX + teamId, 60, () -> {
schedulerService.dispatchRun2(teamId);
});*/
schedulerService
.
dispatchRun2
(
teamId
);
long
end
=
System
.
currentTimeMillis
();
log
.
info
(
">>> 自动派工(teamId:{}) 自动任务结束,耗时:{}"
,
teamId
,
end
-
start
);
schedulerService
.
dispatchRun2
(
teamId
,
nextFireTime
);
log
.
info
(
">>> 自动派工(teamId:{}) 自动任务结束,耗时:{}"
,
teamId
,
timeInterval
.
interval
());
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
...
...
project-dispatch/src/main/java/com/dituhui/pea/dispatch/service/SchedulerService.java
View file @
c7e4b8b
package
com
.
dituhui
.
pea
.
dispatch
.
service
;
import
java.util.Date
;
public
interface
SchedulerService
{
/**
...
...
@@ -9,5 +11,5 @@ public interface SchedulerService {
* @author RenPing
* @date 2023/11/02
*/
void
dispatchRun2
(
String
teamId
);
void
dispatchRun2
(
String
teamId
,
Date
nextFireTime
);
}
project-dispatch/src/main/java/com/dituhui/pea/dispatch/service/impl/SchedulerServiceImpl.java
View file @
c7e4b8b
...
...
@@ -28,6 +28,7 @@ import java.io.File;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Date
;
import
java.util.Objects
;
import
java.util.Optional
;
import
java.util.UUID
;
...
...
@@ -75,7 +76,7 @@ public class SchedulerServiceImpl implements SchedulerService {
}
@Override
public
void
dispatchRun2
(
String
teamId
)
{
public
void
dispatchRun2
(
String
teamId
,
Date
nextFireTime
)
{
OrgTeamEntity
orgTeamEntity
=
orgTeamDao
.
findByTeamId
(
teamId
);
if
(
Objects
.
isNull
(
orgTeamEntity
))
{
log
.
error
(
">>> teamId:{} 不存在"
,
teamId
);
...
...
@@ -94,18 +95,14 @@ public class SchedulerServiceImpl implements SchedulerService {
cutOffTime
=
"18:00"
;
}
LocalDateTime
now
=
LocalDateTime
.
now
();
String
today
=
DateUtil
.
format
(
now
,
"yyyy-MM-dd"
);
String
nowTime
=
DateUtil
.
format
(
now
,
"HH:mm"
);
String
nowTimePlus30
=
DateUtil
.
format
(
now
.
plusMinutes
(
30
),
"HH:mm"
);
String
nextTime
=
DateUtil
.
format
(
nextFireTime
,
"HH:mm"
);
boolean
cutOff
=
false
;
//当前时间大于等于截止时间,或者当前时间+30分钟大于截止时间,运行停止
if
(
n
owTime
.
compareTo
(
cutOffTime
)
>=
0
||
nowTimePlus30
.
compareTo
(
cutOffTime
)
>
0
)
{
if
(
n
extTime
.
compareTo
(
cutOffTime
)
>
0
)
{
cutOff
=
true
;
}
String
groupId
=
orgTeamEntity
.
getGroupId
();
log
.
info
(
"dispatchRun group:{}, team:{} done"
,
groupId
,
teamId
);
...
...
@@ -114,7 +111,8 @@ public class SchedulerServiceImpl implements SchedulerService {
Optional
<
DispatchBatch
>
optional
=
dispatchBatchRepository
.
findByTeamIdAndBatchDate
(
teamId
,
currDay
);
if
(
optional
.
isPresent
()
&&
Objects
.
nonNull
(
optional
.
get
().
getCutoffedTime
())
&&
DateUtil
.
format
(
optional
.
get
().
getCutoffedTime
(),
"yyyy-MM-dd"
).
equals
(
today
))
{
&&
DateUtil
.
format
(
optional
.
get
().
getCutoffedTime
(),
"yyyy-MM-dd"
)
.
equals
(
DateUtil
.
format
(
new
Date
(),
"yyyy-MM-dd"
)))
{
//自动任务截止
log
.
error
(
">>> teamId:{}, day:{} 自动任务已截止"
,
teamId
,
currDay
);
return
;
...
...
project-dispatch/src/main/resources/application-dev.yaml
View file @
c7e4b8b
...
...
@@ -3,7 +3,7 @@ server:
dispatch
:
cron
:
expr
:
0
50
8-23 * * ?
expr
:
0
3
8-23 * * ?
next-day-limit
:
2
scheduler
:
...
...
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