Skip to content

Commit 75c834c

Browse files
committed
SNAPSHOT XContent serialization done
1 parent 49f58f7 commit 75c834c

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ public abstract class CronnableSchedule implements Schedule {
1818
private static final Comparator<Cron> CRON_COMPARATOR = Comparator.comparing(Cron::expression);
1919

2020
protected final Cron[] crons;
21+
private TimeZone timeZone;
2122

22-
CronnableSchedule(TimeZone timeZone, String... expressions) {
23-
this(crons(timeZone, expressions));
23+
CronnableSchedule(String... expressions) {
24+
this(crons(expressions));
2425
}
2526

2627
private CronnableSchedule(Cron... crons) {
@@ -30,11 +31,16 @@ private CronnableSchedule(Cron... crons) {
3031
}
3132

3233
protected void setTimeZone(TimeZone timeZone) {
34+
this.timeZone = timeZone;
3335
for (Cron cron : crons) {
3436
cron.setTimeZone(timeZone);
3537
}
3638
}
3739

40+
public TimeZone getTimeZone() {
41+
return timeZone;
42+
}
43+
3844
@Override
3945
public long nextScheduledTimeAfter(long startTime, long time) {
4046
assert time >= startTime;
@@ -69,10 +75,10 @@ public boolean equals(Object obj) {
6975
return Objects.deepEquals(this.crons, other.crons);
7076
}
7177

72-
static Cron[] crons(TimeZone timeZone, String... expressions) {
78+
static Cron[] crons(String... expressions) {
7379
Cron[] crons = new Cron[expressions.length];
7480
for (int i = 0; i < crons.length; i++) {
75-
crons[i] = new Cron(expressions[i], timeZone);
81+
crons[i] = new Cron(expressions[i]);
7682
}
7783
return crons;
7884
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Schedule parse(String context, XContentParser parser) throws IOException
3939
TimeZone timeZone = TimeZone.getTimeZone(ZoneOffset.UTC); // Default to UTC
4040
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
4141
if (token == XContentParser.Token.FIELD_NAME) {
42-
if (parser.currentName().equals("timezone")) {
42+
if (parser.currentName().equals(ScheduleTrigger.TIMEZONE_FIELD)) {
4343
if (schedule != null && schedule instanceof CronnableSchedule == false) {
4444
throw new ElasticsearchParseException(
4545
"could not parse schedule. timezone is not supported for [{}] schedule type",

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
public class ScheduleTrigger implements Trigger {
1515

1616
public static final String TYPE = "schedule";
17+
public static final String TIMEZONE_FIELD = "timezone";
1718

1819
private final Schedule schedule;
1920

@@ -49,7 +50,13 @@ public int hashCode() {
4950

5051
@Override
5152
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
52-
return builder.startObject().field(schedule.type(), schedule, params).endObject();
53+
builder.startObject();
54+
if (schedule instanceof CronnableSchedule cronnableSchedule && cronnableSchedule.getTimeZone() != null) {
55+
builder.field(TIMEZONE_FIELD, cronnableSchedule.getTimeZone().getID());
56+
}
57+
58+
builder.field(schedule.type(), schedule, params);
59+
return builder.endObject();
5360
}
5461

5562
public static Builder builder(Schedule schedule) {

0 commit comments

Comments
 (0)