I cloned Beanstalkd with Clojure

I just cloned Beanstalkd with clojure and you can find this project on github. It’s a light weight task queue that “producers” could put tasks in and “workers” are blocked to reserve them and process them. It will help you to split expensive operations (sending email, etc) to background.

The features:

  • Almost compatible with Beanstalkd protocol
  • Full support on Beanstalkd verbs: put/reserve/release/bury/kick
  • Persistence with binary write-ahead logs
  • Simple JMX monitoring

The networking layer is based on @ztellman‘s libraries, aleph, lamina and gloss. And I also received great help from him on implementing the protocol with gloss.

This is not my first clojure project but the largest one. I add marginalia in the dev-dependencies, you can generate well formatted code and docs with `lein marg`.

By the way, this project is not well tested. I’m still working on it to find potential issues and to fix them. Do not use it on any production environment before you have carefully gone through the code base. I just made this announcement here to let people know this project if they are interested in this topic.

And always, Any fork/contribution is welcomed.

Beginning Emacs for Clojure

没错我开始用Emacs了!对于一个使用lisp方言的开发的人来说,Emacs无疑是正统,是professional的象征。

Screenshot--home-sun-projects-clojalk-src-clojalk-wal.clj

其实用Emacs,远不需要太多的配置,就可以创建一个高效率的开发环境。我用了两天不到时间,当然还包括过去三年甚至更长时间里多次试图学习Emacs并最终半途而废残存的精力。

一步一步来,fedora系统的用户,比如这位,还有我,都会奇怪,命名有emacs,可是为什么没有GUI呢。因为系统自带的只是一个简单的版本,用yum安装一下emacs这个包才算是真正完整。

接下来要跳关了,大家注意。Emacs-starter-kit是一个帮助初学者预先配置emacs的一套默认配置文件,有了这个配置文件,也算是爬上了巨人的肩膀,省去了很多探路的时间。安装方式,把这个git仓库的master分支打包下载到~/.emacs.d/里即可(展开到这个目录)。启动emacs后,会自动安装。(作者名字很熟悉,对了,就是leiningen的作者)

由于package.el已经配置好了,接下来就可以安装一些必要的mode,两天的时间初探,我安装了:

  • clojure-mode
  • clojurescript-mode
  • auto-complete
  • color-theme-tangotango
  • slime
  • slime-repl

安装方式:
M-x package-install
输入包名即可。

clojure-mode和clojurescript-mode是编辑clojure的mode,不多说。

配色

color-theme我本来是习惯用solarized,不过尝试了几次color-theme-solarized这个包应该是有一些问题。退而选择了tangotango。要默认选择这个颜色,在init.el里加:

