CronJobs in Kontroler
Creating CronJobs
Kontroler makes it easy to create scheduled workflows using cron syntax. These workflows can be configured with retry logic, conditional execution, and sophisticated error handling.
Basic CronJob Structure
Here’s a simple example that runs every minute and demonstrates retry logic:
apiVersion: kontroler.greedykomodo/v1alpha1
kind: DAG
metadata:
name: cronjob
spec:
schedule: "*/1 * * * *"
task:
- name: "job"
command: ["sh", "-c"]
args:
[
"if [ $((RANDOM%2)) -eq 0 ]; then echo $second; else exit 1; fi",
]
image: "alpine:latest"
backoff:
limit: 3
conditional:
enabled: true
retryCodes: [1]
Breaking Down the Components
-
Schedule Definition
schedule: "*/1 * * * *"
- Uses standard cron syntax
- This example runs every minute
- Supports all cron expressions
-
Task Configuration
task: - name: "job" image: "alpine:latest"
- Defines the task to be executed
- Specifies the container image
- Each task must have a unique name
-
Command and Arguments
command: ["sh", "-c"] args: ["if [ $((RANDOM%2)) -eq 0 ]; then echo $second; else exit 1; fi"]
- Sets the command to run
- Arguments are passed as an array
- Supports shell commands and scripts
-
Retry Configuration
backoff: limit: 3 conditional: enabled: true retryCodes: [1]
backoff.limit
: Maximum number of retriesconditional.enabled
: Enables conditional retriesretryCodes
: Exit codes that trigger retries
Common Cron Patterns
# Every 5 minutes
schedule: "*/5 * * * *"
# Every hour
schedule: "0 * * * *"
# Every day at midnight
schedule: "0 0 * * *"
# Every Monday at 9am
schedule: "0 9 * * 1"
Best Practices
-
Error Handling
- Always define retry conditions
- Use appropriate backoff limits
- Consider retry codes carefully
-
Resource Management
task: - name: "job" resources: requests: cpu: "100m" memory: "128Mi" limits: cpu: "200m" memory: "256Mi"
-
Logging
- Use clear task names
- Output meaningful logs
- Consider log retention