跳过AOT的原因? [英] Reason for skipping AOT?

查看:115
本文介绍了跳过AOT的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多noir应用程序中,我看到下面的声明。跳过aot的目的是什么?何时使用它和何时不使用它?任何优点/缺点?

 :main ^ {:skip-aot true} sample-app.server 


解决方案

这不是特定于noir,但是一个方案你可能想跳过AOT



默认情况下,Heroku会对您的代码进行AOT编译,因此请考虑您的server.clj中的此代码段:

 (db / connect!(System / getenvDB_URL))

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


b $ b

原则上,这个代码似乎是无害的,并且不管它是否被AOT编译,都会在本地工作。



但是在heroku上编译时,环境变量DB_URL 不可用,所以上面的 connect!语句将尝试连接到 nil 并抛出异常。 / p>

跳过这个命名空间的AOT编译是防止这种情况的一种方法。



瞬间将稍微改变到这一点:

 (defn bootstrap! [] 
(db / connect!(System / getenvDB_URL)))

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



我学会了这种困难的方式,并记录在这篇博文



<希望这是有用的。


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

解决方案

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.

这篇关于跳过AOT的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