Monday, September 29, 2008
利用 Nginx 與 FastCGI 佈署 Jifty 應用程式
我在 jifty.org 上撰寫了此頁,在此以中文簡單說明一次。
Nginx 內附的 fastcgi 已經可以直接配合 Jifty application,請對照著 Wiki 上的設定內容,幾個重點如下:
- fastcgi_param PATH_INFO $fastcgi_script_name;
- Jifty 跟 Catalyst 內附的 fastcgi server 都需要 PATH_INFO 這個環境變數,FreeBSD 跟 Linux 上安裝好的 nginx 的預設 fastcgi 設定裡面並沒有此值,必需要手動加入。
- root /APP/share/web/;
- 目錄設定到 share/web 就可以
- fastcgi_pass localhost:9000;
- 這裡是指定 fastcgi server 的所在之處。似乎沒有方法讓 nginx 自動啟動 fastcgi server。因此需要另外啟動。
手動啟動 jifty fastcgi server 的方式為:
FCGI_SOCKET_PATH="localhost:9000" bin/jifty fastcgi &
此外,原本 Jifty 有附許多 static assets,但不知為何我給的設定並不會使 jifty fastcgi server 去 serve 那些 static assets。解決辨法之一是直接全部複製到 share/web/static 底下來:
cp -rn /usr/local/lib/perl5/site_perl/5.8.8/auto/Jifty/web/static/* share/web/static
此處的 -n 參數是「不要覆蓋現有檔案」之意,有些系統的 cp 沒有這個參數,多半可用 cp -i 替代
這組設定應該還可以再調整以解決此問題。但我覺得這種解法反而對佈署有益。因為讓 fastcgi server 來 serve static assets 完全是以牛刀殺雞。也無法善用 nginx async io,能很快回應靜態檔案的這個大優點。
更新:後來發現可以使用 Nginx 的 rewrite 規則來解決這個問題,只要你知道系統上的 @INC 路徑中,放 Jifty 那些 asset 的路徑是哪條。方法如下:
location /__jifty/static {
alias /usr/local/lib/perl5/site_perl/5.8.8/auto/Jifty/web/static;
}
location /static {
if (!-f $request_filename) {
rewrite ^(.*)$ /__jifty$1 last;
}
}
第一條規則其實是使 /__jifty/static 這個 URL 對應到 Jifty 所用來存放其內附的 asset 路徑。第二條 location 規則中則定義,如果找不到檔案的話,則重導到 /__jifty/static 去。說是重導其實也不太正確,因其並非經過瀏覽器的重導,只是在 Nginx 內部而已。
Labels: deploy, fastcgi, jifty, nginx
Tuesday, September 16, 2008
賣 Macbook Pro 電腦一台
- 中文鍵盤,15 吋螢幕
- 序號: W8630241VWW
- 處理器: Intel Core Duo 2Ghz
- L2 快取記憶體: 2 MB
- 記憶體: 2 GB
- 匯流排速度: 667 MHz
- 這台只有 Firewire 400, 沒有 Firewire 800
- Email: gugod at gugod.org
- Skype: gugodliu
- AIM / iChat: gugod@mac.com
Labels: mac
Monday, September 08, 2008
JiftyX-ModelHelpers 0.01 is released to CPAN
$book = Book(42); $books = BookCollection( author => "Neal Stephenson" );This module is released as an extension under JiftyX namespace as requested by jifty team. Use it at your own risk. Go grab it at http://search.cpan.org/dist/JiftyX-ModelHelpers/
Wednesday, September 03, 2008
JiftyX::ModelHelper makes me happy.
Download: JiftyX-ModelHelpers
It has been a while since audreyt first mentioned the namespace "JiftyX" for Jifty plugins.
Few month ago I had this hack to make my life coding in Jifty easier (in the aspect of typing):
sub M {
return Jifty->app_class(Model => $_[0])->new;
}
This lets you do something like:
$book = M("Book");
$book->load(42);
Instead of
$book = Jifty->app_class(Model => "Book")->new;
Jifty always want to be polite, prevent global namespace clobbering. That is an absolutely good convention to follow. But it does not make life of developer easier. I decided to see if I can push this M hack further and came up with something like this:
$book = Book(42);
I think this is likely the shortest way you can do to load a record with id 42 from Book model. In RoR, you do this:
book = Book.find(42)
Or, we might need to load a record with other info instead of ID:
$book = Book(name => "Harry Potter");
This is like calling Jifty::DBI::Record::load_by_cols(), but shorter
To do this in Rails, you need to say:
book = Book.find(:first, :conditions => ["name = ?", "Harry Potter"])
OK, now I am very happy ;)
After discussed on jifty-dev mailing list, I put my currently progress of this work on svn.jifty.org here: JiftyX-ModelHelpers . It has no doc in there, but currently what's implemented is really all described in this blog entry. So feel free to try it in your Jifty app.
Sometimes in View you will failed to use JiftyX::ModelHelpers, perl will tell you there are some syntax error. In that case, please say:
require JiftyX::ModelHelpers; JiftyX::ModelHelpers->import();
This makes me feels especially good writing tests. Here's a piece of test setup code in one of my recently work in progress:
use Retail::Test::Fixtures qw(providers commodities consumers);
my $good_company = Provider(name => "good company");
my $good_consumer = Consumer(name => "good consumer");
my $a = Commodity(name => "Hello Kitty A");
is($a->quantities_in_stock, 0, "Initially its stockless");
add_supply($a, 10, 0);
is($a->quantities_in_stock, 10, "Added 10");
add_supply($a, 15, 0);
is($a->quantities_in_stock, 25, ".. added another 15");
# # Draft supply.
add_supply($a, 15, 1);
is($a->quantities_in_stock, 25, "A draft supply shouldn't count.");
# Sold 15 items
add_sale($a, 15, 0);
is($a->quantities_in_stock, 10, "Just sold 10.");
sub add_supply {
my $record = shift;
my $quantity = shift;
my $draft = shift;
my $s = Supply->create(
provider => $good_company,
draft => $draft
);
SupplyCommodity->create(
supply => $s,
commodity => $record,
quantity => $quantity
);
}
sub add_sale {
my $record = shift;
my $quantity = shift;
my $draft = shift;
my $s = Sale->create(
consumer => $good_consumer,
draft => $draft
);
SaleCommodity->create(
sale => $s,
commodity => $record,
quantity => $quantity
);
}
Or see it here: t/commodity-quantities-in-stock.t. Also take a look how I load fixtures in Fixtures.pm
Since it simplyfies the code to create new records on-the-fly. Now I feel much better to write more tests in my Jifty app. :)
Subscribe to Posts [Atom]
