Skip to content

Support long running cron tasks #480

@roelvanduijnhoven

Description

@roelvanduijnhoven

The best current way to work with cron, as documented here, has some down-sides for me. But, luckily, recent work with a separate environment file (#438) offers a great opportunity to improve cron! :)

The biggest downside of this approach is it will not work with long running cron task. As these will be aborted when a deploy hits. We make use of long running cron tasks quite a bit, and deploy quite a bit.

Cron as native thing

So: let's say we would add an option for a role to define a cronfile:

service: app
servers:
  some-role:
    hosts:
      - 34.35.36.37
    cronfile: config/crontab

Now: on deploy Kamal can create a /etc/cron.d/app-some-role file on the host. Kamal will wrap each line in the config/crontab file (extracted from the image) into something like this:

*/2 * * * * docker run --env-file ~/.kamal/env/roles/app-some-role.env some-image:6ec1f39 [original-command-here]

Now as a result the host system will nicely schedule cron :). Nothing is started twice. And long running task can simply finish.

Now: it's now as simple as that. The role options (like add-host) is for example not copied in this way. So we would need to find a way to mimic these settings as well.

One way to do that is that we could have Kamal write an executable ~/.kamal/run/roles/app-some-role file that will contain the whole command to run a new Docker container. In which case we can rewrite the cron to:

*/2 * * * * ~/.kamal/run/roles/app-some-role [original-command-here]

Note that is roughly the strategy that me and team have been using for cron for the last few years (some home-grown solution on top of Docker Swarm with some Ansible scripts that are retiring in favour of Kamal!). But that cron strategy specifically has been working great.

Could also be that I'm missing some simpler solutions that solves these problems I currently face :)!

(Note only now found out about https://github.com/basecamp/kamal/discussions/categories/ideas, should have used that probably. Sorry!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions