诗歌rails之关于ruby script/runner

    Rails开发的时候,估计console和Debug我们会比较多用,JE上搜Script/runner没有什么相关的资料。

    runner和console一样是内置在script下的命令,也非常类似的提供console的功能,只不过是console针对的是命令行,runner针对的是文件。换句话说,都是提供Rails执行环境。

    runner最大可能的应用,在于定时执行。例如,你的Rake任务,对于一些没有满足条件的用户发送邮件通知,每晚执行:

    我们的需求是,每天晚上,用户不怎么访问的时候检查系统日志,如果,超过范围就清理日志:
1. 在lib下建立文件rake任务clear_daemon_log

Ruby代码
  1. namespace :log do  
  2.   desc "Truncates all *.log files in log/ to zero bytes"  
  3.   task :clear_all do  
  4.     FileList["#{RAILS_ROOT}/log/*.out"].each do |log_file|  
  5.       f = File.open(log_file, "w")  
  6.       f.close  
  7.     end  
  8.     FileList["#{RAILS_ROOT}/log/*.err"].each do |log_file|  
  9.       f = File.open(log_file, "w")  
  10.       f.close  
  11.     end  
  12.     FileList["#{RAILS_ROOT}/log/*.log"].each do |log_file|  
  13.       f = File.open(log_file, "w")  
  14.       f.close  
  15.     end  
  16.   end  
  17. end  



2. 在script下建立文件logclean

Ruby代码
  1. #调用上面的Rake任务  
  2.   
  3. require 'rake'  
  4. Rake.application.rake_require 'http://www.cnblogs.com/lib/tasks/clear_daemon_log'  
  5. Rake.application['log:clear_all'].invoke  


3. 如下调用

Ruby代码
  1. 0 1,13 * * * RAILS_ENV=production /.../current/script/runner /.../current/script/dbcleaner  


4. 查看cron

Ruby代码
  1. crontab -l -u user  


5. 通过spec生成rpm

Ruby代码
  1. %prep  
  2. echo Building %{name}-%{version}-%{release}  
  3.   
  4. # Since we'll be copying the files from the repository checkout instead of  
  5. # extracting a tarball (-T), we only need to create the target directory (-c).  
  6. %setup -q -T -c  
  7.   
  8. %build  
  9.   
  10. %clean  
  11. #rm -rf %{buildroot}  
  12.   
  13. %install  
  14. rm -rf %{buildroot}  
  15. mkdir -p %{buildroot}/var/spool/cron  
  16. cp %{SOURCE0} %{buildroot}/var/spool/cron/××  



这样在系统安装的时候,就可以执行这个cron了
posted @ 2009-08-24 17:24  麦飞  阅读(1325)  评论(0编辑  收藏  举报