newBieDev
newBieDev

Reputation: 504

Clojure - Permanently Running Application?

I've seen some similar questions, but nothing that was very specific and being a new Clojure dev wasn't sure if any of them were best practice.

Basically, I have an application that every couple of seconds needs to hit a database and iterates through each item. Then, depending on certain conditions needs to spawn a new thread and perform some work on the items which will then be inserted into a separate database.

My question is what is the best way to start and run an application like this in clojure, one that needs to be constantly running. My current idea is to just block the main thread, and then spawn a new one which will be my primary loop that then spawns the additional ones mentioned above. I'm not sure if this is the best way to accomplish this, or if I'm overlooking a better way to achieve my goals.

Upvotes: 0

Views: 122

Answers (2)

user235273
user235273

Reputation:

It depends on how you want the app to be. You can he have a quartz scheduler within and have jobs which will do the query. Since the app needs to query db every second or so, have db pooling. You can use futures to spawn additional worker threads.

Another way would be to have the scheduler outside the app and use cron job to call the app or endpoint.

If you want a distributed service altogther, you can use onyx. Again, it depends on how complex the worker tasks would be and use cases.

Upvotes: 0

Wout Neirynck
Wout Neirynck

Reputation: 314

I don't see any reason to spawn a new thread from the main, just to wait on it. You can just run the "main loop" from your main thread, like so:

(defn spawn-db-thread! []
  (doto (Thread. #(println "Doing something with the db here..."))
    (.start)))

(defn run-loop [max]
  (loop [n max]
    (when (pos? n)
      (spawn-db-thread!)
      (Thread/sleep 1000)
      (recur (dec n)))))

This snippet will run for max times, and on each iteration it will start a thread that (in this case) just prints something to the console. It will then sleep for a second. I just added the max argument for testing purposes, you can leave it out if you want.

Of course, if you want to build something fancy, you should look into the core.async library.

Also, maybe it's not really necessary to do your db processing in a separate thread, I'd just run it on the main thread. But that's up to you.

Upvotes: 1

Related Questions