Skip to content

Commit 2f39ca7

Browse files
committed
add methods to access crontablist and get next/previous executions
Next executions plugin currently uses reflection to get the next executions. By adding a java api the plugin can make use of that api. Also add a method to get the next ParameterizedCronTab so one has access to the parameters.
1 parent ee4c27a commit 2f39ca7

4 files changed

Lines changed: 91 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: 21 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,6 +28,7 @@ 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

3134
/**
@@ -60,4 +63,22 @@ public boolean check(Calendar calendar) {
6063
public String checkSanity() {
6164
return cronTabList.checkSanity();
6265
}
66+
67+
@SuppressRestrictedWarnings(CronTabList.class)
68+
public Calendar previous() {
69+
return cronTabList.previous();
70+
}
71+
72+
@SuppressRestrictedWarnings(CronTabList.class)
73+
public Calendar next() {
74+
return cronTabList.next();
75+
}
76+
77+
public Calendar ceil(long timeInMillis) {
78+
return cronTab.ceil(timeInMillis);
79+
}
80+
81+
public Calendar floor(long timeInMillis) {
82+
return cronTab.floor(timeInMillis);
83+
}
6384
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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

@@ -63,4 +64,63 @@ public String checkSanity() {
6364
}
6465
return null;
6566
}
67+
68+
public @CheckForNull Calendar previous() {
69+
Calendar nearest = null;
70+
for (ParameterizedCronTab tab : cronTabs) {
71+
Calendar next = tab.next();
72+
if (next == null || next.before(nearest)) {
73+
nearest = next;
74+
}
75+
}
76+
return nearest;
77+
}
78+
79+
public @CheckForNull Calendar next() {
80+
Calendar nearest = null;
81+
for (ParameterizedCronTab tab : cronTabs) {
82+
Calendar previous = tab.previous();
83+
if (nearest == null || nearest.after(previous)) {
84+
nearest = previous;
85+
}
86+
}
87+
return nearest;
88+
}
89+
90+
public @CheckForNull ParameterizedCronTab nextParameterizedCronTab() {
91+
Calendar nearest = null;
92+
ParameterizedCronTab next = null;
93+
for (ParameterizedCronTab tab : cronTabs) {
94+
Calendar previous = tab.previous();
95+
if (nearest == null || nearest.after(previous)) {
96+
nearest = previous;
97+
next = tab;
98+
}
99+
}
100+
return next;
101+
}
102+
103+
@CheckForNull
104+
public Calendar ceil(long timestamp) {
105+
Calendar ceil = null;
106+
for (ParameterizedCronTab wrapper: cronTabs) {
107+
Calendar scheduled = wrapper.ceil(timestamp);
108+
if (ceil == null || (scheduled != null && ceil.after(scheduled))) {
109+
ceil = scheduled;
110+
}
111+
}
112+
return ceil;
113+
}
114+
115+
@CheckForNull
116+
public Calendar floor(long timestamp) {
117+
Calendar floor = null;
118+
for (ParameterizedCronTab wrapper: cronTabs) {
119+
Calendar scheduled = wrapper.floor(timestamp);
120+
if (floor == null || (scheduled != null && floor.before(scheduled))) {
121+
floor = scheduled;
122+
}
123+
}
124+
return floor;
125+
}
66126
}

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)