Clojure on CloudFoundry

In this article, I will show you how to develop and deploy clojure web application on CloudFoundry. As you may know, CloudFoundry is an opensource PaaS backed by VMWare. Java, Ruby and Nodejs are officially supported. As a JVM language, clojure is born to be also available on this platform, although it’s not listed.

CloudFoundry accepts a .war file for Java web application deployment. So you don’t need the ring-jetty-adaptor and a procfile as heroku requires. To help your development and deployment, I strongly recommend the lein-ring plugin:

  :dev-dependencies [[lein-ring "0.5.4"]]

CloudFoundry provides backend services like mysql, redis, mongodb and more. The connection information are stored as environment variables. Here you can find a subset of them.

Take mongodb as example, connection information (host, port, username and password) are encoded as JSON, stored in environment variables. You can get them with this function:

(defn mongo-config [key]
  (if-let [services (System/getenv "VCAP_SERVICES")]
    (let [services-dict (json/read-json services false)]
      (-> services-dict
        (get "mongodb-1.8")
        first
        (get "credentials")
        (get key)))))

In the server bootstrap function, create the mongodb connection:

(defn app-init []
  (def db-conn (make-connection
                 (or (mongo-config "db") "lazypress")
                   :host (or (mongo-config "hostname") "localhost")
                   :port (or (mongo-config "port") 27017)))
  (when-not (nil? (mongo-config "username"))
    (authenticate db-conn
      (mongo-config "username")
      (mongo-config "password")))

By adding check for nil, local databased is also supported. This is pretty convenience for local development. These environment variables are consistent on all cloudfoundry application, so it’s possible to deploy the application on multiple accounts without any changes.

Then you can add your web stuff just like standard clojure web development. (If you are using compojure, place your static files under resource/public.)

Finally, package it. (Suppose your application is named as “lazypress”)

lein ring uberwar lazypress.war

Use the vmc tool to deploy it:

vmc update lazypress

For more usage about the vmc tool, you can read this article.

So you have finished deploying your clojure web application to cloudfoundry.

Backed by spring and vmware, cloudfoundry is more Java-friendly than other PaaS like heroku. You don’t have to start a Java process by yourself (“lein run” isn’t a graceful way to start your app in product environment). And you don’t have to worry about your web container settings (configure jetty with limited options via ring-jetty-adaptor). All you have to do is package the application as a portable war file, which you can deploy to tomcat, glassfish, and also cloudfoundry. The vmc tool could detect you war file and handle it correctly.

Roar for mootools 1.4

早在天下大势还处在分久必合的时候,那时候mootools还有不少简单实用的小库,比如我今天搜索”mootools notification”就找到这个08年的库叫做Roar。不过遗憾的是从那以后,这个库就再也没有更新过了。

Mootools本身也沉寂了很久,这个项目恐怕也要思考自己未来的发展方向了。今年9月Mootools迈进了1.4,API上有一些变化。现在的下载页也能看到with/without backward compatibility的版本分开下载。为了用上Roar,我尝试了这两个版本发现都不能使用。最后downgrade到1.2可以确定Roar本身在当时是没有什么问题。

这么多年对mootools痴心不改,所以顺手维护了一下Roar,现在可以在1.4 without compatibility的发布下运行了。主要是几个小修改,大多是一些多年deprecated函数被正式删除:

  • Type常量,原先的String.type,Object.type现在统一到一个Type对象下,变成Type.isString和Type.isObject
  • $empty 常量被删除了,现在直接用function()或Function.from()代替
  • $pick 方法被Array.pick取代,参数现在也必须接受数组类型了
  • $merge 方法被Object.merge取代
  • $type 被typeOf取代
  • 函数对象的create方法被删除了,现在可以用函数对象的bind方法替代
  • Browser.Engine 被删除了,需要用其他Browser的API替代

修改后的Roar,放在这个gist里,测试过可以在firefox和chromium上健康使用。IE没有做测试。这个08年的库,眼看四年过去了,用起来依然不错。

作为mootools的铁杆,我还是会一直专一地坚守下去的。(于是,我也已经变成了多年前那些我眼中为旧事物顽抗到底的老家伙了)

TF101 101

上周末决定不再忍耐,又入了一个大件:华硕的平板,变形金刚。TF101上市已经半年了,而且现在TF102号称四核的版本已经开始接受预订了,所以差不多也到了应该出手的时候了。因为我知道如果去等102的话,我还会像现在这样地去和103做比较。

选择这款的主要原因即他的键盘配置,平板加键盘的组合彻底把上网本推进深渊。对于我这种还算是制造内容比率比较高的人来说,有个强有力的输入设备是必要的。目前市面上有这种搭配的只有transformer和think的,而且think似乎又只有配图和说明,没见真正卖那款键盘的。更严重的是,当然,think太贵了。

接下来开始说问题:
第一关叫做充电。TF101在充电方面有严重的缺陷。当电池电量极低时,会出现无法充电的情况。明明接着电源,可是电量提示一直是0%。如此情况下我整整充了一天拔下电源依然无法开机。最后看了网上的说法,在充电自动开机后关机了半个小时终于起死回生。

第二个小问题,键盘底座的平板本身电源是分离的,二者各自充电。所以在前面〝整整充了一天〞之前还有整整充了一晚上键盘。早晨起来键盘电满了,平板没充进去。

系统出厂是Android 3.0,这个系统的伟大之处在于从他一启动开始,就开始不断有程序报出错退出,不断有应用停止响应。他简直都对不起这个版本号。直到后来充上电升级到3.2之后才可以用。但是,还是有浏览器突然僵死然后突然从眼前消失的场面。再有就是机捆绑的什么人人网,电子书,开心网什么的,让你想不root都不行。

再有andrid 3.x上应用可能本来就不多,随机捆的又是一个流氓市场,除了满眼的流氓软件和山寨以外,就是版已经过时的软件。可怜这挺好一机器都不知道该装点什么。要说TF101硬件已经很可以了,只是配上这么个系统,用范伟的话说,白瞎你这个人了。手放键盘上,真恨不得能打开个终端来挡住这个浅薄的外观。等有时间我一定要尝试在这台机器上装个正经系统。也算是对得起这硬件了。

一句话概括一下的话,硬件还好,软件拉倒。

Extend slacker server with interceptors

An interceptor framework was introduced in slacker 0.3.0. It’s designed to allow user to add custom functionality without hacking into the internal of slacker.

Like many server frameworks, slacker abstracts the request processing as a pipeline. The request object is modified by adding or updating attributes through each node of the pipeline. So it’s easy to add your interceptor into the pipeline, with which you can get the data before and after function executed.

To create such an interceptor, you should use the slacker.interceptor/definterceptor macro and slacker.interceptor/definterceptor+ macro:

(definterceptor name
:before interceptor-function
:after interceptor-function)

(definterceptor+ name [arguments]
:before interceptor-function
:after interceptor-function)

definterceptor+ can accept arguments so you can configure the interceptor when you use it.

See a simple example:

(definterceptor log-function-call
  :before (fn [req] (println (str "calling " (:fname req))) req))

(definterceptor+ log-function-call-prefixed [prefix]
  :before (fn [req] (println (str
                               (if (fn? prefix) (prefix) prefix)
                               " calling "
                               (:fname req)))
                    req))

Then, add it to your slacker server by

(use '[slacker.interceptor])
(import '[java.util Date])
(start-slacker (the-ns 'slapi) 2104
  :interceptors (interceptors log-function-call
                              (log-function-call-prefixed
                                (fn [] (.toString (Date.)))))

Now you can log every function call of your slacker server.

For more detail about the interceptor framework, especially the request data, please check the wiki page.

In slacker 0.3.0, there is a built-in interceptor to stats function calls. You can find it at slacker.interceptors.stats. The stats data is expose via JMX. You can also write monitoring application to retrieve the data.

And there will be more built-in interceptors in 0.4.0, includes function call time stats and logging.