Skip to content

Commit bb7d79a

Browse files
Feature/305 improve cron (#423)
* Tried with two separate libraries * Remove other cron libraries * Use verbose mode
1 parent fbe939c commit bb7d79a

File tree

3 files changed

+79
-16
lines changed

3 files changed

+79
-16
lines changed

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
<scalatest.version>3.0.5</scalatest.version>
105105
<h2database.version>1.4.200</h2database.version>
106106
<mockito.version>2.25.0</mockito.version>
107-
<cronutils.version>9.1.3</cronutils.version>
107+
<cron-expression-descriptor.version>1.2</cron-expression-descriptor.version>
108108
<node.version>12.14.1</node.version>
109109
<npm.version>6.13.4</npm.version>
110110
<scala-maven-plugin.version>3.2.1</scala-maven-plugin.version>
@@ -213,9 +213,9 @@
213213
<version>${spring.ldap.version}</version>
214214
</dependency>
215215
<dependency>
216-
<groupId>com.cronutils</groupId>
217-
<artifactId>cron-utils</artifactId>
218-
<version>${cronutils.version}</version>
216+
<groupId>it.burning</groupId>
217+
<artifactId>cron-expression-descriptor</artifactId>
218+
<version>${cron-expression-descriptor.version}</version>
219219
</dependency>
220220
<dependency>
221221
<groupId>org.liquibase</groupId>

src/main/scala/za/co/absa/hyperdrive/trigger/api/rest/controllers/UtilController.scala

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,30 @@
1515

1616
package za.co.absa.hyperdrive.trigger.api.rest.controllers
1717

18-
import java.util.Locale
19-
20-
import com.cronutils.descriptor.CronDescriptor
21-
import com.cronutils.model.CronType
22-
import com.cronutils.model.definition.CronDefinitionBuilder
23-
import com.cronutils.parser.CronParser
18+
import it.burning.cron.CronExpressionParser.{CronExpressionParseException, Options}
19+
import it.burning.cron.CronExpressionDescriptor
2420
import org.springframework.web.bind.annotation._
2521
import za.co.absa.hyperdrive.trigger.models.QuartzExpressionDetail
2622

23+
import java.util.Locale
24+
2725
@RestController
2826
class UtilController {
29-
val parser: CronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
30-
val descriptor: CronDescriptor = CronDescriptor.instance(Locale.UK)
3127

3228
@GetMapping(path = Array("/util/quartzDetail"))
3329
def getQuartzDetail(@RequestParam expression: String): QuartzExpressionDetail = {
3430
try {
35-
val expressionDescription = descriptor.describe(parser.parse(expression))
31+
val description = CronExpressionDescriptor.getDescription(expression, new Options() {{
32+
setLocale(Locale.UK)
33+
setVerbose(true)
34+
}})
3635
QuartzExpressionDetail(
3736
expression = expression,
3837
isValid = true,
39-
explained = expressionDescription
38+
explained = description
4039
)
4140
} catch {
42-
case e: IllegalArgumentException => {
41+
case e: CronExpressionParseException => {
4342
QuartzExpressionDetail(
4443
expression = expression,
4544
isValid = false,
@@ -48,5 +47,4 @@ class UtilController {
4847
}
4948
}
5049
}
51-
5250
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/*
3+
* Copyright 2018 ABSA Group Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package za.co.absa.hyperdrive.trigger.api.rest.controller
18+
19+
import org.scalatest.mockito.MockitoSugar
20+
import org.scalatest.{BeforeAndAfter, FlatSpec, Matchers}
21+
import za.co.absa.hyperdrive.trigger.api.rest.controllers.UtilController
22+
23+
class UtilControllerTest extends FlatSpec with Matchers with MockitoSugar with BeforeAndAfter {
24+
private val underTest = new UtilController
25+
26+
"getQuartzDetail" should "return a human-readable description, incl. month" in {
27+
// given
28+
val expression = "0 0 2 2 * ?"
29+
30+
// when
31+
val result = underTest.getQuartzDetail(expression)
32+
33+
// then
34+
result.expression shouldBe expression
35+
result.isValid shouldBe true
36+
result.explained shouldBe "At 02:00, on day 2 of the month"
37+
}
38+
39+
it should "return a verbose human-readable description" in {
40+
// given
41+
val expression = "0 0/20 * ? * * *"
42+
43+
// when
44+
val result = underTest.getQuartzDetail(expression)
45+
46+
// then
47+
result.expression shouldBe expression
48+
result.isValid shouldBe true
49+
result.explained shouldBe "Every 20 minutes, every hour, every day"
50+
}
51+
52+
it should "return an error description if the cron expression is malformed" in {
53+
// given
54+
val expression = "0 0 2 2 * MON"
55+
56+
// when
57+
val result = underTest.getQuartzDetail(expression)
58+
59+
// then
60+
result.expression shouldBe expression
61+
result.isValid shouldBe false
62+
result.explained shouldBe "Specifying both a Day of Month and Day of Week is not supported. Either one or the other should be declared as \"?\""
63+
}
64+
65+
}

0 commit comments

Comments
 (0)