# =============================================================================
# WOOO TECH - Momo Pro System
# 優化版 momo-scheduler Deployment
# 優化重點: 資源配置、健康檢查、爬蟲任務穩定性
# =============================================================================
apiVersion: apps/v1
kind: Deployment
metadata:
  name: momo-scheduler
  namespace: momo
spec:
  replicas: 1
  strategy:
    type: Recreate  # Scheduler 不需要滾動更新，避免重複排程
  selector:
    matchLabels:
      app: momo-scheduler
  template:
    metadata:
      labels:
        app: momo-scheduler
    spec:
      # 優雅終止時間 - 給予爬蟲任務完成時間
      terminationGracePeriodSeconds: 60

      # Init Container - 複製 Google Drive 認證
      initContainers:
      - name: copy-google-credentials
        image: busybox:1.36
        command: ['sh', '-c', 'cp /secrets/* /config/ && chmod 644 /config/*']
        volumeMounts:
        - name: google-drive-secrets
          mountPath: /secrets
          readOnly: true
        - name: momo-config-volume
          mountPath: /config

      containers:
      - name: scheduler
        image: momo-pro-system:local
        imagePullPolicy: Never
        command: ["python", "run_scheduler.py"]

        # 環境變數
        envFrom:
        - configMapRef:
            name: momo-config
        - secretRef:
            name: momo-secrets

        # 資源配置 (爬蟲任務需要較多記憶體)
        resources:
          requests:
            memory: 256Mi
            cpu: 100m
          limits:
            memory: 1Gi
            cpu: 500m

        # 存活探測 - 使用 exec 檢查進程
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "pgrep -f 'python run_scheduler.py' || exit 1"
          initialDelaySeconds: 60
          periodSeconds: 60
          timeoutSeconds: 10
          failureThreshold: 3
          successThreshold: 1

        # 就緒探測 - Scheduler 啟動即就緒
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "pgrep -f 'python run_scheduler.py' || exit 1"
          initialDelaySeconds: 30
          periodSeconds: 30
          timeoutSeconds: 10
          failureThreshold: 3
          successThreshold: 1

        volumeMounts:
        - name: momo-data
          mountPath: /app/data
        - name: momo-config-volume
          mountPath: /app/config

      volumes:
      - name: momo-data
        persistentVolumeClaim:
          claimName: momo-data-pvc
      - name: google-drive-secrets
        secret:
          secretName: google-drive-credentials
      - name: momo-config-volume
        emptyDir: {}
