Skip to content
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

#19006 a start date must be set even for Cron task #19222

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.dotmarketing.quartz;

import static com.dotmarketing.quartz.DotStatefulJob.getJobDescription;
import static com.dotmarketing.quartz.DotStatefulJob.getJobGroupName;
import static com.dotmarketing.quartz.DotStatefulJob.getJobName;
import static com.dotmarketing.quartz.DotStatefulJob.getTriggerGroupName;
import static com.dotmarketing.quartz.QuartzUtils.getSequentialScheduler;
import static java.util.Objects.requireNonNull;
import static org.junit.Assert.assertEquals;
Expand All @@ -9,10 +13,15 @@

import com.dotcms.IntegrationTestBase;
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.util.DateUtil;
import com.dotmarketing.util.Logger;
import com.google.common.collect.ImmutableMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -24,6 +33,7 @@
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;

public class DotStatefulJobTest extends IntegrationTestBase {

Expand All @@ -39,8 +49,8 @@ public static void prepare() throws Exception {
* @throws SchedulerException
*/
private void removeAnyExistingJob() throws SchedulerException {
final String jobName = DotStatefulJob.getJobName(MyStatefulJob.class);
final String jobGroupName = DotStatefulJob.getJobGroupName(MyStatefulJob.class);
final String jobName = getJobName(MyStatefulJob.class);
final String jobGroupName = getJobGroupName(MyStatefulJob.class);
QuartzUtils.removeJob(jobName, jobGroupName);
}

Expand Down Expand Up @@ -98,7 +108,7 @@ public void Test_Launch_Stateful_Jobs_Verify_They_Dont_Overlap_In_Time()
* @return
*/
private Optional<JobExecutionContext> getJobExecutionContext(){
final String jobName = DotStatefulJob.getJobName(MyStatefulJob.class);
final String jobName = getJobName(MyStatefulJob.class);
try {
final Scheduler sequentialScheduler = getSequentialScheduler();
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -155,10 +165,70 @@ public static LocalTimeRange of(final LocalTime from, final LocalTime to){
*/
@Test
public void Test_Stateful_Job_Utility_Methods(){
assertEquals("MyStatefulJob",DotStatefulJob.getJobName(MyStatefulJob.class));
assertEquals("MyStatefulJob_Group",DotStatefulJob.getJobGroupName(MyStatefulJob.class));
assertEquals("MyStatefulJob_Trigger_Group",DotStatefulJob.getTriggerGroupName(MyStatefulJob.class));
assertEquals("MyStatefulJob", getJobName(MyStatefulJob.class));
assertEquals("MyStatefulJob_Group", getJobGroupName(MyStatefulJob.class));
assertEquals("MyStatefulJob_Trigger_Group", getTriggerGroupName(MyStatefulJob.class));
assertTrue(DotStatefulJob.nextTriggerName(MyStatefulJob.class).startsWith("MyStatefulJob_Trigger_"));
}

/**
* create a ScheduledTask
* @param startDate
* @return
*/
private ScheduledTask scheduledTask(final Date startDate){
final String jobName = getJobName(MyStatefulJob.class);
final String groupName = getJobGroupName(MyStatefulJob.class);
final String description = getJobDescription(MyStatefulJob.class);
final String nextTriggerName = DotStatefulJob.nextTriggerName(MyStatefulJob.class);
final String triggerGroup = getTriggerGroupName(MyStatefulJob.class);

final Map<String, Object> jobProperties = new HashMap<>();
//get the job detail so we dont lose any data already saved for other triggers.

final Calendar calendar = Calendar.getInstance();
final String cronString = new SimpleDateFormat("ss mm H d M ? yyyy")
.format(calendar.getTime());
final ScheduledTask task = new CronScheduledTask(jobName,
groupName, description,
MyStatefulJob.class.getCanonicalName(), false,
nextTriggerName, triggerGroup, startDate, null,
SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW, 10, true, jobProperties,
cronString);
task.setDurability(true);
return task;
}

/**
* Given scenario: The ScheduledTasks is build but then fired too late.
* Expected Results: We expect SchedulerException `Based on configured schedule, the given trigger will never fire.`
* @throws ParseException
* @throws SchedulerException
* @throws ClassNotFoundException
*/
@Test(expected = SchedulerException.class)
public void Test_Schedule_Task_Make_It_Start_Later_Expect_SchedulerException()
throws ParseException, SchedulerException, ClassNotFoundException {

final ScheduledTask task = scheduledTask(null);
DateUtil.sleep(10000);
QuartzUtils.scheduleTask(task);
}

/**
* Given scenario: The ScheduledTasks is build but then fired too late. But this time we specify a startDate.
* Expected Results: The jobs fired normally.
* @throws ParseException
* @throws SchedulerException
* @throws ClassNotFoundException
*/
@Test
public void Test_Schedule_Task_Make_It_Start_Later_Expect_No_Exception()
throws ParseException, SchedulerException, ClassNotFoundException {

final ScheduledTask task = scheduledTask(new Date());
DateUtil.sleep(10000);
QuartzUtils.scheduleTask(task);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -162,7 +163,7 @@ protected static void enqueueTrigger(final Map<String, Serializable> nextExecuti
final ScheduledTask task = new CronScheduledTask(jobName,
groupName, description,
jobClass.getCanonicalName(), false,
nextTriggerName, triggerGroup, null, null,
nextTriggerName, triggerGroup, new Date(), null,
SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW, 10, true, jobProperties,
cronString);
task.setDurability(true); //must be durable to preserve the detail across triggers.
Expand Down