title: Implementing a cron type scheduler in clojure
body: <p>I'm looking for any way for clojure that can trigger an event at a given time,</p>
  
  <p>For example: I want a particular process to start at 9:30am and then I can trigger another process to start running half an hour later etc.</p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <h3>Updated 2:</h3>
  
  <p>Thanks @arthur-ulfeoldt and @unknown-person who also suggested using <a href="https://github.com/samaaron/at-at">https://github.com/samaaron/at-at</a> before deleting his answer. The documentation is a little out of date but here's how I got going.</p>
  
  <pre>
  (use 'overtone.at-at)
  
  (def my-pool (mk-pool))
  ;=> make a thread pool
  
  (every 1000 #(println "I am super cool!") my-pool :initial-delay 2000) 
  ;=> starts print function every 1 sec after a 2 sec delay
  
  (stop *1) 
  ;=> stops it
  </pre>
  
  <p>So to make it start at exactly 9, with an interval of half an hour, I would do:</p>
  
  <pre>
  (require '[clj-time.core :as t])
  (require '[clj-time.coerce :as c])
  (use 'overtone.at-at)
  
  ;Make Thread Pool
  (def my-pool (mk-pool))
  
  (def current-time (t/now))
  
  (def current-date (t/date-time 
                      (t/year current-time)
                      (t/month current-time)
                      (t/day current-time)))
  
  (def next-9-oclock
    (if (> 9 (t/hour current-time))
      (t/plus current-date (t/hours 9))
      (t/plus current-date (t/days 1) (t/hours 9))))
  
  (def initial-delay
     (- (c/to-long next-9-oclock) (c/to-long current-time))
  
  (every 1800000 
         #(println "I am super cool!") 
         my-pool 
         :initial-delay 
         initial-delay) 
  
  </pre>
  
  <h3>Updated:</h3>
  
  <p>@arthur-ulfeoldt, I am not sure how to translate some of the java code into clojure. 
  <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html">http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html</a></p>
  
  <p>like:</p>
  
  <pre><code>final ScheduledFuture&lt;?&gt; beeperHandle = 
          scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>final Runnable beeper = new Runnable() {
          public void run() { System.out.println("beep"); }
       };
  </code></pre>
  
  <h3>Entire Example</h3>
  
  <pre>
   class BeeperControl {
      private final ScheduledExecutorService scheduler = 
         Executors.newScheduledThreadPool(1);
  
      public void beepForAnHour() {
          final Runnable beeper = new Runnable() {
                  public void run() { System.out.println("beep"); }
              };
          final ScheduledFuture beeperHandle = 
              scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
          scheduler.schedule(new Runnable() {
                  public void run() { beeperHandle.cancel(true); }
              }, 60 * 60, SECONDS);
      }
   }
  </pre>