time-based job scheduler


License
EPL-1.0

Documentation

aika

Clojure time-based job scheduler. Jobs are scheduled using psuedo cron-tabs

"Equation-wise the first thing to do is to consider time as officially ended. 
 We work on the other side of time."

— Sun Ra

Build Status

Install

Add the following dependency to your project.clj file:

[aika "0.0.33-alpha"]

Cron

The namespace for the date side of things is aika.cron

(:require [aika.cron :refer :all]))

Aika's cron tabs have 5 arguments:

  • minute between 0 - 59
  • hour between 1 - 24
  • day between 1 - 30
  • month between 1 - 12
  • weekday between 0 - 6

Optionally, a DateTime instance can be provided as the first argument

Examples

Run on the 50th minute of every hour of every day of every month

(valid-tab? [50 * * * *]) ;; true

Run on minutes divisible by 10: 0,10,20,30...

(valid-tab? [(%minutes 10) * * * *]) ;; true

Run every three hours on Sunday's and Monday's

(valid-tab? [* (%hours 3) * * (range 0 2)]) ;; true

Schedule every three days from 1969

(parse-tab [(clj-time.core/data-time 1969) * * (%days 3) * *])
;; #<DateTime 1969-01-03...>

Tab->msec

(tab->msec [(%minutes 1) * * * *]) ;; 60000

Job

The namespace for the job scheduling side of things is aika.core

(:require [aika.core :refer :all]
          [aika.cron :refer :all])

Defining a job

You can define a job using the defjob macro

Here is an example of a job that prints 'Hfeflflfo' every minute

(defjob hello-world [(%minutes 1) * * * *]
            (println "Hfeflflfo")
 ;; #'user/hello-world

Scheduling a job

You can schedule a job by calling the schedule function

This function starts a job and returns the status map

(schedule hello-world)

You can check to see if a job has been scheduled by calling the scheduled? function

(scheduled? hello-world) ;; true

Job status

You can retrieve a job's status by calling the status function

This returns a map containing:

  • :cycles - the number of times the task has run
  • :scheduled? - has the job been scheduled?
  • :scheduled-last - DateTime instance of when the last task was run
  • :scheduled-next - DateTime instance of when the next task will run
(status hello-world) ;; {:cycles 0, :scheduled :true...}

Stopping a job

You can stop a job by calling the stop function

(stop hello-world)

License

Copyright © 2014 Kibu

Distributed under the Eclipse Public License version 1.0