dbuschman7 / akka-crontab

Running crontabs with an Akka ActorSystem without a ton of dependencies

Version Matrix

akka-crontab

Bintray

Running crontabs with an Akka ActorSystem without a ton of dependencies

See (Wikipedia)[https://en.wikipedia.org/wiki/Cron]

Summary

  • 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

Build setup

resolvers in ThisBuild += Resolver.jcenterRepo

plus

libraryDependencies ++= “me.lightspeed7” %% “akka-crontab” % “0.3.3”

or

libraryDependencies ++= “me.lightspeed7” %% “akka-crontab-streams” % “0.3.3”

Constructing Cron object

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) 

Parsing using StringInterpolator

import me.lightspeed7.crontab._

val parsed1: Try[Cron] = cron"1 * * * *"
val parsed2: Try[Cron] = Crontab.apply("1 * * * *")

Scheduling - finding the next time from the cron to run

import me.lightspeed7.crontab._
import scala.concurrent.duration._
cron"".map { implicit cron =>
  val nextRunTime: Future[LocalDateTime] = Schedule.nextScheduledTime(LocalDateTime.now, 5 seconds)
  // ...
}

Running a Cron with Akka Actor

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
    ...  
  }
}

Running a Cron as an Akka Stream Source

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