Check out How to Schedule Functions as an alternative to this tutorial.
To run functions on a schedule, you can create a function trigger and then specify a schedule using standard cron syntax.
App Platform allows you to Build, deploy, and scale apps quickly using a simple, fully-managed infrastructure solution.
This tutorial shows you how to run scheduled jobs on App Platform using a Cron Worker.
Explore more App Platform tutorials in the DigitalOcean Developer Center»
Quick App Platform tour»
In this tutorial, we'll guide you through the process of setting up a job scheduler in App Platform using a docker container that runs cron as an App Platform Worker. We'll talk you through the steps required to build the docker container in case you want to modify it and deploy the scheduler as an App Platform Worker using the smallest/cheapest container size. Defining your own scheduled jobs is as easy as modifying the included crontab
file.
- Fork this docker-cron repo
- Add the following to your App Spec (yaml):
workers:
- dockerfile_path: Dockerfile
github:
branch: main
deploy_on_push: true
repo: <your-github-username>/docker-cron
instance_count: 1
instance_size_slug: basic-xxs
name: docker-cron
source_dir: /
- Modify
crontab
in the forked repo to add your own cron jobs.
First import or fork https://github.com/DO-Solutions/docker-cron to a new Git repo on Github or Gitlab so that we can deploy our Docker Cron Worker
The provided Dockerfile is used by App Platform to build and run our Cron Worker, it should be modified to your liking
ubuntu:22.04
base image is pulledcron
andcurl
is installed, you can modify this line to include any other tools you may need to run your scheduled jobs
FROM ubuntu:22.04
RUN apt-get update \
&& apt-get install ca-certificates -y \
&& DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install -y cron curl \
# Remove package lists for smaller image sizes
&& rm -rf /var/lib/apt/lists/* \
&& which cron \
&& rm -rf /etc/cron.*/*
COPY crontab /hello-cron
COPY entrypoint.sh /entrypoint.sh
RUN crontab hello-cron
RUN chmod +x entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
# https://manpages.ubuntu.com/manpages/trusty/man8/cron.8.html
# -f | Stay in foreground mode, don't daemonize.
# -L loglevel | Tell cron what to log about jobs (errors are logged regardless of this value) as the sum of the following values:
CMD ["cron","-f", "-L", "2"]
* * * * * curl http://sample-nodejs:8080 >/proc/1/fd/1 2>/proc/1/fd/2
# An empty line is required at the end of this file for a valid cron file.
We're going to assume that you're adding docker-cron to an existing App Platform app. Use doctl
to retrieve your existing apps App Spec and add docker-cron.
- Retrieve App ID
doctl apps list
- Use that ID to retrieve your apps App Spec
doctl apps spec get b6af73dc-8aba-4237-8dc9-b632ad379bd5 > appspec.yaml
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
name: walrus-app
region: nyc
services:
- environment_slug: node-js
git:
branch: main
repo_clone_url: https://github.com/digitalocean/sample-nodejs.git
http_port: 8080
instance_count: 1
instance_size_slug: basic-xxs
name: sample-nodejs
routes:
- path: /
run_command: yarn start
source_dir: /
- Add the Docker-cron worker
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
name: walrus-app
region: nyc
services:
- environment_slug: node-js
git:
branch: main
repo_clone_url: https://github.com/digitalocean/sample-nodejs.git
http_port: 8080
instance_count: 1
instance_size_slug: basic-xxs
name: sample-nodejs
routes:
- path: /
run_command: yarn start
source_dir: /
workers:
- dockerfile_path: Dockerfile
github:
branch: main
deploy_on_push: true
repo: <your-github-username>/docker-cron
instance_count: 1
instance_size_slug: basic-xxs
name: docker-cron
source_dir: /
- Update your app to deploy Docker-cron Worker
doctl apps update b6af73dc-8aba-4237-8dc9-b632ad379bd5 --spec appspec.yaml
We can use doctl
to retrieve our runtime logs and verify our cron is running, by default it will output to console
doctl apps logs b6af73dc-8aba-4237-8dc9-b632ad379bd5 --type=run
Jack Pearce, Solutions Engineer - jpearce@digitalocean.com