Skip to content

Commit 837133a

Browse files
committed
SNAPSHOT FOR TEST
1 parent 8e26d18 commit 837133a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils;
1818
import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent;
1919
import org.elasticsearch.xpack.core.watcher.watch.Watch;
20+
import org.elasticsearch.xpack.core.watcher.watch.WatchStatus;
21+
import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
2022
import org.elasticsearch.xpack.watcher.trigger.schedule.Schedule;
2123
import org.elasticsearch.xpack.watcher.trigger.schedule.ScheduleRegistry;
2224
import org.elasticsearch.xpack.watcher.trigger.schedule.ScheduleTrigger;
@@ -32,6 +34,7 @@
3234
import java.util.Collections;
3335
import java.util.List;
3436
import java.util.Map;
37+
import java.util.Optional;
3538
import java.util.concurrent.ConcurrentHashMap;
3639
import java.util.concurrent.CountDownLatch;
3740
import java.util.concurrent.atomic.AtomicBoolean;
@@ -67,7 +70,17 @@ public synchronized void start(Collection<Watch> jobs) {
6770
Map<String, ActiveSchedule> startingSchedules = Maps.newMapWithExpectedSize(jobs.size());
6871
for (Watch job : jobs) {
6972
if (job.trigger() instanceof ScheduleTrigger trigger) {
70-
startingSchedules.put(job.id(), new ActiveSchedule(job.id(), trigger.getSchedule(), startTime));
73+
if (trigger.getSchedule() instanceof IntervalSchedule) {
74+
long firstActivated = Optional.ofNullable(job.status())
75+
.map(WatchStatus::state)
76+
.map(WatchStatus.State::getTimestamp)
77+
.map(ZonedDateTime::toInstant)
78+
.map(Instant::toEpochMilli)
79+
.orElse(startTime);
80+
startingSchedules.put(job.id(), new ActiveSchedule(job.id(), trigger.getSchedule(), firstActivated));
81+
} else {
82+
startingSchedules.put(job.id(), new ActiveSchedule(job.id(), trigger.getSchedule(), startTime));
83+
}
7184
}
7285
}
7386
// why are we calling putAll() here instead of assigning a brand
@@ -108,7 +121,18 @@ public void add(Watch watch) {
108121
// watcher indexing listener
109122
// this also means that updating an existing watch would not retrigger the schedule time, if it remains the same schedule
110123
if (currentSchedule == null || currentSchedule.schedule.equals(trigger.getSchedule()) == false) {
111-
schedules.put(watch.id(), new ActiveSchedule(watch.id(), trigger.getSchedule(), clock.millis()));
124+
if (trigger.getSchedule() instanceof IntervalSchedule) {
125+
long firstActivated = Optional.ofNullable(watch.status())
126+
.map(WatchStatus::state)
127+
.map(WatchStatus.State::getTimestamp)
128+
.map(ZonedDateTime::toInstant)
129+
.map(Instant::toEpochMilli)
130+
.orElse(clock.millis());
131+
schedules.put(watch.id(), new ActiveSchedule(watch.id(), trigger.getSchedule(), firstActivated));
132+
} else {
133+
schedules.put(watch.id(), new ActiveSchedule(watch.id(), trigger.getSchedule(), clock.millis()));
134+
}
135+
112136
}
113137
}
114138

0 commit comments

Comments
 (0)