/cron-stream

Manifold-based cron streams

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

cron-stream

Manifold-based cron streams.

Artifacts

cron-stream artifacts are released to Clojars. To use with Leiningen:

Clojars Project

Usage

cron-stream takes a 5-field cron expression and returns a stream that emits Dates periodically, according to the supplied schedule. Here are some sample cron expressions:

Expression Description
"0 * * * *" the top of every hour of every day
"*/10 * * * *" every ten minutes
"0 8-10 * * *" 8, 9 and 10 o'clock of every day
"0/30 8-10 * * *" 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day
"0 9-17 * * MON-FRI" on the hour nine-to-five weekdays
"0 0 25 12 ?" every Christmas Day at midnight

Additionally, cron-stream takes the following option keys:

Option Description Default
:timezone evaluate the cron expression in a givenTimeZone local system timezone
:buffer buffer size of the returned stream 0

It returns a manifold stream containing Dates. If the stream parks for an extended period of time, the next date will be computed based on when the stream resumes accepting put!s. To prevent missing cron events, use a non-zero buffer value. Note: the initial Date is calculated from the time the stream is created.

To create a stream that emits dates every three minutes:

(require '[manifold.deferred :as d]
         '[manifold.stream :as s]
         '[com.joshuagriffith.cron-stream :refer [cron-stream]])

(dotimes [_ 3]
  (println @(s/take! (cron-stream "*/3 * * * *"))))

This will print dates every three minutes (on the minute):

#inst "2015-04-16T05:51:00.000-00:00"
#inst "2015-04-16T05:52:00.000-00:00"
#inst "2015-04-16T05:53:00.000-00:00"

Manifold streams interoperate with core.async. To connect a stream to a channel, use connect:

(require '[clojure.core.async :refer [chan go-loop <!]])

(def cs (cron-stream "*/3 * * * *"))
(def ch (chan))

(s/connect cs ch)

(go-loop [i 0]
  (when (< i 3)
    (println (<! ch))
    (recur (inc i))))

Streams can also be treated as lazy seqs:

(take 5 (s/stream->seq cs))

Changes

  • 1.0.0: BREAKING: drop second support and switch to cronus
  • 0.2.1: improve performance and update manifold dependency
  • 0.2.0: remove max-error option and add buffer option
  • 0.1.1: fix classpath
  • 0.1.0: initial release

License

Copyright © 2015 Joshua Griffith

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.