Coder Social home page Coder Social logo

cronish's Introduction

Cronish Core

Note: I moved the cronish-sbt plugin and the cronish-app into their own repositories.

The cronish library can be used to determine the cron lingo from any given string. This library is clearly a work in progress, but it works for the most part.

Basic Syntax

A typical cron job is said to run every something where "something" represents a single field in cron or multiple fields strung together. An example would be:

Every day at midnight

This dsl library makes that assumption as well... in fact all are legitimate cron definitions:

"Every day at midnight".crons == "0 0 * * *"
"Every 15 minutes at midnight on the weekend".crons == "*/15 0 * * 0,6"
"Every other minute in July at noon on the weekday".crons == "*/2 12 * 7 1-5"
"Every 1st day in April at midnight".crons == "0 0 1 4 *"
"Every day on the weekday at 3:30".crons == "30 3 * * 1-5"

Let's take this excerpt from the cron article on Wikipedia:

In 2003 CE on the 11th to 26th of each month in January to June every third minute starting from 2 past 1am, 9am and 10pm

Using cronish, we can almost write this verbatim...

"Every 3 minutes in the year 2003 on the 11th to 26th day in January to June at 1am, 9am, and 10pm".cron

Cron("0", "*/3", "1,9,22", "11-26", "1-6", "*", "2003")

Determining the Next Run

A Cron object created from an expression, or created manually, can determine its next run from now, or a specific time in the future. Here's a few examples of this:

import cronish._
import cronish.dsl._

val cron = "every 10 days".cron
val now = Scalendar.now

println(cron.nextFrom(now)) // returns milliseconds
println(cron.nextFrom(now + 12.days)) // can advance

println(cron.nextTime) // returns a Scalendar object
println(now to cron.nextTime) // This is obviously a duration

println(cron.next) // returns milliseconds from Scalendar.now

println(cron.nextFrom(cron.nextTime)) // The next-next run

Creating a Cron Job

Cron jobs are created via dsl language as well. The syntax borrows heavily from sbt task creation.

val payroll = task {
  println("You have just been paid... Finally!")
}

// Yes... that's how you run it 
payroll executes "every last Friday in every month"

A job can easily do the following:

  • delayed start
  • exact start
  • reset

An example of the three below:

val greetings = job (println("Hello there")) describedAs "General Greetings"

// give a delayed start
val delayed = greetings runs "every day at 7:30" in 5.seconds

// give an exact time to start
val exact = greetings runs "every day at noon" starting now + 1.week

// resets a job to its definition 
val reseted = exact.reset()

reseted starting now + 1.day

A cron task can optionally handle exceptions, a pre-start hook, and a ending hook. Take a look below:

// These are formed in the task definition phase
val spoken = job (println ("My name is Philip Cali."))

val greetings = spoken starts (println("Hi, there...")) ends (println("How are you?"))

greetings runs "every second" exactly 2.times

/**
 * Hi, there...
 * My name is Philip Cali.
 * My name is Philip Cali.
 * How are you?
 */

val badJob = job {
  throw new RuntimeException("Something terrible happened")
} catches {
  case e: RuntimeException => // Report this... rollback... whatever
  case _ => Scheduled.destroyAll()
}

badJob runs "every 5 seconds"

Cron Management

All jobs described previously are loaded up in a singleton Cron manager via the Scheduled object.

// Gets a list of all active jobs
Scheduled.active
Scheduled.active.foreach(_.stop)

// Destroys all jobs (same as above)
Scheduled.shutdown()

// Destroys a single instance
Scheduled.stop(Scheduled.active(0))

Installation

libraryDependencies += "com.github.philcali" %% "cronish" % "0.1.3"

cronish's People

Contributors

philcali avatar aleitner avatar earldouglas avatar mortimerp9 avatar

Watchers

Lior Harel avatar James Cloos avatar Liel Ran avatar Tomer Praizler avatar

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.