Git Product home page Git Product logo

akka-crontab's Introduction

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

akka-crontab's People

Contributors

dbuschman7 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

fernandoracca

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.