Skip to main content

Overview

The CronJob construct creates a Kubernetes CronJob for running scheduled tasks with configurable history limits and restart policies.

Class Definition

export class CronJob extends Construct {
  constructor(scope: Construct, jobname: string, props: CronJobProps)
}

Constructor Parameters

scope
Construct
required
Parent construct
jobname
string
required
Job name (will be prefixed with release name)
props
CronJobProps
required
CronJob configuration properties

CronJobProps

Extends ContainerProps with cron-specific options.

Container Properties

image
string
required
Docker image name (without tag)
tag
string
default:"GIT_SHA env var"
Docker image tag. Defaults to GIT_SHA environment variable.
secret
string
Name of Kubernetes secret to mount as environment variables
cmd
string[]
Container command to execute
cmd: ['python', 'manage.py', 'cleanup_old_data']
env
Array<{name: string, value: string}>
Additional environment variables
env: [
  { name: 'CLEANUP_DAYS', value: '30' }
]
pullPolicy
'IfNotPresent' | 'Always' | 'Never'
default:"IfNotPresent"
Container image pull policy

CronJob-Specific Properties

schedule
string
required
Cron schedule expression
schedule: '0 2 * * *'  // Every day at 2 AM
schedule: '*/15 * * * *'  // Every 15 minutes
schedule: '0 0 * * 0'  // Every Sunday at midnight
restartPolicy
'Always' | 'OnFailure' | 'Never'
default:"Never"
Restart policy for the job container
  • Never: Don’t restart on failure (default)
  • OnFailure: Restart only if the container fails
  • Always: Always restart the container
successLimit
number
default:"1"
Number of successful finished jobs to retain in history
failureLimit
number
default:"1"
Number of failed jobs to retain in history
secretMounts
Array<{name: string, mountPath: string, subPath: string}>
Secret volume mounts for the cronjob container
secretMounts: [
  {
    name: 'backup-credentials',
    mountPath: '/etc/secrets',
    subPath: 'credentials.json'
  }
]
createServiceAccount
boolean
default:"false"
Creates and attaches a service account to job pods. Service account name is set to the release name.

Naming Convention

CronJob names are automatically prefixed with the release name:
${RELEASE_NAME}-${jobname}
For example, if RELEASE_NAME=myapp and jobname=cleanup, the final name is myapp-cleanup.

Labels

Each cronjob automatically applies the label:
app.kubernetes.io/name: <RELEASE_NAME>-<jobname>

Container Ports

CronJobs are created with noContainerPorts: true, meaning no container ports are exposed (see src/cronjob.ts:65).

Usage Examples

Daily Backup Job

import { CronJob } from './cronjob';

new CronJob(chart, 'backup', {
  image: 'pennlabs/backup-tool',
  schedule: '0 2 * * *',  // 2 AM daily
  cmd: ['./backup.sh'],
  secret: 'backup-secrets',
  restartPolicy: 'OnFailure',
  successLimit: 7,  // Keep last 7 successful runs
  failureLimit: 3   // Keep last 3 failed runs
});

Database Cleanup

new CronJob(chart, 'cleanup', {
  image: 'pennlabs/backend',
  schedule: '0 0 * * 0',  // Weekly on Sunday
  cmd: ['python', 'manage.py', 'cleanup_old_records'],
  secret: 'db-secrets',
  env: [
    { name: 'DJANGO_SETTINGS_MODULE', value: 'app.settings.production' },
    { name: 'CLEANUP_DAYS', value: '90' }
  ]
});

Data Sync Job

new CronJob(chart, 'sync', {
  image: 'pennlabs/sync-service',
  schedule: '*/30 * * * *',  // Every 30 minutes
  cmd: ['./sync.sh'],
  secretMounts: [
    {
      name: 'api-credentials',
      mountPath: '/etc/credentials',
      subPath: 'api-key.json'
    }
  ],
  restartPolicy: 'Never',
  successLimit: 10,
  failureLimit: 5
});

Report Generation with Service Account

new CronJob(chart, 'reports', {
  image: 'pennlabs/reporting',
  schedule: '0 9 1 * *',  // 9 AM on first day of month
  cmd: ['python', 'generate_reports.py'],
  secret: 'report-secrets',
  createServiceAccount: true,  // Needs K8s API access
  restartPolicy: 'OnFailure',
  env: [
    { name: 'REPORT_TYPE', value: 'monthly' },
    { name: 'OUTPUT_FORMAT', value: 'pdf' }
  ]
});

Cache Warming

new CronJob(chart, 'cache-warmer', {
  image: 'pennlabs/cache-tool',
  schedule: '0 */4 * * *',  // Every 4 hours
  cmd: ['./warm-cache.sh'],
  secret: 'cache-secrets',
  restartPolicy: 'Never',
  successLimit: 3,
  failureLimit: 2,
  pullPolicy: 'Always'
});

Cron Schedule Format

The schedule follows standard cron format:
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │
│ │ │ │ │
* * * * *

Common Examples:

  • 0 0 * * * - Daily at midnight
  • 0 */6 * * * - Every 6 hours
  • */15 * * * * - Every 15 minutes
  • 0 9 * * 1-5 - 9 AM on weekdays
  • 0 0 1 * * - First day of every month

Environment Variables

Required environment variables:
GIT_SHA
string
required
Git commit SHA. Automatically injected into container and used as default image tag.
RELEASE_NAME
string
default:"undefined_release"
Release name used for prefixing cronjob names and service account names.

Source Reference

Defined in src/cronjob.ts:55

Build docs developers (and LLMs) love