Archive

Archive for January, 2008

Godaddy cron job 的执行时间最大是36秒

January 12th, 2008

想想也是,这种相对来说比较便宜的共享空间,也不会让你占用太多的系统资源。
刚写了一个死循环的脚本,跑了一下,得出的时间是36妙。

#!/usr/local/bin/ruby
# index.cgi
require "time"

i=0
tm = Time.now
newfile = File.open( "time1.html", "w+",0644)
newfile << tm.min().to_s + ":" + tm.sec().to_s + "\n"
newfile.close

while true
  i=i+1
  if i % 1000000 == 0 then
    tm = Time.now
    newfile = File.open("time2.html", "w+",0644)
    newfile << tm.min().to_s + ":" + tm.sec().to_s + "\n"
    newfile.close
  end

end

time1.html和time2.html的时间相差了36妙。
有个老外也碰到了这个问题。
http://bogdan.org.u......html
不光跑ruby有这个问题,跑python,ELF等也有这个问题。

webflier i.t.

ruby, DateTime class实现rfc2822转换

January 12th, 2008

比较诡异的事情,ruby的Time类有实现rfc2822(),而DateTime类没有实现。
那我们可以自己动手。

class DateTime
    def to_rfc2822
        sprintf("%.3s, %02d %.3s %04d %02d:%02d:%02d %s",
				Date::DAYNAMES[self.wday],
				self.day, Date::MONTHNAMES[self.mon],
				self.year, self.hour, self.min, self.sec,
				self.zone)
    end
end

好了,Ruby标准库的DateTime类支持到rfc2822的转换方法了。you see, ruby太灵活,自由了。

webflier i.t.

ruby, open-uri发起conditional get

January 12th, 2008

Conditional GET 有两种,一种是etag, 另一种是last-modified。
关于这两种介绍,请移步:http://www.dbanotes.net/web/http_11_etag_lastmodified.html
我们分别来看一下这两种实现。
1。用ETag

require 'open-uri'

 # 1st request -- save the ETag
 etag = nil
 open( "http://redhanded.hobix.com/index.xml" ) do |feed|
   etag = feed.meta['etag']
 end

 # 2nd request -- only retrieve the file if it has changed
 begin
   open( "http://redhanded.hobix.com/index.xml",
         "If-None-Match" => etag ) do |feed|
     puts "File has changed: #{ feed.read.length } bytes read"
   end
 rescue OpenURI::HTTPError
   puts "No file available or file has not changed."
 end

2。用last-modified

require 'open-uri'

 # 1st request -- save the modification time
 mtime = nil
 open( "http://redhanded.hobix.com/index.xml" ) do |feed|
   mtime = feed.last_modified
 end

 # 2nd request -- only retrieve the file if it has changed
 begin
   open( "http://redhanded.hobix.com/index.xml",
         "If-Modified-Since" => mtime.rfc2822 ) do |feed|
     puts "File has changed: #{ feed.read.length } bytes read"
   end
 rescue OpenURI::HTTPError
   puts "No file available or file has not changed."
 end

webflier i.t.

在Godaddy Deluxe运行Ruby脚本

January 11th, 2008

网上有很多文章介绍怎么在Godaddy的空间上跑Ruby on Rails,但是怎么单独运行Ruby脚本,却很少有提及。和Perl、php相比,Ruby更灵活,更省力。
我们看下面这个例子:

#!/usr/local/bin/ruby -w
# index.cgi
puts "Content-Type: text/html"
puts
puts "Hello World!"

把上面这段脚本上传到Godady的空间里,任意目录都可以。然后将这个文件的属性改为755(大部分FTP软件都能做到)。

这样你就可以看这个脚本运行了,比如:http://www.yoursite.com/main.cgi

注意,脚本的名字一定要以cgi作扩展名,而不是rb。

如果要用gems,可以这样

require "rubygems"
require "mysql"

webflier i.t.