Skip to content

Fix - Concurrent jobs launch: start several cron expressions at the same time #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import hudson.scheduler.CronTabList;
import hudson.scheduler.Hash;
import hudson.scheduler.Messages;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import java.util.stream.Collectors;
import antlr.ANTLRException;

/**
* mostly a copy of {@link CronTabList}
*
*
* @author jameswilson
*
*/
Expand Down Expand Up @@ -45,12 +44,10 @@ public static ParameterizedCronTabList create(String cronTabSpecification, Hash
return new ParameterizedCronTabList(result);
}

public ParameterizedCronTab check(Calendar calendar) {
for (ParameterizedCronTab tab : cronTabs) {
if (tab.check(calendar))
return tab;
}
return null;
public List<ParameterizedCronTab> check(Calendar calendar) {
return cronTabs.stream()
.filter(tab -> tab.check(calendar))
.collect(Collectors.toList());
}

public String checkSanity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.triggers.TriggerDescriptor;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -20,7 +19,7 @@

/**
* {@link Trigger} that runs a job periodically with support for parameters.
*
*
* @author jameswilson
*
*/
Expand All @@ -43,7 +42,7 @@ public void run() {

/**
* this method started out as hudson.model.AbstractProject.getDefaultParametersValues()
* @param parameterValues
* @param parameterValues
* @return the ParameterValues as set from the crontab row or their defaults
*/
@SuppressWarnings("unchecked")
Expand All @@ -70,10 +69,10 @@ private List<ParameterValue> configurePropertyValues(Map<String, String> paramet

public void checkCronTabsAndRun(Calendar calendar) {
LOGGER.fine("checking and maybe running at " + calendar);
ParameterizedCronTab cronTab = cronTabList.check(calendar);
List<ParameterizedCronTab> checkedCronTabList = cronTabList.check(calendar);
Jenkins jenkins = Jenkins.getInstance();

if (cronTab != null) {
checkedCronTabList.forEach(cronTab -> {
Map<String, String> parameterValues = cronTab.getParameterValues();
ParametersAction parametersAction = new ParametersAction(configurePropertyValues(parameterValues));
assert job != null : "job must not be null, if this was 'started'";
Expand All @@ -82,7 +81,7 @@ public void checkCronTabsAndRun(Calendar calendar) {
} else if (jenkins != null && jenkins.getPlugin("workflow-job") != null && job instanceof WorkflowJob) {
((WorkflowJob) job).scheduleBuild2(0, causeAction(parameterValues), parametersAction);
}
}
});
}

private CauseAction causeAction(Map<String, String> parameterValues) {
Expand All @@ -104,7 +103,7 @@ public void start(Job project, boolean newInstance) {

/**
* for the config.jelly to populate
*
*
* @return the raw specification
*/
public String getParameterizedSpecification() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.jenkinsci.plugins.parameterizedscheduler.ParameterizedCronTab;
import org.jenkinsci.plugins.parameterizedscheduler.ParameterizedCronTabList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -28,14 +29,20 @@ public class ParameterizedCronTabListTest {

@Test
public void create() throws Exception {
ParameterizedCronTabList testObject = ParameterizedCronTabList.create("* * * * *%foo=bar");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second test should be split into a new test

ParameterizedCronTabList testObject = ParameterizedCronTabList.create("* * * * *%foo=bar\n */1 * * * *%bar=foo");
assertTrue(testObject.checkSanity(), testObject.checkSanity().startsWith("Do you really mean \"every minute\""));
ParameterizedCronTab actualCronTab = testObject.check(new GregorianCalendar());
assertTrue(actualCronTab != null);
List<ParameterizedCronTab> actualCronTabList = testObject.check(new GregorianCalendar());
assertTrue(actualCronTabList.size() == 2);

Map<String, String> expected = Maps.newHashMap();
expected.put("foo", "bar");
assertEquals(expected, actualCronTab.getParameterValues());
expected.put("bar", "foo");

Map<String, String> actual = actualCronTabList.stream()
.flatMap(tab -> tab.getParameterValues().entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

assertEquals(expected, actual);
}

@Test
Expand All @@ -44,22 +51,20 @@ public void check_Delegates_ReturnsNull() {
mockParameterizedCronTabToo));
GregorianCalendar testCalendar = new GregorianCalendar();

assertNull(testObject.check(testCalendar));
assertTrue(testObject.check(testCalendar).isEmpty());

Mockito.verify(mockParameterizedCronTab).check(testCalendar);
Mockito.verify(mockParameterizedCronTabToo).check(testCalendar);
}

@Test
public void check_Delegates_ReturnsSame_EarlyExit() {
public void check_Delegates_ReturnsSame_WithoutEarlyExit() {
ParameterizedCronTabList testObject = new ParameterizedCronTabList(Arrays.asList(mockParameterizedCronTab,
mockParameterizedCronTabToo));
GregorianCalendar testCalendar = new GregorianCalendar();

Mockito.when(mockParameterizedCronTab.check(testCalendar)).thenReturn(true);
assertSame(mockParameterizedCronTab, testObject.check(testCalendar));

Mockito.verifyZeroInteractions(mockParameterizedCronTabToo);
assertEquals(Collections.singletonList(mockParameterizedCronTab), testObject.check(testCalendar));
}

@Test
Expand All @@ -69,8 +74,7 @@ public void check_Delegates_ReturnsSame() {
GregorianCalendar testCalendar = new GregorianCalendar();

Mockito.when(mockParameterizedCronTabToo.check(testCalendar)).thenReturn(true);
assertSame(mockParameterizedCronTabToo, testObject.check(testCalendar));

assertEquals(Collections.singletonList(mockParameterizedCronTabToo), testObject.check(testCalendar));
}

@Test
Expand Down