Skip to content

Commit

Permalink
Fix invalid cron expressions breaking haJobs
Browse files Browse the repository at this point in the history
  • Loading branch information
slider committed Jan 25, 2018
1 parent f729127 commit bf09f48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
.idea/
*.iml
38 changes: 22 additions & 16 deletions ha-jobs-core/src/main/scala/de/kaufhof/hajobs/JobManager.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.kaufhof.hajobs

import java.lang.RuntimeException
import java.util.{TimeZone, UUID}

import akka.pattern.ask
Expand Down Expand Up @@ -88,27 +89,32 @@ class JobManager(managedJobs: => Jobs,
jobToSchedule.cronExpression match {
case Some(cronExpression) =>
logger.debug("scheduling job for {}", jobType)
val job = JobBuilder
.newJob(classOf[TriggerPuller])
.usingJobData(TPData(jobType, this).asDataMap)
.build()
if(org.quartz.CronExpression.isValidExpression(cronExpression)) {
val job = JobBuilder
.newJob(classOf[TriggerPuller])
.usingJobData(TPData(jobType, this).asDataMap)
.build()

val jobName = jobType.name
val jobName = jobType.name

val trigger = TriggerBuilder
.newTrigger
.withIdentity(s"$jobName-trigger")
.withSchedule(CronScheduleBuilder
// this will throw an exception if the expression is incorrect
.cronSchedule(cronExpression)
.inTimeZone(schedulesTimeZone)
).build()
val trigger = TriggerBuilder
.newTrigger
.withIdentity(s"$jobName-trigger")
.withSchedule(CronScheduleBuilder
// this will throw an exception if the expression is incorrect
.cronSchedule(cronExpression)
.inTimeZone(schedulesTimeZone)
).build()

scheduler.scheduleJob(job, trigger)
scheduler.scheduleJob(job, trigger)

logger.info("Job '{}' has cron expression '{}', first execution at {}", jobType, trigger.getCronExpression, trigger.getNextFireTime)
logger.info("Job '{}' has cron expression '{}', first execution at {}", jobType, trigger.getCronExpression, trigger.getNextFireTime)

runJobNowIfPreviousRunWasSkipped(cronExpression, jobType).map(_ => ())
runJobNowIfPreviousRunWasSkipped(cronExpression, jobType).map(_ => ())
} else {
logger.info("Invalid cronExpression defined for job {}", jobType)
Future.successful(())
}
case None =>
logger.info("No cronExpression defined for job {}", jobType)
Future.successful(())
Expand Down

0 comments on commit bf09f48

Please sign in to comment.