Running crontabs with an Akka ActorSystem without a ton of dependencies
See (Wikipedia)[https://en.wikipedia.org/wiki/Cron]
- Uses Java 8 Time API for data and time calculations
- Slf4j for common logging support
- Minimalized dependencies - KISS
- Available for Scala 2.11, 2.12 and 2.13
resolvers in ThisBuild += Resolver.jcenterRepo
plus
libraryDependencies ++= “me.lightspeed7” %% “akka-crontab” % “0.3.3”
or
libraryDependencies ++= “me.lightspeed7” %% “akka-crontab-streams” % “0.3.3”
Cron object can be constructed from various helper methods and Raw construction
import me.lightspeed7.crontab.Crontab._
val daily = Crontab.daily
val hourly = Crontab.hourly
val everyDayAt = Crontab.everyDayAt(12)
val everyHourOnTheHour = Cron(Fixed(0), Every, Every, Every, Every)
import me.lightspeed7.crontab._
val parsed1: Try[Cron] = cron"1 * * * *"
val parsed2: Try[Cron] = Crontab.apply("1 * * * *")
import me.lightspeed7.crontab._
import scala.concurrent.duration._
cron"".map { implicit cron =>
val nextRunTime: Future[LocalDateTime] = Schedule.nextScheduledTime(LocalDateTime.now, 5 seconds)
// ...
}
import me.lightspeed7.crontab._
class CronActor(cron: Cron) extends Actor {
val scheduler = context.actorOf(Props(classOf[ScheduleActor], CronConfig(self, cron)))
def receive: Actor.Receive = {
case time: LocalDateTime =>
// cron needs to run
...
}
}
import me.lightspeed7.crontab._
val cron: Cron = ...
val sink = Sink.foreach[LocalDateTime] { dt => println(s"Fired - $dt") }
val src: Source[LocalDateTime, NotUsed] = StreamSource.create(cron)
src.runWith(sink) // run your stream