Skip to content

Commit

Permalink
Merge pull request #4173 from Coduz/fix-parsingCronExpressionException
Browse files Browse the repository at this point in the history
🐛 [Scheduler] Fixed returned exception when an invalid cron expression is provided
  • Loading branch information
Coduz authored Jan 14, 2025
2 parents 3a9ae40 + dcfa1e2 commit 971fa6e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.kapua.service.scheduler.quartz.driver;

import com.google.common.base.Strings;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.model.id.KapuaId;
Expand All @@ -28,6 +29,7 @@
import org.eclipse.kapua.service.scheduler.quartz.job.KapuaJobLauncher;
import org.eclipse.kapua.service.scheduler.trigger.Trigger;
import org.eclipse.kapua.service.scheduler.trigger.definition.TriggerProperty;
import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
Expand All @@ -45,6 +47,7 @@
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.Date;
import java.util.TimeZone;

Expand Down Expand Up @@ -114,7 +117,7 @@ public static void createIntervalJobTrigger(@NotNull Trigger trigger) throws Kap
}


public static void createCronJobTrigger(@NotNull Trigger trigger) throws KapuaIllegalNullArgumentException, QuartzTriggerDriverException {
public static void createCronJobTrigger(@NotNull Trigger trigger) throws KapuaIllegalArgumentException, QuartzTriggerDriverException {
String cron = null;

for (TriggerProperty tp : trigger.getTriggerProperties()) {
Expand All @@ -128,12 +131,22 @@ public static void createCronJobTrigger(@NotNull Trigger trigger) throws KapuaIl
throw new KapuaIllegalNullArgumentException("cronExpression");
}

createQuartzTriggerWithSchedule(
trigger,
CronScheduleBuilder.cronSchedule(cron)
.withMisfireHandlingInstructionFireAndProceed() // This option force a misfired trigger to be always fired
.inTimeZone(TimeZone.getTimeZone("UTC"))
);
CronExpression cronExpression;
try {
cronExpression = new CronExpression(cron);
}
catch (ParseException pe) {
LOG.warn("Error while parsing cron {} expression for Trigger {} in scope {}", cron, trigger.getId(), trigger.getScopeId(), pe);
throw new KapuaIllegalArgumentException("cronExpression", cron);
}

CronScheduleBuilder cronScheduleBuilder =
CronScheduleBuilder
.cronSchedule(cronExpression)
.withMisfireHandlingInstructionFireAndProceed() // This option force a misfired trigger to be always fired
.inTimeZone(TimeZone.getTimeZone("UTC"));

createQuartzTriggerWithSchedule(trigger, cronScheduleBuilder);
}

public static void deleteTrigger(@NotNull Trigger trigger) throws CannotUnscheduleJobException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Feature: Scheduler Service
And A regular trigger creator with the name "schedule0" is created
And The trigger is set to start today at "10:00"
Then I set cron expression to "12 1 ? ? *"
And I expect the exception "KapuaException" with the text "*"
And I expect the exception "KapuaIllegalArgumentException" with the text "An illegal value was provided for the argument cronExpression"
And I create a new trigger from the existing creator with previously defined date properties
Then An exception was thrown

Expand Down

0 comments on commit 971fa6e

Please sign in to comment.