Liking cljdoc? Tell your friends :D

Do this Do that

A lightweight & effective Clojure periodic task dispatcher.

Why?

Inspired by the simplicity of at-at this library was created to address some of its issues:

  • Dependency on clojure 1.3.0
  • Reflection
  • Silent error swallowing
  • ScheduledThreadPoolExecutor which is suited for more heavy-duty tasks

Some of the other available Clojure libraries didn't quite fit, they either:

  • Require java interop usage
  • Depend on the core.async thread pool
  • Bloatware

Setup

Add the following to project.clj dependencies:

[dtdt "0.2.0"]

Usage

Create a timer - each timer contains a single thread to run tasks.

(def t (create-timer))

Next, assign a task to the timer. Task will start running periodically right away.

(def some-task (every t 5000 #(println "Hello there..")))
=> #'dtdt.core/some-task
Hello there..
Hello there..
...

Assign as many tasks as you wish to a timer, make sure each function's execution is short to prevent hogging the Timer's thread.

Tasks are cancellable.

Hello there..
Hello there..
(cancel some-task)
=> true

Notes:

  • Timers can also be cancelled using the same cancel function.
  • Canceling a timer will cancel all of its running tasks.

Initial delay

By default, new tasks will execute immediately. Optionally, you may pass an initial delay (or even a Date).

; will execute f after 5 seconds and then every 10 milliseconds
(every t 10 f 5000)

last execution time

Return the last execution time of a task in epoch-time.

(last-execution-time task)
=> 1601223864378

One-off

Execute a task in X milliseconds from now.

(in t 100 #(println "you will see me only once!"))

Exceptions

When an exception from a task is thrown it will be printed.

(def bad-task (every t 5000 #(throw (Exception. "Something is broken"))))
caught exception in dtdt: Something is broken
=> #'dtdt.core/bad-task1
caught exception in dtdt: Something is broken
caught exception in dtdt: Something is broken

Optionally, pass an exception handler to alter the default behaviour.

(let [task #(throw (Exception. "Something broke again"))
      ex-handler (fn [e] (log/error (.getMessage e)))] 
  (every t 2000 task 0 ex-handler))



License

Copyright © 2020
Distributed under the Eclipse Public License

Can you improve this documentation? These fine people already did:
Chorinb & chorin1
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close