Scheduled Tasks

Execute commands on a schedule using cron expressions

Scheduled Tasks execute commands automatically at specified times using cron expressions. They provide reliable, time-based automation for recurring operations.

What are Scheduled Tasks?

A Schedule is a configuration that automatically executes a command with arguments at times defined by a cron expression. When you create a Schedule, Weik.io generates an Apache Camel integration flow that uses Quartz for scheduling and the Camel exec component for command execution.

Scheduled Tasks are ideal for:

  • Running maintenance scripts
  • Triggering periodic data syncs
  • Executing automated backups
  • Making scheduled API calls
  • Running system health checks
  • Processing batch operations

How Schedules Work

When you create a Schedule configuration:

  1. Weik.io stores the schedule definition with cron expression, command, and arguments
  2. An integration flow is automatically generated using Apache Camel
  3. A NATS stream is created for the schedule
  4. Integration Agents with matching requirements can execute the schedule
  5. Quartz triggers the schedule based on the cron expression
  6. The specified command is executed with the provided arguments

Creating a Schedule

Define a schedule using YAML configuration:

apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: webhook-test
  description: Call webhook every minute
spec:
  cron: "0 0/1 * * * ?"
  command: curl
  args:
    - "-X"
    - GET
    - "https://api.example.com/health"
  requirements:
    Environment: production
    Network: external

Configuration Properties

cron (required)

Cron expression using Quartz format with 6 fields:

seconds minutes hours day-of-month month day-of-week

Example: "0 0/1 * * * ?" runs every minute at 0 seconds.

command (required)

The command to execute. Can be any executable available to the Integration Agent:

  • curl - Make HTTP requests
  • python - Execute Python scripts
  • node - Run Node.js scripts
  • bash - Run shell scripts
  • Any system command available to the agent

args (optional)

Array of command-line arguments passed to the command in order:

args:
  - "-X"
  - "POST"
  - "https://api.example.com"
  - "-H"
  - "Content-Type: application/json"

requirements (optional)

Key-value pairs used to select which Integration Agent executes the schedule. Requirements specify agent capabilities needed to run the command:

requirements:
  Environment: onpremise
  Region: eu-west
  Datacenter: dc1

Integration Agents register with specific capabilities. The schedule runs on agents that match the requirements.

Cron Expression Format

Schedules use Quartz cron syntax with 6 fields:

* * * * * *
│ │ │ │ │ │
│ │ │ │ │ └─ Day of week (1-7 or MON-SUN)
│ │ │ │ └─── Month (1-12 or JAN-DEC)
│ │ │ └───── Day of month (1-31)
│ │ └─────── Hour (0-23)
│ └───────── Minute (0-59)
└─────────── Second (0-59)

Common Patterns

Every Minute

cron: "0 0/1 * * * ?"  # At 0 seconds of every minute

Every 5 Minutes

cron: "0 0/5 * * * ?"  # At 0 seconds, every 5 minutes

Hourly

cron: "0 0 0/1 * * ?"  # Top of every hour

Daily at 2 AM

cron: "0 0 2 * * ?"  # 2:00:00 AM daily

Weekdays at 9 AM

cron: "0 0 9 ? * MON-FRI"  # 9:00:00 AM Monday through Friday

First Day of Month

cron: "0 0 0 1 * ?"  # Midnight on the 1st of each month

Special Characters

  • * - Any value
  • ? - No specific value (used in day-of-month or day-of-week)
  • - - Range (e.g., MON-FRI)
  • , - List (e.g., MON,WED,FRI)
  • / - Increments (e.g., 0/15 for every 15 units)

Use Cases

API Health Check

apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: api-health-check
  description: Check API health every 5 minutes
spec:
  cron: "0 0/5 * * * ?"
  command: curl
  args:
    - "-f"
    - "https://api.example.com/health"
  requirements:
    Environment: production
    Network: external

Nightly Database Backup

apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: nightly-backup
  description: Backup database at 2 AM daily
spec:
  cron: "0 0 2 * * ?"
  command: bash
  args:
    - "/scripts/backup-database.sh"
  requirements:
    Environment: production
    DatabaseAccess: "true"

Run Python Script

apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: data-sync
  description: Sync data every hour
