Reason for skipping AOT?
Asked Answered
U

1

31

In many noir apps I have seen the below declaration. What is the purpose of skipping aot ? When to use it and when not to use it ? Any advantages / disadvantages ?

:main ^{:skip-aot true} sample-app.server
Unbearable answered 24/6, 2012 at 2:2 Comment(1)
clojure.org/compilation That answers the above question.Unbearable
F
32

This isn't specific to noir but one scenario you might want to skip AOT for a given namespace is when deploying your code to a PaaS provider such as heroku.

Heroku performs AOT compilation of your code by default so consider this snippet in your server.clj:

(db/connect! (System/getenv "DB_URL"))

(defn start [port]
  (run-jetty app {:port port :join? false :max-threads 100}))

In principle this code seems harmless and will work locally regardless of it being AOT-compiled.

However during compilation on heroku, the environment variable "DB_URL" isn't available yet so the connect! statement above will try to connect to nil and throw an exception.

Skipping AOT compilation of this namespace is one way of preventing this.

Another, and my preferred approach at the moment would be to change it slightly to this:

(defn bootstrap! []
  (db/connect! (System/getenv "DB_URL")))

(defn start [port]
  (bootstrap!)  
  (run-jetty app {:port port :join? false :max-threads 100}))

That way it's a little clearer what your intention is and you avoid attempting a database connection during compilation.

I learned this the hard way and documented it in this blog post.

Hope this is useful.

Fornix answered 12/3, 2013 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.