Rails开发的时候,估计console和Debug我们会比较多用,JE上搜Script/runner没有什么相关的资料。
runner和console一样是内置在script下的命令,也非常类似的提供console的功能,只不过是console针对的是命令行,runner针对的是文件。换句话说,都是提供Rails执行环境。
runner最大可能的应用,在于定时执行。例如,你的Rake任务,对于一些没有满足条件的用户发送邮件通知,每晚执行:
我们的需求是,每天晚上,用户不怎么访问的时候检查系统日志,如果,超过范围就清理日志:
1. 在lib下建立文件rake任务clear_daemon_log
- namespace :log do
- desc "Truncates all *.log files in log/ to zero bytes"
- task :clear_all do
- FileList["#{RAILS_ROOT}/log/*.out"].each do |log_file|
- f = File.open(log_file, "w")
- f.close
- end
- FileList["#{RAILS_ROOT}/log/*.err"].each do |log_file|
- f = File.open(log_file, "w")
- f.close
- end
- FileList["#{RAILS_ROOT}/log/*.log"].each do |log_file|
- f = File.open(log_file, "w")
- f.close
- end
- end
- end
namespace :log do
desc "Truncates all *.log files in log/ to zero bytes"
task :clear_all do
FileList["#{RAILS_ROOT}/log/*.out"].each do |log_file|
f = File.open(log_file, "w")
f.close
end
FileList["#{RAILS_ROOT}/log/*.err"].each do |log_file|
f = File.open(log_file, "w")
f.close
end
FileList["#{RAILS_ROOT}/log/*.log"].each do |log_file|
f = File.open(log_file, "w")
f.close
end
end
end
2. 在script下建立文件logclean
-
-
- require 'rake'
- Rake.application.rake_require 'http://www.cnblogs.com/lib/tasks/clear_daemon_log'
- Rake.application['log:clear_all'].invoke
#调用上面的Rake任务
require 'rake'
Rake.application.rake_require 'http://www.cnblogs.com/lib/tasks/clear_daemon_log'
Rake.application['log:clear_all'].invoke
3. 如下调用
- 0 1,13 * * * RAILS_ENV=production /.../current/script/runner /.../current/script/dbcleaner
0 1,13 * * * RAILS_ENV=production /.../current/script/runner /.../current/script/dbcleaner
4. 查看cron
crontab -l -u user
5. 通过spec生成rpm
- %prep
- echo Building %{name}-%{version}-%{release}
-
-
-
- %setup -q -T -c
-
- %build
-
- %clean
-
-
- %install
- rm -rf %{buildroot}
- mkdir -p %{buildroot}/var/spool/cron
- cp %{SOURCE0} %{buildroot}/var/spool/cron/××
%prep
echo Building %{name}-%{version}-%{release}
# Since we'll be copying the files from the repository checkout instead of
# extracting a tarball (-T), we only need to create the target directory (-c).
%setup -q -T -c
%build
%clean
#rm -rf %{buildroot}
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/var/spool/cron
cp %{SOURCE0} %{buildroot}/var/spool/cron/××
这样在系统安装的时候,就可以执行这个cron了
|
请发表评论