1. Install
$ gem install whenever
Or with Bundler in your Gemfile.
gem 'whenever', :require => false
2. Getting Started
$ cd /my/rails/app
$ wheneverize .
This will create an initial "config/schedule.rb" file you.
config/schedule.rb
# Learn more: http://github.com/javan/whenever
set :output, "#{path}/log/whenever.log"
every 15.minutes do
rake "send_emails"
end
|
3. Rake Tasks
desc "send emails"
task :send_emails do
command = "ar_sendmail_rails3 -o -e #{Rails.env}"
`#{command}`
end
|
4. Command
The whenever
command
$ cd /my/rails/app
$ whenever
This will simply show you your schedule.rb file converted to cron syntax. It does not read or write your crontab file. Run whenever --help
for a complete list of options.
Usage: whenever [options]
-i [identifier], Default: full path to schedule.rb file
--update-crontab
-w, --write-crontab [identifier] Default: full path to schedule.rb file
-c, --clear-crontab [identifier]
-s, --set [variables] Example: --set environment=staging&path=/my/sweet/path
-f, --load-file [schedule file] Default: config/schedule.rb
-u, --user [user] Default: current user
-k, --cut [lines] Cut lines from the top of the cronfile
-v, --version
|
5. Add cron job
$ cd /my/rails/app $ whenever -s environment=qa -w AC |
6. Update cron job
$ cd /my/rails/app $ whenever -i AC
|
7. Clear cron job
$ cd /my/rails/app $ whenever -c AC |
8. Capistrano
namespace :deploy do
...
desc "Stop the existing cron job"
task :stop_cron, :roles=>:app do
run("crontab -r")
end
desc "Update the crontab file"
task :update_crontab, :roles => :db do
run "cd #{current_path} && whenever --set environment=#{rails_env} --update-crontab #{application}"
end
....
end
|
After Task
after "deploy:symlink","deploy:update_crontab" |
9. Tips
If you use RVM, you should install it on system root path or make a soft link to rake. Maybe whenever can't find rake in /usr/bin/.
分享到:
相关推荐
6. **任务和调度**:如果需要定时发送推送,如新闻简报,项目可能利用 `Rake` 任务或者第三方库如 `Whenever` 来安排推送。 7. **日志和错误处理**:在生产环境中,项目会记录推送过程中的错误和异常,这有助于调试...
`whenever`是Rails框架中用于简化Cron表达式创建的gem,它允许开发者用更易读的语法来定义计划任务。例如: ```ruby every 2.days, at: '3:00 am' do runner "MyModel.some_process" end ``` 在这个例子中,`...
- 首先,通过`gem install whenever`安装whenever。 - 进入项目目录,确保有一个名为`config`的文件夹,或者在Rails项目中,这个文件夹通常已存在。 - 使用`wheneverize .`命令,会在`config`目录下生成`schedule...
`whenever` gem提供了一个更友好的界面来配置Cron作业,它将Ruby代码转换为Cron表达式,使得在Rails应用中管理定时任务变得更加简单。在Gemfile中添加`whenever`,然后在`config/schedule.rb`中定义任务: ```...
2. **whenever**:whenever是一个简化了在Rails应用中配置Cron作业的gem。它允许你用简洁的Ruby语法编写Cron表达式,并将它们写入Crontab文件。例如,如果你想在每天的凌晨1点运行一个任务,你可以在`config/...
在Ruby中,可以使用`whenever` gem来简化CRON作业的配置。 5. **数据库存储**:为了保存预定的推文及其发布时间,通常会使用数据库。Rails(Ruby的一个Web开发框架)默认使用ActiveRecord,它简化了与数据库的交互...
在Ruby社区,还有许多用于执行周期性任务的Gem,如`Whenever`、`Rufus-Scheduler`和`Sidekiq`,它们提供了方便的方式来调度和管理后台任务,提高应用程序的响应速度。 总的来说,通过使用`profile.rb`这样的性能...
你还应该设置自动化的任务调度(例如使用`whenever` gem)来处理后台任务,以及监控和日志记录。 此外,Mastodon是一个活跃的项目,定期更新以修复漏洞和添加新功能。因此,建议定期更新你的代码库以保持安全和最新...