(require 'color-theme-tangotango)
(color-theme-tangotango)

代码提示

auto-complete是代码提示和自动补全插件,安装完成后要简单配置一下才能使用:

(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/elpa/auto-complete-1.4.20110207/dict/")
(ac-config-default)

当然网上有更多的高级配置,可以搜一下。

REPL

clojure的IDE必须要有repl的支持,在emacs中,repl通过slime这个mode来实现。此外还要额外安装swank:
lein plugin install swank-clojure 1.3.2
在项目目录通过lein swank启动。在emacs中:
M-x slime-connect
选择swank启动的host和port即可。这是一个支持tab提示的强大REPL,并且可以引用lein项目中的代码。

Edit/20110920
其实只需要在clojure-mode中通过M-x clojure-jack-in 即可启动swank和slime。

ParEdit

ParEdit是编辑括号的利器,Emacs-Starter-Kit已经把这个包配置好了。ParEdit不仅会自动打印括号,最强大的是还能阻止用户误删或误输入括号!有了它基本上括号的问题就轻松多了。
(当然,有时候需要强制删掉括号C-u DEL,也可能要强制输入括号C-q (C-q )

TagList

很遗憾ctags不支持clojure,而且emacs似乎也没有一个比较好的TagList。我看到有人提到imenu在一定程度上倒是可以做到类似的功能:
M-x imenu

总得来说,捅破了窗户纸,用emacs编辑clojure还是最好的选择!

流水帐

最近没有什么特别值得一提的事情,或者说都还在进行中还没有到值得一提的时候。

中秋节休息之后的第一天清早去驾校考交规理论,这个事没什么可说的,除了在4sq上又拓展了一点疆土,后来我又后悔没把GPS带着错过了一些还没有数据的道路。倒是那天去和回分别坐错了车,想十年前本人对城市交通网络了然于胸,除了视力不好把48路看成46路就没有坐错过车走错过路。结果现在公交线路的数量翻了一番,城市的交通网络也更复杂了。人脑则不具备scalability,活地图的时代一去不复返了。

第二件值得一说的是我在VirtualBox里成功安装了ArchLinux和GNOME Shell桌面。我工作的机器是有点古老的Dell OptiPlex 760,显卡还是ATI的Radeon HD 3420。为了开发的方便,我们装的都是CentOS 5,没错是5!!这周上班后升到了8G内存我就开始琢磨开个虚拟机体会一下现代生活。多余的不说了,几个注意事项:

  • Host要装ATI的fglrx驱动,xorg-x11-drv-ati直接也能用,但是性能非常差
  • VirtualBox要在4.0.4以上,我看网上说的,具体能个版本也不知道,但是用最新的就可以
  • 3D加速要开,在虚拟机的设置里
  • 最最重要的一点!不要从虚拟机的菜单里安装Guest Addon,那个对Arch不管用。要用pacman安装仓库里,包名我忘了,请自行-Ss
  • 别忘了把rc.vboxadd加入rc.conf的Deamons里

这周最诡异的问题是在配Netty的Pipeline时,我用了:

Channels.pipeline(new HttpMessageDecoder(), new HttpMessageEncoder(), myhandler);

结果稍微上一点压力居然出现了很多decoder异常,什么包不全、头不全、空指针什么的。不明真相最后改用例子里的:

ChannelPipeline cp = Channels.pipeline();
cp.addLast("decoder", new HttpMessageDecoder());
cp.addLast("encoder", new HttpMessageEncoder());
cp.addLast("handler", myhandler);

就成功了。也许是顺序的原因也许是名字的原因,可如果这个名字真的具有意义的话为什么没有常量可用呢?不去追究具体原因了,记住就好。

此外遇到的bug还包括clojure中transient数据结构的问题,我有一个transient的map,但是persistent之后发现只有前9(依稀好像是)次assoc!的内容保存了,后面却都丢失了。1.2里的transient都被标记了alpha,不过印象1.3的changes里好像也没提transient。

另一件与clojure有关的诡异事件是一个test case的failure report:

FAIL in (test-read-job) (wal.clj:61)
expected: (= (job k) (rjob k))
actual: (not (= “tomcat” “tomcat”))

不过最后好在是冤有头债有主,睁着眼有时也要说下滑。在从stream中读字节流多读了一个byte,后来用这一组byte去new String的时候,就变成了一个不可见字符。结果在命令行里的输出就是这么骇人听闻,还是在IDEA里跑这个测试才发现错误的那个字符串后面跟了一个方块。至于为什么多读了一个byte,copy/paste害死人啊。

以上这些牢骚是关于clojure的,但是这篇就不加clojure的tag了,否则被planet clojure收录去我又给中文世界丢脸了。

周五晚上我又更新了sunng.info的首页,以后也许我会定期的放一些得意的照片(或者照片上有得意的东西)上去做背景,以免被人看到后发现太单调乏味。

最后还要感谢一下twitter网友Kymair这周将Rage Viewer部署到了Heroku上。

RageViewer updated

I just added some new features to RageViewer, a rage comic viewer written in Clojure and ClojureScript. Issue on a recent update of CloudFoundry has been fixed. So I can update this application as normal.

Since there are a lot of over-18 content on rage comics, RageViewer now prompts you to confirm if it is suitable to display NSFW (Not Suit For Work, marked by author) content. If rejected, a placeholder comic (made by me) will be displayed to you.

Also, a lot of rage comic related subreddits are now supported by RageViewer. You can find an indicator on the top bar for current channel. Click it and you can switch to a different channel.

Bugs are fixed when there is a ? on the end of image url. “Show most recent hot comics” button also works now.

Let me know if you have any ideas or questions about this site.

Squealer (a test framework for Pig script) is using jip

Squealer is a framework written in Jython to test your Apache Pig scripts, by Mark Roddy. It’s now using jip to resolve Java dependencies. On huge dependencies of Hadoop, jip could be great helpful to setup Squealer.

To get started, it is recommended to create a standalone Jython environment for Squealer:
virtualenv -p /usr/local/bin/jython –no-site-packages squealer-env

Activate the environment
cd squealer-env
. bin/activate

Install jip with pip
pip install jip

Download squealer from bitbucket project page, extract it to somewhere. Install it with jip:
jython setup.py install

Dependencies will be downloaded from Maven Central. You just wait for it to finish.

Start a Jython interpreter with ‘jython-all‘ and now you can import squealer.