{{- /* Cron-Image robust bestimmen */ -}}
{{- $vars := dict "cronImage" (include "mautic.effectiveImage" .) -}}
{{- if and (hasKey .Values "cron") (kindIs "map" .Values.cron) (hasKey .Values.cron "image") -}}
  {{- $_ := set $vars "cronImage" .Values.cron.image -}}
{{- end -}}

apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "mautic.fullname" . }}-segments-update
  labels:
    {{- include "mautic.labels" . | nindent 4 }}
spec:
  schedule: "*/15 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            {{- include "mautic.selectorLabels" . | nindent 12 }}
        spec:
          restartPolicy: OnFailure
          containers:
            - name: mautic-cli
              image: {{ get $vars "cronImage" | quote }}
              imagePullPolicy: {{ include "mautic.effectiveImagePullPolicy" . }}
              command: ["php","/var/www/html/bin/console","mautic:segments:update","--batch-limit","200"]
              env:
                - name: MAUTIC_DB_HOST
                  value: "hf-mautic-mariadb"
                - name: MAUTIC_DB_NAME
                  value: "mautic"
                - name: MAUTIC_DB_USER
                  value: "mautic"
                - name: MAUTIC_DB_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mautic-infra-secrets
                      key: mariadb-password
                - name: MAUTIC_CACHE_HOST
                  value: "hf-mautic-redis-master"
                - name: MAUTIC_CACHE_PORT
                  value: "6379"
                - name: MAUTIC_REDIS_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mautic-infra-secrets
                      key: redis-password
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "mautic.fullname" . }}-campaigns-update
  labels:
    {{- include "mautic.labels" . | nindent 4 }}
spec:
  schedule: "5,20,35,50 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: mautic-cli
              image: {{ get $vars "cronImage" | quote }}
              imagePullPolicy: IfNotPresent
              command: ["php","/var/www/html/bin/console","mautic:campaigns:update"]
              env:
                - name: MAUTIC_DB_HOST
                  value: "hf-mautic-mariadb"
                - name: MAUTIC_DB_NAME
                  value: "mautic"
                - name: MAUTIC_DB_USER
                  value: "mautic"
                - name: MAUTIC_DB_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mautic-infra-secrets
                      key: mariadb-password
                - name: MAUTIC_CACHE_HOST
                  value: "hf-mautic-redis-master"
                - name: MAUTIC_CACHE_PORT
                  value: "6379"
                - name: MAUTIC_REDIS_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mautic-infra-secrets
                      key: redis-password
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "mautic.fullname" . }}-campaigns-trigger
  labels:
    {{- include "mautic.labels" . | nindent 4 }}
spec:
  schedule: "10,25,40,55 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: mautic-cli
              image: {{ get $vars "cronImage" | quote }}
              imagePullPolicy: IfNotPresent
              command: ["php","/var/www/html/bin/console","mautic:campaigns:trigger"]
              env:
                - name: MAUTIC_DB_HOST
                  value: "hf-mautic-mariadb"
                - name: MAUTIC_DB_NAME
                  value: "mautic"
                - name: MAUTIC_DB_USER
                  value: "mautic"
                - name: MAUTIC_DB_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mautic-infra-secrets
                      key: mariadb-password
                - name: MAUTIC_CACHE_HOST
                  value: "hf-mautic-redis-master"
                - name: MAUTIC_CACHE_PORT
                  value: "6379"
                - name: MAUTIC_REDIS_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mautic-infra-secrets
                      key: redis-password
