Cron jobs using Whenever with VM, Docker & Dokku
Asked Answered
L

2

8

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.

Latvia answered 4/11, 2014 at 16:3 Comment(0)
D
10

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!

Dialectal answered 22/11, 2014 at 11:12 Comment(1)
Good workaround but I believe it's really hard to maintain and whenever is out of question, I thought of using run too and I'm trying to find a more robust alternative, good call though.Peeler
S
1

Whenever probably isn't working because the cron daemon isn't running in your Docker/Dokku container. Docker will only run the processes it is told to, using either a CMD or RUN directive or in a script executed by one of those directives.

The Dokku guys have explicitly said that cron is not supported in Dokku, though without saying why. A quick search for cron in the Dokku, Buildstep and Dokku base image repos brings up no results, so it seems to be the case that Dokku never starts a cron service when building/running an app.

The solutions they suggest are to either set up the cron job on the host machine (as you've already figured out), use a web_based scheduling service, or try Heroku's Scheduler.

Swanson answered 22/11, 2014 at 18:22 Comment(2)
Thanks Nick, there's some great information shared here.Latvia
Note: while dokku currently does not manage cron, we do have official documentation on how to use dokku in conjunction with cron here.Downright

© 2022 - 2024 — McMap. All rights reserved.