spec:
  cron: "0 0 0/1 * * ?"
  command: python
  args:
    - "/opt/scripts/sync-data.py"
    - "--incremental"
  requirements:
    Environment: staging
    Region: us-west

Scheduled API Call with POST

apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: trigger-report
  description: Trigger daily report generation
spec:
  cron: "0 0 8 * * MON-FRI"
  command: curl
  args:
    - "-X"
    - "POST"
    - "https://api.example.com/reports/generate"
    - "-H"
    - "Authorization: Bearer ${API_TOKEN}"
    - "-H"
    - "Content-Type: application/json"
    - "-d"
    - '{"reportType":"daily","format":"pdf"}'
  requirements:
    Environment: production
    Team: reporting

Multiple Schedules

apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: morning-cleanup
spec:
  cron: "0 0 6 * * ?"
  command: bash
  args:
    - "/scripts/cleanup-temp.sh"
  requirements:
    Environment: production
    OS: linux
---
apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: hourly-metrics
spec:
  cron: "0 0 0/1 * * ?"
  command: python
  args:
    - "/scripts/collect-metrics.py"
  requirements:
    Environment: production
    Team: monitoring
---
apiVersion: weik.io/v1alpha1
kind: Schedule
metadata:
  name: weekly-report
spec:
  cron: "0 0 9 ? * MON"
  command: curl
  args:
    - "-X"
    - "POST"
    - "https://api.example.com/reports/weekly"
  requirements:
    Environment: production
    Network: external

Agent Selection with Requirements

Requirements specify which Integration Agents can execute a schedule. Agents register with capabilities, and schedules match agents with compatible requirements.

Schedule Requirements:

requirements:
  Environment: production
  Region: us-east
  Datacenter: dc1
  DatabaseAccess: postgres

Agent Registration: An Integration Agent registers with matching capabilities:

  • Environment: production
  • Region: us-east
  • Datacenter: dc1
  • DatabaseAccess: postgres

This agent will execute schedules with matching requirements.

Common Requirement Keys:

  • Environment - Target environment (production, staging, development)
  • Region - Geographic region or cloud region
  • Datacenter - Specific datacenter or availability zone
  • Network - Network access type (internal, external, vpn)
  • DatabaseAccess - Database connectivity requirements
  • OS - Operating system (linux, windows, macos)
  • Team - Team ownership or access
  • Custom keys - Any agent-specific capability

Generated Integration Flow

When you create a Schedule, Weik.io generates an Apache Camel integration flow:

- from:
    uri: "quartz:{{schedule.name}}"
    parameters:
      cron: "{{schedule.cron}}"
    steps:
      - to:
          uri: exec
          parameters:
            executable: "{{schedule.command}}"
            args: "{{schedule.args}}"

This flow:

  • Uses Quartz for cron-based scheduling
  • Executes commands using Camel’s exec component
  • Passes arguments to the command
  • Runs on agents with matching requirements

Best Practices

Use Descriptive Names

metadata:
  name: nightly-database-backup  # Clear purpose
  description: Backup PostgreSQL database to S3

Test Commands First Before scheduling, verify the command works manually on an agent with matching requirements.

Choose Appropriate Intervals

  • Avoid unnecessarily frequent schedules
  • Consider system load during peak hours
  • Use appropriate intervals for your use case

Specify Clear Requirements

requirements:
  Environment: production
  Region: eu-west
  Network: external

Handle Errors in Scripts Implement error handling and logging in scripts executed by schedules.

Monitor Schedule Execution Track schedule runs and investigate failures promptly.

Use Separate Schedules for Different Tasks Don’t combine multiple operations in one schedule. Create separate schedules for better monitoring and control.

Avoid Overlapping Executions Ensure schedules complete before the next trigger. Choose intervals longer than typical execution time.

Troubleshooting

Schedule Not Running

  • Verify cron expression is valid (use 6 fields)
  • Check that an Integration Agent with matching requirements is running
  • Review schedule configuration for errors

Command Not Found

  • Ensure the command is available on the agent
  • Verify the agent has the required tools installed
  • Check requirements match agent capabilities

Arguments Not Working

  • Verify arguments are in the correct order
  • Check argument formatting in the args array
  • Test the command manually with the same arguments

No Matching Agent

  • Review requirements in schedule configuration
  • Check agent registrations and capabilities
  • Ensure at least one agent matches all requirements

What’s Next