Skip to content

Commit 531b4f4

Browse files
authored
Merge pull request #294 from mawinter69/java-api
Add methods to access crontablist and get next/previous executions
2 parents 8d2a3e2 + 74290c6 commit 531b4f4

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@
6666
<groupId>org.jenkins-ci.plugins.workflow</groupId>
6767
<artifactId>workflow-job</artifactId>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.kohsuke</groupId>
71+
<artifactId>access-modifier-suppressions</artifactId>
72+
<version>1.35</version>
73+
<scope>provided</scope>
74+
</dependency>
6975
<dependency>
7076
<groupId>org.jenkins-ci.plugins.workflow</groupId>
7177
<artifactId>workflow-cps</artifactId>

src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedCronTab.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import hudson.scheduler.CronTab;
99
import hudson.scheduler.CronTabList;
1010
import hudson.scheduler.Hash;
11+
import org.kohsuke.accmod.restrictions.suppressions.SuppressRestrictedWarnings;
1112

1213
/**
1314
* this is a copy of {@link CronTab} with added parameters map support
@@ -18,6 +19,7 @@ public class ParameterizedCronTab {
1819

1920
private final Map<String, String> parameterValues;
2021
private final CronTabList cronTabList;
22+
private final CronTab cronTab;
2123

2224
/**
2325
* @param cronTab the crontab to use as a template
@@ -26,8 +28,13 @@ public class ParameterizedCronTab {
2628
public ParameterizedCronTab(CronTab cronTab, Map<String, String> parameters) {
2729
cronTabList = new CronTabList(Collections.singleton(cronTab));
2830
parameterValues = parameters;
31+
this.cronTab = cronTab;
2932
}
3033

34+
public CronTab getCronTab() {
35+
return cronTab;
36+
}
37+
3138
/**
3239
* @param hash
3340
* Used to spread out token like "@daily". Null to preserve the legacy behaviour
@@ -60,4 +67,22 @@ public boolean check(Calendar calendar) {
6067
public String checkSanity() {
6168
return cronTabList.checkSanity();
6269
}
70+
71+
@SuppressRestrictedWarnings(CronTabList.class)
72+
public Calendar previous() {
73+
return cronTabList.previous();
74+
}
75+
76+
@SuppressRestrictedWarnings(CronTabList.class)
77+
public Calendar next() {
78+
return cronTabList.next();
79+
}
80+
81+
public Calendar ceil(long timeInMillis) {
82+
return cronTab.ceil(timeInMillis);
83+
}
84+
85+
public Calendar floor(long timeInMillis) {
86+
return cronTab.floor(timeInMillis);
87+
}
6388
}

src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedCronTabList.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.jenkinsci.plugins.parameterizedscheduler;
22

3+
import edu.umd.cs.findbugs.annotations.CheckForNull;
34
import hudson.scheduler.CronTabList;
45
import hudson.scheduler.Hash;
56

67
import java.util.ArrayList;
78
import java.util.Calendar;
9+
import java.util.Collections;
810
import java.util.List;
911
import java.util.stream.Collectors;
1012

@@ -22,6 +24,10 @@ public ParameterizedCronTabList(List<ParameterizedCronTab> cronTabs) {
2224
this.cronTabs = cronTabs;
2325
}
2426

27+
public List<ParameterizedCronTab> getCronTabs() {
28+
return Collections.unmodifiableList(cronTabs);
29+
}
30+
2531
public static ParameterizedCronTabList create(String cronTabSpecification) {
2632
return create(cronTabSpecification, null);
2733
}
@@ -63,4 +69,63 @@ public String checkSanity() {
6369
}
6470
return null;
6571
}
72+
73+
public @CheckForNull Calendar previous() {
74+
Calendar nearest = null;
75+
for (ParameterizedCronTab tab : cronTabs) {
76+
Calendar next = tab.next();
77+
if (next == null || next.before(nearest)) {
78+
nearest = next;
79+
}
80+
}
81+
return nearest;
82+
}
83+
84+
public @CheckForNull Calendar next() {
85+
Calendar nearest = null;
86+
for (ParameterizedCronTab tab : cronTabs) {
87+
Calendar previous = tab.previous();
88+
if (nearest == null || nearest.after(previous)) {
89+
nearest = previous;
90+
}
91+
}
92+
return nearest;
93+
}
94+
95+
public @CheckForNull ParameterizedCronTab nextParameterizedCronTab() {
96+
Calendar nearest = null;
97+
ParameterizedCronTab next = null;
98+
for (ParameterizedCronTab tab : cronTabs) {
99+
Calendar previous = tab.previous();
100+
if (nearest == null || nearest.after(previous)) {
101+
nearest = previous;
102+
next = tab;
103+
}
104+
}
105+
return next;
106+
}
107+
108+
@CheckForNull
109+
public Calendar ceil(long timestamp) {
110+
Calendar ceil = null;
111+
for (ParameterizedCronTab wrapper: cronTabs) {
112+
Calendar scheduled = wrapper.ceil(timestamp);
113+
if (ceil == null || (scheduled != null && ceil.after(scheduled))) {
114+
ceil = scheduled;
115+
}
116+
}
117+
return ceil;
118+
}
119+
120+
@CheckForNull
121+
public Calendar floor(long timestamp) {
122+
Calendar floor = null;
123+
for (ParameterizedCronTab wrapper: cronTabs) {
124+
Calendar scheduled = wrapper.floor(timestamp);
125+
if (floor == null || (scheduled != null && floor.before(scheduled))) {
126+
floor = scheduled;
127+
}
128+
}
129+
return floor;
130+
}
66131
}

src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedTimerTrigger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public ParameterizedTimerTrigger(String parameterizedSpecification) {
3838
this.cronTabList = ParameterizedCronTabList.create(parameterizedSpecification);
3939
}
4040

41+
public ParameterizedCronTabList getCronTabList() {
42+
return cronTabList;
43+
}
44+
4145
@Override
4246
public void run() {
4347
LOGGER.fine("tried to run from base Trigger, nothing will happen");

0 commit comments

Comments
 (0)