Sooey

2011-07-04 11:00:28 +0900

HerokuのCedarスタックでSendgridを使うための設定について。

Herokuに新しく登場したCedarスタックでメールを送信するため、Sendgridアドオンを有効にしてWorkerのジョブからActionMailerを利用してメールを送信しようとしたところ、以下のようなエラーログが記録されました。

2011-07-01T11:34:29+00:00 app[worker.1]: Sent mail to foo@example.com (3065ms)
2011-07-01T11:34:29+00:00 app[worker.1]: [Worker(host:0ad8724e-50ca-b229-486b-542b1b07ba54 pid:1)] Class#usage_report_mail failed with Errno::ECONNREFUSED: Connection refused - connect(2) - 0 failed attempts
2011-07-01T11:34:29+00:00 app[worker.1]: [Worker(host:0ad8724e-50ca-b229-486b-542b1b07ba54 pid:1)] 1 jobs processed at 0.2842 j/s, 1 failed ...

Heroku | Dev Center | Sendgridを読んでみると、

If you’re not using Rails and ActionMailer, or you are using the Cedar stack, you will need to setup your email framework manually; check out examples below.

とあり、Cedarスタックでは明示的にActionMailerの設定を行うことが必要なようです。

というわけで、config/environments/production.rbに以下の設定を追加してアプリケーションをheroku restartしたところ、問題なくメールの送信が行われるようになりました。

TestApp::Application.configure do
  # ...

  ActionMailer::Base.smtp_settings = {
    :address        => "smtp.sendgrid.net",
    :port           => "25",
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => ENV['SENDGRID_DOMAIN']
  }

end

2011-06-26 02:45:41 +0900

NoirというClojure用のシンプルなWebアプリケーションフレームワークを見つけたので、インストール手順のメモ。

(ns my-app
  (:use noir.core)
  (:require [noir.server :as server]))

(defpage "/welcome" []
    "Welcome to Noir!")

(server/start 8080 {})

↑Noirでは、こんな感じでアプリケーションが書ける。

Leiningenのインストール

まず、Clojure用ビルドツールとして定番のleiningenを導入します。ビルドツールといいつつ、依存するライブラリのインストールやClojureアプリケーションの起動など色々やってくれるみたい。

leiningenはleinというシェルスクリプトとjarファイルのセットで構成されており、leinスクリプトをダウンロードして実行すると自身でjarファイルのダウンロード&インストールを行うようになっています。

$ curl -L -O https://github.com/technomancy/leiningen/raw/stable/bin/lein
$ chmod +x lein
$ ./lein
Downloading Leiningen now...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 8003k  100 8003k    0     0  1563k      0  0:00:05  0:00:05 --:--:-- 2380k
Leiningen is a build tool for Clojure.

Several tasks are available:
classpath   Print the classpath of the current project.
clean       Remove compiled class files from project.
compile     Compile Clojure source into .class files.
deploy      Build and deploy jar to remote repository.
deps        Download all dependencies and put them in :library-path.
help        Display a list of tasks or help for a given task.
install     Install the current project or download the project specified.
interactive Enter interactive task shell.
jar         Package up all the project's files into a jar file.
javac       Compile Java source files.
new         Create a new project skeleton.
plugin      Manage user-level plugins.
pom         Write a pom.xml file to disk for Maven interop.
repl        Start a repl session either with the current project or standalone.
run         Run a -main function with optional command-line arguments.
test        Run the project's tests.
test!       Run a project's tests after cleaning and fetching dependencies.
uberjar     Package up the project files and deps into a jar file.
upgrade     Upgrade Leiningen to the latest stable release.
version     Print version for Leiningen and the current JVM.

Run lein help $TASK for details.
Also available: readme, tutorial, copying, sample, and news.

インストールが完了したら、leinコマンドはどこかパスの通ったところに置いておけばよさそうです。

Noirのインストール

leiningenでNoirプロジェクトの作成を行うためのlein-noirプラグインというものがあるので、これをインストールします。

