apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "keydb.fullname" . }}-health
  labels:
    {{- include "keydb.labels" . | nindent 4 }}
data:
  ping_readiness_local.sh: |-
    #!/bin/bash

    set -e

    loading_response="LOADING KeyDB is loading the dataset in memory"

    [[ -n "${REDIS_PASSWORD}" ]] && export REDISCLI_AUTH="${REDIS_PASSWORD}"
    response="$(
      timeout -s 15 "${1}" \
      keydb-cli \
        -h localhost \
        -p "${REDIS_PORT}" \
        GET {{ .Values.readinessProbeRandomUuid }}
    )"
    if [ "${response}" = "${loading_response}" ]; then
      echo "${response}"
      exit 1
    fi

  ping_liveness_local.sh: |-
    #!/bin/bash

    set -e

    [[ -n "${REDIS_PASSWORD}" ]] && export REDISCLI_AUTH="${REDIS_PASSWORD}"
    response="$(
      timeout -s 15 "${1}" \
      keydb-cli \
        -h localhost \
        -p "${REDIS_PORT}" \
        PING
    )"
    if [ "${response}" != "PONG" ]; then
      echo "${response}"
      exit 1
    fi

{{- if .Values.scripts.enabled }}

  scripts_local.sh: |-
    #!/bin/bash

    set -e

    script_dir="$(dirname "$0")"
    while true; do
{{- if .Values.scripts.cleanupCoredumps.enabled }}
      "${script_dir}/cleanup_coredumps.sh"
{{- end }}
{{- if .Values.scripts.cleanupTempfiles.enabled }}
      "${script_dir}/cleanup_tempfiles.sh"
{{- end }}
      sleep 60
    done
{{- end }}

{{- if .Values.scripts.cleanupCoredumps.enabled }}
  cleanup_coredumps.sh: |-
    #!/bin/bash

    set -e

    find /data/ -type f -name "core.*" -mmin +{{ .Values.scripts.cleanupCoredumps.minutes }} -delete
{{- end }}
{{- if .Values.scripts.cleanupTempfiles.enabled }}
  cleanup_tempfiles.sh: |-
    #!/bin/bash

    set -e

    find /data/ -type f \( -name "temp-*.aof" -o -name "temp-*.rdb" \) -mmin +{{ .Values.scripts.cleanupTempfiles.minutes }} -delete
{{- end }}
