Mark24
记录灵感、技术、思考
笔记:Sinatra源码踹门
版本: sinatra git:(72be291d)
有趣的点
- 启动时间点 竟然在 at_exit
# lib/sinatra.rb
at_exit do
Sinatra::Server.new.start unless Sinatra::Server.running
end
at_exit 他是让程序在脚本执行结束的时候执行的。
- Trap
# ....
class Server
# ....
def start
begin
Rack::Handler::Mongrel.run(Sinatra::Dispatcher.new, :Port => 4567) do |server|
logger.info "== Sinatra has taken the stage on port #{server.port}!"
trap("INT") do
server.stop
self.class.running = false
logger.info "\n== Sinatra has ended his set (crowd applauds)"
end
end
self.class.running = true
rescue => e
logger.exception e
end
end
end
# ....
trap 专门用于处理系统信号
详细可以参考 《理解Unix进程》16.9 Page 64
trap("INT") do
server.stop
self.class.running = false
logger.info "\n== Sinatra has ended his set (crowd applauds)"
end
trap在这里是重新定义信号,做一些响应信号之前的工作。而 INT 是 SIGINT 信号名字,可以在 Linux上查看
kill -l 列出所有信号
man kill 然后搜索 INT
2 INT (interrupt)
3 QUIT (quit)
6 ABRT (abort)
9 KILL (non-catchable, non-ignorable kill)
14 ALRM (alarm clock)
15 TERM (software termination signal)
INT就是 Ctrl+C 是中断的意思。
所以这里中断对中断信号做了处理。