Rails: 记得要定时清理session 表 ( Rails: Do remember to clear your session table! )

访问量: 1989

明天要给国外同事做一个演示,关于我过去一年多参与的项目。于是我打算在生产服务器上做,环境很干净,而且速度快(8G内存)。于是乎,我就做了个数据库备份。( Due to a demonstration for a project that I have been working on since last year on tomorrow, to some US workmates, I did a database backup operation on the production server which is more faster than another server, and hope to restore the data once the demo is done. )

[babble@bportal02 ~]$ /home/kcv478/local/bin/mysqldump -u babble -p -h dbbabble-vip01.mcloud201.blur.svcmot.com babble_portal > babble_portal.sql

可是,当我仔细查看文件的时候,我愣了。。。( However I was shocked when the dump is done )

-rw-rw-r--   1 babble babble 223512238 Aug 21 06:26 babble_portal.sql

这么多位数字。。。我数数。。。 223 MB。 奇了怪了,这个APP现在应该啥数据都没有,只有些初始化的数据啊。( 223MB.... so strange, it should don't contain any data except some initial data that used by the system, less than 1k. )

找了一下原因,发现是这样的:     ( after digging into the problem, I found that: )
1. 我们使用了session store.   ( 1. we used session store instead of cookie store)
2. 有个监控程序,每秒ping 一下系统。 ( 2. there's a monitor system which has been pinging our app every 1 second)
于是,从 8月7号到现在(8月21号),session表中产生了 120万条记录。  ( so, from Aug 7th til now, 1.2 million records were created in the sessions table by Rails)

问题的根源是:  Rails 不会主动删除这些session 数据! ( and the interesting thing is: Rails will never delete these data automatically )

1. 你可以使用crontab + mysql client. 
2. 你可以使用 rake task + crontab
3. 你也可以使用 whenever

我打算使用 whenever 来做。改天记录下来吧~~~ 

参考: http://realityforge.org/code/rails/2006/03/01/removing-stale-rails-sessions.html

订阅/RSS Feed