# Job for one-time data processing
apiVersion: batch/v1
kind: Job
metadata:
  name: data-processing-job
  namespace: my-app
  labels:
    app: data-processor
    job-type: batch
spec:
  completions: 3  # Run 3 times
  parallelism: 2  # Run 2 in parallel
  backoffLimit: 3  # Retry 3 times on failure
  template:
    metadata:
      labels:
        app: data-processor
        job-type: batch
    spec:
      restartPolicy: Never
      containers:
      - name: data-processor
        image: python:3.9-slim
        command: ["python", "process_data.py"]
        env:
        - name: INPUT_FILE
          value: "/data/input.csv"
        - name: OUTPUT_FILE
          value: "/data/output.csv"
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: DB_HOST
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: DB_PASSWORD
        volumeMounts:
        - name: data-volume
          mountPath: /data
        - name: script-volume
          mountPath: /app
        resources:
          requests:
            memory: "128Mi"
            cpu: "250m"
          limits:
            memory: "256Mi"
            cpu: "500m"
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: data-pvc
      - name: script-volume
        configMap:
          name: app-config
---
# CronJob for scheduled tasks
apiVersion: batch/v1
kind: CronJob
metadata:
  name: daily-backup
  namespace: my-app
  labels:
    app: backup
    job-type: scheduled
spec:
  schedule: "0 2 * * *"  # Daily at 2 AM
  concurrencyPolicy: Forbid  # Don't run if previous job is still running
  successfulJobsHistoryLimit: 3  # Keep 3 successful job histories
  failedJobsHistoryLimit: 1  # Keep 1 failed job history
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: backup
            job-type: scheduled
        spec:
          restartPolicy: OnFailure
          containers:
          - name: backup
            image: postgres:13
            command: ["/bin/bash", "-c"]
            args:
            - |
              echo "Starting backup at $(date)"
              pg_dump -h $DB_HOST -U $DB_USER -d $DB_NAME > /backup/backup-$(date +%Y%m%d).sql
              echo "Backup completed at $(date)"
              echo "Backup file size: $(ls -lh /backup/backup-$(date +%Y%m%d).sql)"
            env:
            - name: PGHOST
              valueFrom:
                configMapKeyRef:
                  name: app-config
                  key: DB_HOST
            - name: PGUSER
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: DB_USERNAME
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: DB_PASSWORD
            - name: PGDATABASE
              value: "myapp"
            volumeMounts:
            - name: backup-volume
              mountPath: /backup
            resources:
              requests:
                memory: "256Mi"
                cpu: "250m"
              limits:
                memory: "512Mi"
                cpu: "500m"
          volumes:
          - name: backup-volume
            persistentVolumeClaim:
              claimName: backup-pvc
---
# CronJob for cleanup tasks
apiVersion: batch/v1
kind: CronJob
metadata:
  name: cleanup-logs
  namespace: my-app
  labels:
    app: cleanup
    job-type: maintenance
spec:
  schedule: "0 3 * * 0"  # Weekly on Sunday at 3 AM
  concurrencyPolicy: Allow
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: cleanup
            job-type: maintenance
        spec:
          restartPolicy: OnFailure
          containers:
          - name: cleanup
            image: alpine:latest
            command: ["/bin/sh", "-c"]
            args:
            - |
              echo "Starting log cleanup at $(date)"
              find /logs -name "*.log" -mtime +7 -delete
              echo "Cleanup completed at $(date)"
            volumeMounts:
            - name: logs-volume
              mountPath: /logs
          volumes:
          - name: logs-volume
            persistentVolumeClaim:
              claimName: logs-pvc
