Skip to content

Commit

Permalink
Solve #1701, Add one-shot rule support (#1702)
Browse files Browse the repository at this point in the history
  • Loading branch information
littlezhou authored Apr 26, 2018
1 parent dfc8f48 commit 996b7b8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public long getEvery() {
}

public boolean isOneShot() {
return startTime == endTime && every == 0;
return startTime == endTime && startTime == 0 && every == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ public ExecutorScheduler(int numThreads) {
public void addPeriodicityTask(RuleExecutor re) {
TimeBasedScheduleInfo si = re.getTranslateResult().getTbScheduleInfo();
long now = System.currentTimeMillis();
service.scheduleAtFixedRate(re, si.getStartTime() - now,
si.getEvery(), TimeUnit.MILLISECONDS);
long startDelay = si.getStartTime() - now;
if (startDelay < 0) {
startDelay = 0;
}
long every = si.getEvery();
if (every <= 0) {
every = 5000;
}
service.scheduleAtFixedRate(re, startDelay, every, TimeUnit.MILLISECONDS);
}

public void addPeriodicityTask(ScheduleInfo schInfo, Runnable work) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,8 @@ public void run() {
TimeBasedScheduleInfo scheduleInfo = tr.getTbScheduleInfo();

if (scheduleInfo.getEndTime() != TimeBasedScheduleInfo.FOR_EVER
// TODO: tricky here, time passed
&& !scheduleInfo.isOneShot()
&& startCheckTime - scheduleInfo.getEndTime() > 0) {
// TODO: special for scheduleInfo.isOneShot()
LOG.info("Rule " + ctx.getRuleId() + " exit rule executor due to time passed or finished");
ruleManager.updateRuleInfo(rid, RuleState.FINISHED, System.currentTimeMillis(), 0, 0);
exitSchedule();
Expand All @@ -288,12 +287,8 @@ public void run() {
numCmdSubmitted = submitCmdlets(info, files);
}
ruleManager.updateRuleInfo(rid, null, System.currentTimeMillis(), 1, numCmdSubmitted);
if (exited) {
exitSchedule();
}
// System.out.println(this + " -> " + System.currentTimeMillis());
long endProcessTime = System.currentTimeMillis();

long endProcessTime = System.currentTimeMillis();
if (endProcessTime - startCheckTime > 2000 || LOG.isDebugEnabled()) {
LOG.warn(
"Rule "
Expand All @@ -309,6 +304,14 @@ public void run() {
+ ".");
}

if (scheduleInfo.isOneShot()) {
ruleManager.updateRuleInfo(rid, RuleState.FINISHED, System.currentTimeMillis(), 0, 0);
exitSchedule();
}

if (exited) {
exitSchedule();
}
} catch (IOException e) {
LOG.error("Rule " + ctx.getRuleId() + " exception", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ trigger
: AT timepointexpr #triTimePoint
| EVERY timeintvalexpr duringexpr? #triCycle
| ON fileEvent duringexpr? #triFileEvent
| ONCE #triOnce
;

duringexpr : FROM timepointexpr (TO timepointexpr)? ;
Expand Down Expand Up @@ -175,6 +176,7 @@ AND : 'and' ;
EVERY : 'every' ;
FROM : 'from' ;
ON : 'on' ;
ONCE : 'once' ;
OR : 'or' ;
NOW : 'now' ;
NOT : 'not' ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public TreeNode visitConditions(SmartRuleParser.ConditionsContext ctx) {
return conditions;
}

@Override
public TreeNode visitTriOnce(SmartRuleParser.TriOnceContext ctx) {
timeBasedScheduleInfo = new TimeBasedScheduleInfo();
return null;
}

@Override
public TreeNode visitTriTimePoint(SmartRuleParser.TriTimePointContext ctx) {
timeBasedScheduleInfo = new TimeBasedScheduleInfo();
Expand Down

0 comments on commit 996b7b8

Please sign in to comment.