Skip to content

Commit

Permalink
Replace futures with threads because they're easier to manage and I d…
Browse files Browse the repository at this point in the history
…on't care about it ever returning a value.
  • Loading branch information
jeremyheiler committed Apr 18, 2012
1 parent f63ac74 commit 0c28087
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/trabajo/core.clj
@@ -1,6 +1,11 @@
(ns trabajo.core (ns trabajo.core
(:require [redis.core :as redis])) (:require [redis.core :as redis]))


; todo
; - fn to kill and remove workers
; - fn to replace dead workers
; - job timeouts

(def redis-conf (atom {:host "localhost" :port 6379})) (def redis-conf (atom {:host "localhost" :port 6379}))


(defmacro with-redis (defmacro with-redis
Expand Down Expand Up @@ -31,26 +36,27 @@


(def workers (ref {})) (def workers (ref {}))


(defn ^:private init-future [queue] (defn ^:private process [queue]
(future (loop []
(loop [] (when-not (Thread/interrupted)
(when-not (Thread/interrupted) (if-let [job (dequeue queue)]
(if-let [job (dequeue queue)] (apply-job job)
(apply-job job) (Thread/sleep 5000))
(Thread/sleep 5000)) (recur))))
(recur)))))


(defn work-on (defn work-on
"Returns a future that polls the given queue until cancelled." "Returns a thread that polls the given queue until interrupted."
[queue] [queue]
(dosync (dosync
(let [f (init-future queue)] (let [t (Thread. #(process queue))]
(alter workers update-in [queue] #(conj (if (nil? %) [] %) f))))) (alter workers update-in [queue] #(conj (if (nil? %) [] %) t))
(.start t))))


(defn test-work [x] (defn test-work [x]
(with-open [out (java.io.FileWriter. "/home/jeremy/job.out" true)] (with-open [out (java.io.FileWriter. "/home/jeremy/job.out" true)]
(.write out (str x "\n")))) (.write out (str x "\n"))))



;(defn inc-workers [queue-key incr] ;(defn inc-workers [queue-key incr]
; (dosync ; (dosync
; (alter queues update-in [queue-key :workers] (fn [old] ; (alter queues update-in [queue-key :workers] (fn [old]
Expand Down

0 comments on commit 0c28087

Please sign in to comment.