taoensso / carmine

Redis client + message queue for Clojure
https://www.taoensso.com/carmine
Eclipse Public License 1.0
1.16k stars 131 forks source link

Problems with keeping carmine mq listener in foreground #132

Closed dvcrn closed 9 years ago

dvcrn commented 9 years ago

Hey Peter!

apologies if this is something that shouldn't be posted here but I wasn't able to find help anywhere else. It is probably a trivial task but I am still kind of new to clojure.

I'm trying to implement a simple carmine listener in foreground with the example provided in the README:

(def my-worker
  (car-mq/worker queue-server "my-queue"
   {:handler (fn [{:keys [message attempt]}]
               (println "Received" message)
               {:status :success})
    :auto-start false
    :nthreads 100}))

(defn -main []
  (.addShutdownHook (Runtime/getRuntime) (Thread. #(car-mq/stop my-worker)))
  (car-mq/start my-worker))

Running the app results in

INFO [taoensso.carmine.message-queue] - Message queue worker starting: my-queue
INFO [taoensso.carmine.message-queue] - Message queue worker stopped: my-queue

I tried changing -main to

(defn -main []
  (.addShutdownHook (Runtime/getRuntime) (Thread. #(car-mq/stop my-worker)))
  (car-mq/start my-worker)
  (while true (Thread/sleep 1000)))

But this change resulted in a long delay between when the message is submitted and when it is being received (10-20s in my tests).

ptaoussanis commented 9 years ago

Hi David,

Running the app results in

How are you running the app? That'll determine whether the application stays open or exits immediately. Carmine's mq worker threads are not daemon threads, so they'll not prevent shutdown of the application on their own.

You might want something like: (nohup lein run 1>>logs/my-app 2>&1 &), etc.

Does that help?

dvcrn commented 9 years ago

phew, that took a lot longer than expected. The hint that it's actually related to how I run my application was the important one.

In my development environment, I was using a simple lein run -m my.foo since that's what I was using for most of my tasks. I read a bit through lein and turns out that using lein trampoline is doing exactly what I need.

The delay part was related to carmine options. Found the answer in this issue here - https://github.com/ptaoussanis/carmine/issues/103

Thanks a lot for the help! :)

ptaoussanis commented 9 years ago

No problem, happy you found a solution! Best of luck with your project, cheers :-)