ruby-on-railscronruby-on-rails-6wheneverclever-cloud

Crontab is not setup after a deployment with rake task and whenever gem


I use the gem whenever to manage cron task throught my rails 6.0 app. the rake task that manage Cron task is called during the deployment but crontab schedule is empty after deployment.

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

i created a rake task in 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

When i launch the rake task manually bundle exec rake start_whenever after a deploy, it work perfectly

but i want this task be runned automatically after each deploy. so i tried 2 solutions:

first, as suggested by my hosting service, i call rack task in a configuration file.

https://www.clever-cloud.com/doc/getting-started/by-language/ruby/ clevercloud/ruby.json

{
  "deploy": {
    "rakegoals": ["assets:precompile", "db:migrate", "start_whenever"]
  }
}

second, i call the raketask from the migration

class LaunchWhenever < ActiveRecord::Migration[6.0]
  def change
    Rake::Task['start_whenever'].invoke
  end
end

Both results are the same. the rake task start_whenever is called during the deployment but the crontab is not setup.

## [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**

the command line crontab -l gives no crontab for bas

Thanks for any help!

environnement

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)

Solution

  • The gem whenever wont be able to update crontab itself. You should use Clever Cloud's CRON system to execute your rake tasks periodically.

    I am working at Clever Cloud.