Giter Club home page Giter Club logo

cron-stream's Introduction

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.

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.