How To Setup a Job Scheduler on DigitalOcean App Platform

By Jack Pearce on 16 Mar 2023

Introduction

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!

Prerequisites

Before you begin, ensure that you have the following:

Easy setup

  1. Fork Docker-cron repo

  2. 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: /
    
  3. Modify crontab in the forked repo to add your cron jobs.

Elaborate Setup

  1. Fork Docker-cron

  2. 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 pulled
    • cron and curl is installed. You can modify this line to include any other tools you may need to run your scheduled jobs
    FROM 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"]
    
  3. 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.
    

Deploy to App Platform

  1. 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.

  2. Retrieve the App ID

      doctl apps list
    
  3. 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: /
    
  4. 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: /
    
  5. Update your app to deploy Docker-cron Worker

      doctl apps update <app-id> --spec appspec.yaml
    
  6. 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
    

Conclusion

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.