apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-cleanup-worker
  namespace: bakery-ia
  labels:
    app: demo-cleanup-worker
    component: background-jobs
    service: demo-session
spec:
  replicas: 2
  selector:
    matchLabels:
      app: demo-cleanup-worker
  template:
    metadata:
      labels:
        app: demo-cleanup-worker
        component: background-jobs
        service: demo-session
    spec:
      initContainers:
      - name: wait-for-migrations
        image: postgres:17-alpine
        command: ["sh", "-c",
          "echo 'Waiting for database to be ready...' && \
           until pg_isready -h demo-session-db-service -p 5432; do sleep 2; done && \
           echo 'Database ready, checking for demo_sessions table...' && \
           MAX_ATTEMPTS=60 && \
           ATTEMPT=1 && \
           until psql -h demo-session-db-service -U demo_session_user -d demo_session_db -c 'SELECT 1 FROM demo_sessions LIMIT 1;' 2>/dev/null; do \
             if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then \
               echo 'ERROR: demo_sessions table not created after maximum attempts'; \
               exit 1; \
             fi; \
             echo \"Waiting for demo_sessions table to be created by migrations... (attempt $ATTEMPT/$MAX_ATTEMPTS)\"; \
             ATTEMPT=$((ATTEMPT + 1)); \
             sleep 5; \
           done && \
           echo 'demo_sessions table is ready!'"]
        env:
        - name: PGPASSWORD
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: DEMO_SESSION_DB_PASSWORD
        resources:
          requests:
            memory: "64Mi"
            cpu: "50m"
          limits:
            memory: "128Mi"
            cpu: "100m"
      containers:
      - name: worker
        image: bakery/demo-session-service
        imagePullPolicy: IfNotPresent
        command:
        - python
        - -m
        - app.jobs.cleanup_worker
        env:
        - name: DEMO_SESSION_DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: DEMO_SESSION_DATABASE_URL
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: redis-secrets
              key: REDIS_PASSWORD
        - name: REDIS_URL
          value: "rediss://:$(REDIS_PASSWORD)@redis-service:6379/0?ssl_cert_reqs=none"
        - name: LOG_LEVEL
          value: "INFO"
        - name: INVENTORY_SERVICE_URL
          value: "http://inventory-service:8000"
        - name: RECIPES_SERVICE_URL
          value: "http://recipes-service:8000"
        - name: SALES_SERVICE_URL
          value: "http://sales-service:8000"
        - name: ORDERS_SERVICE_URL
          value: "http://orders-service:8000"
        - name: PRODUCTION_SERVICE_URL
          value: "http://production-service:8000"
        - name: SUPPLIERS_SERVICE_URL
          value: "http://suppliers-service:8000"
        - name: POS_SERVICE_URL
          value: "http://pos-service:8000"
        - name: PROCUREMENT_SERVICE_URL
          value: "http://procurement-service:8000"
        - name: DISTRIBUTION_SERVICE_URL
          value: "http://distribution-service:8000"
        - name: FORECASTING_SERVICE_URL
          value: "http://forecasting-service:8000"
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          exec:
            command:
            - python
            - -c
            - "import sys; sys.exit(0)"
          initialDelaySeconds: 30
          periodSeconds: 60
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          exec:
            command:
            - python
            - -c
            - "import sys; sys.exit(0)"
          initialDelaySeconds: 10
          periodSeconds: 30
          timeoutSeconds: 5
      restartPolicy: Always
