-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fs2-cron-cron-utils module (#412)
- Loading branch information
Showing
7 changed files
with
172 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
modules/cron-utils/src/main/scala/eu/timepit/fs2cron/cronutils/CronUtilsScheduler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2018-2021 fs2-cron contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.timepit.fs2cron.cronutils | ||
|
||
import cats.effect.{Sync, Temporal} | ||
import com.cronutils.model.time.ExecutionTime | ||
import eu.timepit.fs2cron.{Scheduler, ZonedDateTimeScheduler} | ||
|
||
import java.time.{ZoneId, ZoneOffset, ZonedDateTime} | ||
|
||
object CronUtilsScheduler { | ||
def systemDefault[F[_]](implicit temporal: Temporal[F], F: Sync[F]): Scheduler[F, ExecutionTime] = | ||
from(F.delay(ZoneId.systemDefault())) | ||
|
||
def utc[F[_]](implicit F: Temporal[F]): Scheduler[F, ExecutionTime] = | ||
from(F.pure(ZoneOffset.UTC)) | ||
|
||
def from[F[_]](zoneId: F[ZoneId])(implicit F: Temporal[F]): Scheduler[F, ExecutionTime] = | ||
new ZonedDateTimeScheduler[F, ExecutionTime](zoneId) { | ||
override def next(from: ZonedDateTime, schedule: ExecutionTime): F[ZonedDateTime] = | ||
schedule.nextExecution(from).map[F[ZonedDateTime]](zdt => F.pure(zdt)).orElse { | ||
val msg = s"Could not calculate the next date-time from $from " + | ||
s"given the cron expression '$schedule'." | ||
F.raiseError(new Throwable(msg)) | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
modules/cron-utils/src/test/scala/eu/timepit/fs2cron/cronutils/CronUtilsSchedulerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2018-2021 fs2-cron contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.timepit.fs2cron.cronutils | ||
|
||
import cats.effect.IO | ||
import com.cronutils.model.CronType | ||
import com.cronutils.model.definition.CronDefinitionBuilder | ||
import com.cronutils.model.time.ExecutionTime | ||
import com.cronutils.parser.CronParser | ||
import fs2.Stream | ||
import munit.CatsEffectSuite | ||
|
||
import java.time.{Instant, ZoneId, ZoneOffset} | ||
|
||
class CronUtilsSchedulerTest extends CatsEffectSuite { | ||
private val cronDef = CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING) | ||
private val parser = new CronParser(cronDef) | ||
|
||
private val everySecond = ExecutionTime.forCron(parser.parse("* * * ? * *")) | ||
private val evenSeconds = ExecutionTime.forCron(parser.parse("*/2 * * ? * *")) | ||
|
||
private def isEven(i: Long): Boolean = i % 2 == 0 | ||
private def instantSeconds(i: Instant): Long = i.getEpochSecond | ||
private val evalInstantNow: Stream[IO, Instant] = Stream.eval(IO(Instant.now())) | ||
|
||
private val schedulerSys = CronUtilsScheduler.systemDefault[IO] | ||
private val schedulerUtc = CronUtilsScheduler.utc[IO] | ||
|
||
test("awakeEvery") { | ||
val s1 = schedulerSys.awakeEvery(evenSeconds) >> evalInstantNow | ||
val s2 = s1.map(instantSeconds).take(2).forall(isEven) | ||
assertIO(s2.compile.last, Some(true)) | ||
} | ||
|
||
test("sleep") { | ||
val s1 = schedulerUtc.sleep(evenSeconds) >> evalInstantNow | ||
val s2 = s1.map(instantSeconds).forall(isEven) | ||
assertIO(s2.compile.last, Some(true)) | ||
} | ||
|
||
test("schedule") { | ||
val s1 = schedulerSys | ||
.schedule(List(everySecond -> evalInstantNow, evenSeconds -> evalInstantNow)) | ||
.map(instantSeconds) | ||
|
||
for { | ||
seconds <- s1.take(3).compile.toList | ||
_ = assertEquals(seconds.count(isEven), 2) | ||
_ = assertEquals(seconds.count(!isEven(_)), 1) | ||
} yield () | ||
} | ||
|
||
test("timezones") { | ||
val zoneId: ZoneId = ZoneOffset.ofTotalSeconds(1) | ||
val scheduler = CronUtilsScheduler.from(IO.pure(zoneId)) | ||
|
||
val s1 = scheduler.awakeEvery(evenSeconds) >> evalInstantNow | ||
val s2 = s1.map(instantSeconds).take(2).forall(!isEven(_)) | ||
assertIO(s2.compile.last, Some(true)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import sbt._ | ||
|
||
object Dependencies { | ||
val calevCore = "com.github.eikek" %% "calev-core" % "0.6.4" | ||
val cron4s = "com.github.alonsodomin.cron4s" %% "cron4s-core" % "0.6.1" | ||
val cronUtils = "com.cronutils" % "cron-utils" % "9.2.0" | ||
val fs2Core = "co.fs2" %% "fs2-core" % "3.6.1" | ||
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.15" | ||
val calevCore = "com.github.eikek" %% "calev-core" % "0.6.4" | ||
val munitCatsEffect = "org.typelevel" %% "munit-cats-effect-3" % "1.0.7" | ||
} |