Sid
Sid

Reputation: 4997

Rails Capistrano - Whenever Cron appends cron jobs for every Deployment

I am using:

Ruby     1.9.3
whenever 0.9.4
Rails    3.2
and capistrano/whenever extension.

Whenever the deployment happens, it adds entries to the crontab file after each deployment. Since 5 deployments, there are 40 entries in crontab -l, as schedule.rb has 8 cron entries. For each release there are different entries. Should it not overwrite the entries by default?

It recreates entries everytime mentioned in schedule.rb file.

Upvotes: 3

Views: 2667

Answers (2)

Les Nightingill
Les Nightingill

Reputation: 6156

I found that whenever was adding a cron job to the crontab file, with each cron job delimited by a comment line that includes the path to the capistrano releases directory... something like this:

# Begin Whenever generated tasks for: /home/path/www/to/releases/2070527160502/config/schedule.rb

(You can look at the raw crontab file with crontab -e to see what whenever has put in there)

When the next deploy occurs, whenever looks to see if there are comment-delimited cron jobs, but using the new release number. It doesn't find that, so it appends new jobs to the crontab file.

My workaround for this problem is to specify the update_crontab task in deploy.rb with the explicit path to schedule.rb like this:

namespace :deploy do
  task :update_crontab do
    on roles(:all) do
      within current_path do
        execute :bundle, :exec, :whenever, "--update-crontab", "~/path/to/current/config/schedule.rb"
      end
    end
  end
end
after 'deploy:symlink:release', 'deploy:update_crontab'

So the comment delimiters in the crontab file contain the 'current' path and not the 'releases/nnnnnnnnnnn' path.

I suspect that this should not be necessary, but after trying to solve the problem for some time, this what what I ended up with.

Upvotes: 4

fabro
fabro

Reputation: 771

Check if you are setting

set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }

If you do not it will pick the default identifier, which is the expanded path of your config/schedule.rb file.

https://github.com/javan/whenever/blob/6e69dd8a6b3e2a8f4b2911b4efa5aab65cdc9dcb/lib/whenever/command_line.rb#L51

File.expand_path(@options[:file])

Upvotes: 1

Related Questions