调试陈旧的Ruby进程

时间:2010-01-31 15:20:53

标签: mysql ruby-on-rails ruby daemon

我在Rails项目中有一个ruby脚本。该脚本包括environment.rb,以便Rails被加载等。然后它监听另一台服务器的TCP套接字上的数据,解析它以使mysql数据库保持最新。

我正在使用守护进程gem来轻松启动和停止进程。 Iv让脚本在生产中运行了一天没有问题,但现在已经过时了。 Daemons控制文件说进程仍在运行,当我执行ps -ef时进程显示,monit也认为进程正常运行。但是数据库没有得到更新(如果我在另一个进程中再次运行脚本,数据库正确更新)。

我尝试使用GDB通过运行sudo gdb /usr/local/bin/ruby 25395来调试流程的问题,输出是

GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu"...
/usr/local/bin/ruby: No such file or directory.
Attaching to process 25395
Reading symbols from /usr/bin/ruby1.8...(no debugging symbols found)...done.
Reading symbols from /usr/lib/libruby1.8.so.1.8...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libruby1.8.so.1.8
Reading symbols from /lib/libpthread.so.0...
(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x2b7decb08d30 (LWP 25395)]
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libcrypt.so.1...
(no debugging symbols found)...done.
Loaded symbols for /lib/libcrypt.so.1
Reading symbols from /lib/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libc.so.6...
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/thread.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/thread.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/etc.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/etc.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/stringio.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/stringio.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/syck.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/syck.so
Reading symbols from /lib/libnss_compat.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_compat.so.2
Reading symbols from /lib/libnsl.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libnsl.so.1
Reading symbols from /lib/libnss_nis.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_nis.so.2
Reading symbols from /lib/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_files.so.2
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/socket.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/socket.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/nkf.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/nkf.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/bigdecimal.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/bigdecimal.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/iconv.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/iconv.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/strscan.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/strscan.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.1/lib/fast_xs.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.1/lib/fast_xs.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/parser.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/parser.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/generator.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/generator.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/racc/cparse.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/racc/cparse.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/tmail-1.2.3.1/ext/tmailscanner/tmail/tmailscanner.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/tmail-1.2.3.1/ext/tmailscanner/tmail/tmailscanner.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/openssl.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/openssl.so
Reading symbols from /lib/libssl.so.0.9.8...done.
Loaded symbols for /lib/libssl.so.0.9.8
Reading symbols from /lib/libcrypto.so.0.9.8...done.
Loaded symbols for /lib/libcrypto.so.0.9.8
Reading symbols from /lib/libz.so.1...done.
Loaded symbols for /lib/libz.so.1
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/digest.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/digest.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/fcntl.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/fcntl.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so
Reading symbols from /usr/lib/libmysqlclient.so.15...done.
Loaded symbols for /usr/lib/libmysqlclient.so.15
0x00002b7dec86df10 in read ()
   from /lib/libc.so.6

这表明问题出在mysql连接上。几小时内可能没有任何数据。我相信这可能导致进程与Mysql断开连接?但是,如果确实发生了这种情况,我会发现,当脚本再次开始获取数据时,会抛出异常,这会阻止脚本运行,提示monit重新启动它。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,实际上TCP连接正在断开连接。即使重新连接后,套接字上的gets方法也会挂起。我使用的解决方案是在gets方法周围放置一个超时块​​。例如

timeout(10) do    
  line = socket.gets
end

在超时结束脚本后会引发异常。然后Monit可以自动重启。