使用 rake 任务部署后以及 gem 时未设置 Crontab
Crontab is not setup after a deployment with rake task and whenever gem
我使用 gem whenever
通过我的 rails 6.0 应用程序管理 cron 任务。管理 Cron 任务的 rake 任务在部署期间被调用,但 crontab 计划在部署后为空。
config/schedule.rb
require 'active_support/core_ext/object/blank.rb'
env :PATH, ENV['PATH']
set :output, "log/cron_log.log"
set :runner_command, "rails runner"
set :chronic_options, hours24: true
every 1.day, at: ['5:01', '11:01', '17:01','23:01'] do
runner "Task1"
end
every 1.hours do
runner "Task2"
end
我在 lib/tasks/start.rake
中创建了一个 rake 任务
desc 'launch cron task'
task :start_whenever do
puts 'rake start_whenever'
sh 'whenever'
sh 'bundle exec whenever --update-crontab'
puts **'cron activate'**
end
当我在部署后手动启动 rake 任务时bundle exec rake start_whenever
,它工作得很好
但我希望在每次部署后自动运行此任务。所以我尝试了 2 种解决方案:
首先,按照我的托管服务的建议,我在配置文件中调用了 rack 任务。
https://www.clever-cloud.com/doc/getting-started/by-language/ruby/
clevercloud/ruby.json
{
"deploy": {
"rakegoals": ["assets:precompile", "db:migrate", "start_whenever"]
}
}
其次,我从迁移中调用 raketask
class LaunchWhenever < ActiveRecord::Migration[6.0]
def change
Rake::Task['start_whenever'].invoke
end
end
两个结果是一样的。在部署期间调用了 rake 任务 start_whenever
,但未设置 crontab。
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
2021-07-12T09:00:57.352Z: ## [message] Run `whenever --help' for more options.
2021-07-12T09:00:57.352Z: bundle exec whenever --update-crontab
2021-07-12T09:00:57.353Z: [write] crontab file updated
2021-07-12T09:00:57.353Z: **cron activate**
2021-07-12T09:00:57.353Z: Creating build cache archive
2021-07-12T09:01:00.700Z: build cache archive successfully created
2021-07-12T09:01:00.700Z: **No cron to setup**
命令行crontab -l
给出no crontab for bas
感谢您的帮助!
环境
Rails version 6.0.3.4
Ruby version ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
RubyGems version 3.0.3
Rack version 2.2.3
JavaScript Runtime Node.js (V8)
gem whenever
无法更新 crontab 本身。您应该使用 Clever Cloud 的 CRON 系统定期执行您的 rake 任务。
我在 Clever Cloud 工作。
我使用 gem whenever
通过我的 rails 6.0 应用程序管理 cron 任务。管理 Cron 任务的 rake 任务在部署期间被调用,但 crontab 计划在部署后为空。
config/schedule.rb
require 'active_support/core_ext/object/blank.rb'
env :PATH, ENV['PATH']
set :output, "log/cron_log.log"
set :runner_command, "rails runner"
set :chronic_options, hours24: true
every 1.day, at: ['5:01', '11:01', '17:01','23:01'] do
runner "Task1"
end
every 1.hours do
runner "Task2"
end
我在 lib/tasks/start.rake
desc 'launch cron task'
task :start_whenever do
puts 'rake start_whenever'
sh 'whenever'
sh 'bundle exec whenever --update-crontab'
puts **'cron activate'**
end
当我在部署后手动启动 rake 任务时bundle exec rake start_whenever
,它工作得很好
但我希望在每次部署后自动运行此任务。所以我尝试了 2 种解决方案:
首先,按照我的托管服务的建议,我在配置文件中调用了 rack 任务。
https://www.clever-cloud.com/doc/getting-started/by-language/ruby/ clevercloud/ruby.json
{
"deploy": {
"rakegoals": ["assets:precompile", "db:migrate", "start_whenever"]
}
}
其次,我从迁移中调用 raketask
class LaunchWhenever < ActiveRecord::Migration[6.0]
def change
Rake::Task['start_whenever'].invoke
end
end
两个结果是一样的。在部署期间调用了 rake 任务 start_whenever
,但未设置 crontab。
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
2021-07-12T09:00:57.352Z: ## [message] Run `whenever --help' for more options.
2021-07-12T09:00:57.352Z: bundle exec whenever --update-crontab
2021-07-12T09:00:57.353Z: [write] crontab file updated
2021-07-12T09:00:57.353Z: **cron activate**
2021-07-12T09:00:57.353Z: Creating build cache archive
2021-07-12T09:01:00.700Z: build cache archive successfully created
2021-07-12T09:01:00.700Z: **No cron to setup**
命令行crontab -l
给出no crontab for bas
感谢您的帮助!
环境
Rails version 6.0.3.4
Ruby version ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
RubyGems version 3.0.3
Rack version 2.2.3
JavaScript Runtime Node.js (V8)
gem whenever
无法更新 crontab 本身。您应该使用 Clever Cloud 的 CRON 系统定期执行您的 rake 任务。
我在 Clever Cloud 工作。