$ lein plugin install lein-noir 1.0.0
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.pom from central
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.jar from central
Copying 1 file to /var/folders/U1/U1N91KLEHEyHpA-qb8SHhU+++TI/-Tmp-/lein-816281b5-5ee9-4f7d-ad84-6c24edd7ddaf/lib
Including lein-noir-1.0.0.jar
Including clojure-1.2.1.jar
Created lein-noir-1.0.0.jar

leiningenによってNoirが依存するClojureのjarファイルもダウンロードされました。

プロジェクトの作成

leinコマンドでNoirプロジェクトが作成できるようになりました。

$ lein noir new helloworld
Creating noir project:  helloworld
Creating new dirs at:  /Users/juno/helloworld
Adding files...
Project created!

作成されたプロジェクトディレクトリの内容はこんな感じになります。

$ tree helloworld
helloworld
├── README.md
├── project.clj
├── resources
│   └── public
│       ├── css
│       │   └── reset.css
│       ├── img
│       └── js
├── src
│   └── helloworld
│       ├── models
│       ├── server.clj
│       └── views
│           ├── common.clj
│           └── welcome.clj
└── test
    └── helloworld

11 directories, 6 files

とりあえずそのまま実行してみます。

$ cd helloworld
$ lein run

leiningenがproject.cljの内容を読んで必要なライブラリのダウンロードとインストールを行います。インストールがひと通り完了すると、以下のようにポート8080でサーバーが起動します。

Starting server...
2011-06-26 02:33:23.165:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2011-06-26 02:33:23.174:INFO::jetty-6.1.26
2011-06-26 02:33:23.241:INFO::Started SocketConnector@0.0.0.0:8080
Server started on port [8080].
You can view the site at http://localhost:8080

ブラウザでhttp://localhost:8080/にアクセスすると、Noirの簡単なチュートリアルページが表示されます。

2011-05-22 13:50:45 +0900

Mac上のエディタ(Aquaemacs、BBEdit、Coda、Emacs、MacVim、TextMate、XCode)でインテリジェントにファイルが開けるPeepOpenというシェアウェアがとても便利で、もう1年くらい使っているんだけどあんまり日本では知られていないみたい。

どんなものなのかは、サイトにあるプレビュームービー(.mov)を見るのが手っ取り早いです。

$12とちょっとお高めだけど、Snow Leopard + Cocoa Emacsでまったく問題なく使えるし定期的にアップデートもされているので、非常におすすめです。

2011-05-20 10:41:37 +0900

ChromeのブックマークバーにPinboardのpopupブックマークレットを置いてあって、Bookmarks Bar Keyboard Shortcutsを利用してCtrl+2でブックマークレットが起動するようにして使っていたんだけど、Chrome 12になったあたりからCtrl+2を押しても何も起こらなくなってしまった。

Pinboardのブックマークレットはこんなの。

Bookmarks Bar Keyboard Shortcutsでちゃんと起動するブックマークレットもあるので、ブックマークレット自体のコードに原因があるんじゃないかとあたりをつけて調べたところ、open()の引数に不要な%20が混ざっているのが直接の原因みたい。

上のように%20を取り除いたコードに書き換えたところ、Pinboardのブックマークレットもちゃんとキーボード操作で起動することができるようになった。

Google Readerの以下のブックマークレットも同様で、

%20を半角スペースに置換したら、キーボードから呼び出せるようになった。

2011-05-19 15:17:00 +0900

Herokuでこの間発生したHTTPおよびGit接続ができない障害について詳細報告が出た。

Root domains are aesthetically pleasing, but the nature of DNS prevents them from being a robust solution for web apps. Root domains don't allow CNAMEs, which requires hardcoding IP addresses, which in turn prevents flexibility on updates to IPs which may need to change over time to handle new load or divert denial-of-service attacks.

We strongly recommend against using root domains. Use a subdomain that can be CNAME aliased to proxy.heroku.com, and avoid ever manually entering IPs into your DNS configuration. We also recommend a low TTL value, which will allow Heroku network engineers to quickly make changes to DNS mapping when necessary.

今回のようなケースで、迅速にconnectivityが復活するようにするには、example.comのようなルートドメインをHerokuのIPアドレスに向けるような設定・運用は避けて、サブドメインがproxy.heroku.comを指すようCNAMEを設定するほうがいいみたい。