Skip to content

Commit

Permalink
Merge pull request #1 from cagizoz/add-timezone-support-for-crone-sch…
Browse files Browse the repository at this point in the history
…edule

Add support for timezone schedulling
  • Loading branch information
jkobejs authored Nov 15, 2022
2 parents 28ca12f + 61887f1 commit 753724f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
4 changes: 2 additions & 2 deletions zio1/src/main/scala/io/github/jkobejs/cron/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ package object cron {
zoneId: ZoneId = ZoneId.systemDefault()
): ZIO[R with Clock, E, Long] = {
val getSleepFor: ZIO[Clock, Nothing, Option[Duration]] = for {
currentTime <- zio.clock.localDateTime.orDie
currentTime <- zio.clock.localDateTime.map(_.atZone(ZoneId.systemDefault())).orDie
} yield optionalToOption(
ExecutionTime
.forCron(expression)
.timeToNextExecution(currentTime.atZone(zoneId))
.timeToNextExecution(currentTime.withZoneSameInstant(zoneId))
)

for {
Expand Down
26 changes: 26 additions & 0 deletions zio1/src/test/scala/io/github/jkobej/cron/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ object Tests extends DefaultRunnableSpec {
_ <- TestClock.adjust(Duration.ofSeconds(1))
either <- fork.join
} yield assert(either)(Assertion.isLeft(Assertion.equalTo(error)))
},
testM("runs at specific time within other zone") {
val cron = CronBuilder
.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withSecond(FieldExpressionFactory.on(0))
.withMinute(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(5))
.withDoM(FieldExpressionFactory.on(7))
.withDoW(FieldExpressionFactory.questionMark())
.withMonth(FieldExpressionFactory.always())
.withYear(FieldExpressionFactory.on(2022))
.instance()

for {
datesRef <- Ref.make[List[LocalDateTime]](Nil)
eff = for {
date <- zio.clock.localDateTime.orDie
_ <- datesRef.update(dates => date :: dates)
} yield ()
_ <- TestClock.setTime(Duration.between(Instant.EPOCH, startInstant))
_ <- TestClock.setTimeZone(ZoneId.systemDefault())
fork <- eff.repeatWithCron(cron, ZoneId.of("Europe/London")).fork
_ <- TestClock.adjust(Duration.ofDays(1)).repeatN(days)
runs <- fork.join
dates <- datesRef.get
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == (1 to 12).map(dateTime.withMonth).toSet)
}
)
}
4 changes: 2 additions & 2 deletions zio2/src/main/scala/io/github/jkobejs/cron/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ package object cron {
zoneId: ZoneId = ZoneId.systemDefault()
): ZIO[R, E, Long] = {
val getSleepFor: ZIO[Any, Nothing, Option[Duration]] = for {
currentTime <- zio.Clock.localDateTime
currentTime <- zio.Clock.localDateTime.map(_.atZone(ZoneId.systemDefault()))
} yield optionalToOption(
ExecutionTime
.forCron(expression)
.timeToNextExecution(currentTime.atZone(zoneId))
.timeToNextExecution(currentTime.withZoneSameInstant(zoneId))
)

for {
Expand Down
26 changes: 26 additions & 0 deletions zio2/src/test/scala/io/github/jkobej/cron/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ object Tests extends ZIOSpecDefault {
_ <- TestClock.adjust(Duration.ofSeconds(1))
either <- fork.join
} yield assert(either)(Assertion.isLeft(Assertion.equalTo(error)))
},
test("runs at specific time within timezone") {
val cron = CronBuilder
.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withSecond(FieldExpressionFactory.on(0))
.withMinute(FieldExpressionFactory.on(5))
.withHour(FieldExpressionFactory.on(5))
.withDoM(FieldExpressionFactory.on(7))
.withDoW(FieldExpressionFactory.questionMark())
.withMonth(FieldExpressionFactory.always())
.withYear(FieldExpressionFactory.on(2022))
.instance()

for {
datesRef <- Ref.make[List[LocalDateTime]](Nil)
eff = for {
date <- zio.Clock.localDateTime
_ <- datesRef.update(dates => date :: dates)
} yield ()
_ <- TestClock.setTime(startInstant)
_ <- TestClock.setTimeZone(ZoneId.systemDefault())
fork <- eff.repeatWithCron(cron, ZoneId.of("Europe/London")).fork
_ <- TestClock.adjust(Duration.ofDays(1)).repeatN(days)
runs <- fork.join
dates <- datesRef.get
} yield assertTrue(runs == 12L) && assertTrue(dates.toSet == (1 to 12).map(dateTime.withMonth).toSet)
}
)
}

0 comments on commit 753724f

Please sign in to comment.