1

I'm trying to implement a carmine worker in a constantly running process.

When launching the following app with lein run myclass.foo, it just starts the worker and stops it right away.

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


(defn -main []
  (car-mq/start my-worker))

My goal is something like that

  • Launch the foo listener
  • foo listener runs in foreground and prints everything that gets posted to the queue
  • Ctrl-c / quit will close the listener
patchrail
  • 2,007
  • 1
  • 23
  • 33
  • Do you want to run it in background or in foreground? – Leonid Beschastny Apr 30 '15 at 16:55
  • I think you should use convenient tools to tun you provess as a daemon. Here is a pretty good instruction how to do it: [Run php script as daemon process](http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process). I would especially recommend using something like supervisor or upstart. – Leonid Beschastny Apr 30 '15 at 16:57
  • But if, for example, you want to run a worker in a separate thread (in background) while doing some other stuff in a main thread (in foreground), then you should manage it manually from your clojure application, which is pretty easy to do. – Leonid Beschastny Apr 30 '15 at 17:11
  • @LeonidBeschastny as far as I know, carmine starts a background thread. As soon as it's started carmine returns true and the application proceeds and quits. I want to keep the app open until manually interrupted, so I guess a foreground process. – patchrail Apr 30 '15 at 23:58

1 Answers1

0

Running it with lein foo was the wrong approach. I edited the entire question to conform with the 'solution' I found.

The main problem was, that I was using lein run myclass.foo to run it. lein trampoline run myclass.foo launches the app's JVM and gets rid of leiningen's, with seems to be exactly what I need. When using trampoline instead of run, the app doesn't exit right away.

Step 2, to close the connection on ctrl-c is a simple shutdown hook

(.addShutdownHook (Runtime/getRuntime) (Thread. #(car-mq/stop my-worker)))
patchrail
  • 2,007
  • 1
  • 23
  • 33