Automating repetitive tasks or processes in your application can save you time and effort. A job scheduler allows you to schedule tasks at specific times, intervals, or dates, freeing you to focus on more important work. In this tutorial, we’ll show you how to set up a job scheduler in App Platform using a Docker container that runs cron as an App Platform Worker.
We’ll guide you through building the Docker container and deploying the scheduler as an App Platform Worker, optimizing for the smallest container size possible. Then, defining your scheduled jobs is as simple as editing the included crontab
file. With this tutorial, you’ll quickly be on your way to an automated workflow!
Before you begin, ensure that you have the following:
Fork Docker-cron repo
Add the following to your App Spec (yaml):
workers:
- dockerfile_path: Dockerfile
github:
branch: main
deploy_on_push: true
repo: DO-Solutions/Docker-cron
instance_count: 1
instance_size_slug: basic-xxs
name: Docker-cron
source_dir: /
Modify crontab
in the forked repo to add your cron jobs.
Fork Docker-cron
Modify Dockerfile
App Platform uses the provided Dockerfile to build and run our Cron Worker, which should be modified to your liking.
ubuntu:22.04
base image is pulledcron
and curl
is installed. You can modify this line to include any other tools you may need to run your scheduled jobsFROM ubuntu
RUN apt-get update \
&& 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"]
CMD ["cron","-f", "-L", "2"]
Modify crontab
to define your cron jobs
* * * * * 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.
Modify your App Spec
We assume you’re adding Docker-cron to an existing App Platform app. Use doctl
to retrieve your current App Spec and add Docker-cron.
Retrieve the App ID
doctl apps list
Use that ID to retrieve your apps App Spec
doctl apps spec get <app-id> > appspec.yaml
# 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: DO-Solutions/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 <app-id> --spec appspec.yaml
Verify worker functionality
We can use doctl
to retrieve our runtime logs and verify that our cron is running. By default, it will output to the console.
doctl apps logs <app-id> --type=run
This tutorial shows you how to install and set up a job scheduler in the App Platform. You can automate repetitive tasks in your application by utilizing a Docker container that runs cron as an App Platform Worker. We also walked you through modifying the crontab file to define your scheduled jobs.
If you wish to delete your app, follow the instructions in the Destroy an App section in the product documentation.
For more information on the App Platform and its features, you can check out the official App Platform product documentation.
Haven’t got a DigitalOcean account? Start your free trial now and grab $200 in credits.