Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

Alarm Function

Simple description:

The alarm function is a feature, that call a function every period of time .


(defn function
  "This function will be called every 1 minute 10 times"
  (println "Hello, world!"))

 (defn -main 
  [& args]
  (alarm 1 10 function))

Simple use case:

When a user create account and not confirmed email. through the alarm verify if the user really not confirmed

1 Answer

+1 vote

Josue, take a look at java Timers

There is also many ways to do that with clojure: using future, agents, or core.async.

Put this into the clojure.core will bring an unnecessary complexity and cost of maintenance.

There is also some design choices, like, it will run on a thread pool? it will wait the task to terminate to start to count the timeout? it can be blocking?

Depending on which set of choices that you pick, it would make harder to port this function to others (future) platforms: javascript do not support blocking, arduino do not have system clock...

So feel free to create your our josue.timer API or using some already existing

Here a code using java timers:

(let [every-5-sec (java.time.Duration/parse "PT5S")
      ok-task (proxy [java.util.TimerTask] []
                (run []
                  (prn :ok)))
      ;; save this return for later
      timer-task (doto (java.util.Timer.)
                   (.schedule ok-task
                     (.toMillis every-5-sec)
                     (.toMillis every-5-sec)))]
  #_(.cancel ok-task)
  #_(.cancel timer-task)