Search code examples
ruby-on-railscrondockerwheneverdokku

Cron jobs using Whenever with VM, Docker & Dokku


I have a Ruby on Rails app in a Docker container on Ubuntu 14.04. I set up my deployments using Dokku but I'm unsure how to get my cron jobs working correctly.

Currently I'm using the whenever gem which allows me to do something simple like:

every 5.minutes do
  runner 'MyModel.run_something'
end

The problem is I think that every time I deploy using git push dokku master it resets the container and setting it back to it's default thus removing all my cron schedules.

So then I thought maybe the cron scheduling needs to be outside the container and at the VM level instead.

I currently don't see any cron jobs running no matter what I do. Here's what happens when I run crontab -l when ssh'd:

root@dashboard:~# crontab -l
no crontab for root

I'm pretty new to container virtualization so I apologize if I've skipped over a critical part of this but I'm a stumped.


Solution

  • Took me forever to work this one out - ended up call the rails command instead via crontabs. I've also got a rails app uploaded on dokku with ubuntu on the digital ocean server. Trying to get the Whenever gem to work... it just doesn't. whenever -i doesn't work.

    Whenever doesn't actually create any new crontabs for the dokku environment. It's good for figuring out the Cron syntax though!

    So this is how I got scheduled tasks to work in dokku:

    1. Manually create your own crontab via sudo crontab -e which will open it up in vi/vim

    You can use sudo crontab -r to remove it, or sudo crontab -l to view current crontabs

    1. Add the following code to the new crontab

    The below code will execute every 1 minute.

    SHELL=/bin/bash
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    * * * * * /bin/bash -c 'dokku run appname rails r MyModel.run_something'
    

    Make your environmental variables are equivalent to ones you have in the env command

    1. grep CRON /var/log/syslog to see the output log for trouble shooting. You may have to install postfix via sudo apt-get install postfix in order for Cron to send mail notification of errors otherwise you may get “(CRON) info (No MTA installed, discarding output)” error from the syslog.

    2. cat /var/mail/root to view the mail received from Cron - stating errors if a cronjob fails to work.

    Hopefully that's helpful. That's what got me through at least!