Fork me on GitHub
#clojure
<
2020-01-19
>
matheusashton00:01:12

Hello everyone! I’m starting to work with clojure, and I’m having some questions about project organization, I have a project with compojure and monger, how to you usually organize the files? How do I handle the database connection among the application? Do I have to connect on every request or do I connect on application start and reuse the connection?

hiredman05:01:56

Use some kind of connection pool (for sql databases that would be something like c3p0). For namespace organization prefer more namespaces to fewer. Split your namespaces as they grow. A few hundred lines is a pretty nice size for a namespace, but sometimes they are a few thousand. For runtime state use something like component(https://github.com/stuartsierra/component)

sogaiu14:01:50

mark bastian's most recent conj talk might be worth examining - there is an associated repository with example code bringing various pieces together. he used integrant - which some might say is component-like. https://github.com/markbastian/conj2019

Jeremy06:01:40

Hi folks, hope you're all well. I just spent the better part of a day debugging an Agents problem (and learning a lot in the process)... Is there any natural way to add a timeout to clojure.core/shutdown-agents? I needed to resort to this:

Jeremy06:01:44

(doseq [^ExecutorService e [clojure.lang.Agent/soloExecutor
                            clojure.lang.Agent/pooledExecutor]]
  (.shutdown e)
  (when-not (.awaitTermination e 5 java.util.concurrent.TimeUnit/SECONDS)
    (log/debug "forcing" e)
    (.shutdownNow e))))

didibus22:01:25

No, but you can use (await) with a timeout

didibus22:01:10

(await-for timeout-ms & agents)

Jeremy03:01:14

oh thank you. my specific use case is in shutting down cljfx gracefully and i don’t think i have direct access to its agents for that argument. maybe (on-fx-thread (await-for...))

Jeremy23:01:52

i’m so dumb, had effectively been trying to shutdown-agents from within an agent thread... fixed with simple (.start (Thread. shutdown-agents))