resizer / src / resizer / consumer.clj

(ns resizer.consumer
  (:refer-clojure :exclude [declare get])
  (:import [com.rabbitmq.client Connection]
           java.io.IOException)
  (:use [clojure.data.json :only [read-json json-str]]
        [langohr.core  :as lhc]
        [langohr.consumers :as lhcons]
        [langohr.queue :as lhq]
        [langohr.basic :as lhb]
        [claude.core :as cf]
        [resizer.resizer :as rz]))

(defn rabbitmq-connect []
  (if (cf/cloudfoundry?)
    (lhc/connect (lhc/settings-from (cf/rabbit-url)))
    (lhc/connect)))

(defonce ^Connection conn (rabbitmq-connect))

;; TODO if resize fails publish a message or log that it failed to process the image.
(defn process-message [ch metadata payload]
  (let [jsondata (read-json (String. payload))
        filename (:filename jsondata)]
    (rz/process-image filename)
    (lhb/publish ch "cloudstagram-new-image" "" payload :content-type "application/json" )))

(defn start-consumer []
  (let [channel     (.createChannel conn)
        exchange    "cloudstagram-upload"
        queue       "resize_queue"
        shutdown-ln (lhc/shutdown-listener (fn [cause]
                                             (println (str "Shutdown listener has fired: " cause))))]
    (try
      (.addShutdownListener channel shutdown-ln)
      (lhq/declare channel queue :durable true :auto-delete false)
      (lhq/bind channel queue exchange)
      (.start (Thread. #(lhcons/subscribe channel queue process-message :auto-ack true) "resize/consumer"))
      (catch IOException ioe ;; see http://www.rabbitmq.com/api-guide.html#shutdown
        nil))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.