Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 1f1ab3a

Browse files
littlezhouqiyuangong
authored andcommitted
Solve #1701, Add one-shot rule support (#1702)
1 parent 74e1542 commit 1f1ab3a

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

smart-common/src/main/java/org/smartdata/model/rule/TimeBasedScheduleInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ public long getEvery() {
5757
}
5858

5959
public boolean isOneShot() {
60-
return startTime == endTime && every == 0;
60+
return startTime == endTime && startTime == 0 && every == 0;
6161
}
6262
}

smart-engine/src/main/java/org/smartdata/server/engine/rule/ExecutorScheduler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ public ExecutorScheduler(int numThreads) {
3838
public void addPeriodicityTask(RuleExecutor re) {
3939
TimeBasedScheduleInfo si = re.getTranslateResult().getTbScheduleInfo();
4040
long now = System.currentTimeMillis();
41-
service.scheduleAtFixedRate(re, si.getStartTime() - now,
42-
si.getEvery(), TimeUnit.MILLISECONDS);
41+
long startDelay = si.getStartTime() - now;
42+
if (startDelay < 0) {
43+
startDelay = 0;
44+
}
45+
long every = si.getEvery();
46+
if (every <= 0) {
47+
every = 5000;
48+
}
49+
service.scheduleAtFixedRate(re, startDelay, every, TimeUnit.MILLISECONDS);
4350
}
4451

4552
public void addPeriodicityTask(ScheduleInfo schInfo, Runnable work) {

smart-engine/src/main/java/org/smartdata/server/engine/rule/RuleExecutor.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ public void run() {
266266
TimeBasedScheduleInfo scheduleInfo = tr.getTbScheduleInfo();
267267

268268
if (scheduleInfo.getEndTime() != TimeBasedScheduleInfo.FOR_EVER
269-
// TODO: tricky here, time passed
269+
&& !scheduleInfo.isOneShot()
270270
&& startCheckTime - scheduleInfo.getEndTime() > 0) {
271-
// TODO: special for scheduleInfo.isOneShot()
272271
LOG.info("Rule " + ctx.getRuleId() + " exit rule executor due to time passed or finished");
273272
ruleManager.updateRuleInfo(rid, RuleState.FINISHED, System.currentTimeMillis(), 0, 0);
274273
exitSchedule();
@@ -288,12 +287,8 @@ public void run() {
288287
numCmdSubmitted = submitCmdlets(info, files);
289288
}
290289
ruleManager.updateRuleInfo(rid, null, System.currentTimeMillis(), 1, numCmdSubmitted);
291-
if (exited) {
292-
exitSchedule();
293-
}
294-
// System.out.println(this + " -> " + System.currentTimeMillis());
295-
long endProcessTime = System.currentTimeMillis();
296290

291+
long endProcessTime = System.currentTimeMillis();
297292
if (endProcessTime - startCheckTime > 2000 || LOG.isDebugEnabled()) {
298293
LOG.warn(
299294
"Rule "
@@ -309,6 +304,14 @@ public void run() {
309304
+ ".");
310305
}
311306

307+
if (scheduleInfo.isOneShot()) {
308+
ruleManager.updateRuleInfo(rid, RuleState.FINISHED, System.currentTimeMillis(), 0, 0);
309+
exitSchedule();
310+
}
311+
312+
if (exited) {
313+
exitSchedule();
314+
}
312315
} catch (IOException e) {
313316
LOG.error("Rule " + ctx.getRuleId() + " exception", e);
314317
}

smart-rule/src/main/antlr4/org/smartdata/rule/parser/SmartRule.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ trigger
3333
: AT timepointexpr #triTimePoint
3434
| EVERY timeintvalexpr duringexpr? #triCycle
3535
| ON fileEvent duringexpr? #triFileEvent
36+
| ONCE #triOnce
3637
;
3738

3839
duringexpr : FROM timepointexpr (TO timepointexpr)? ;
@@ -175,6 +176,7 @@ AND : 'and' ;
175176
EVERY : 'every' ;
176177
FROM : 'from' ;
177178
ON : 'on' ;
179+
ONCE : 'once' ;
178180
OR : 'or' ;
179181
NOW : 'now' ;
180182
NOT : 'not' ;

smart-rule/src/main/java/org/smartdata/rule/parser/SmartRuleVisitTranslator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ public TreeNode visitConditions(SmartRuleParser.ConditionsContext ctx) {
9696
return conditions;
9797
}
9898

99+
@Override
100+
public TreeNode visitTriOnce(SmartRuleParser.TriOnceContext ctx) {
101+
timeBasedScheduleInfo = new TimeBasedScheduleInfo();
102+
return null;
103+
}
104+
99105
@Override
100106
public TreeNode visitTriTimePoint(SmartRuleParser.TriTimePointContext ctx) {
101107
timeBasedScheduleInfo = new TimeBasedScheduleInfo();

0 commit comments

Comments
 (0)