#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/scheduler-docker-local/internal-functions"

fn-scheduler-docker-local-run-retire-container() {
  declare desc="stop a container"
  declare CONTAINER_ID="$1"

  local DOKKU_DOCKER_STOP_TIMEOUT="$(plugn trigger ps-get-property "$APP" stop-timeout-seconds || true)"

  [[ -n "$DOKKU_DOCKER_STOP_TIMEOUT" ]] && DOCKER_STOP_TIME_ARG="--time=${DOKKU_DOCKER_STOP_TIMEOUT}"

  "$DOCKER_BIN" container update --restart=no "$CONTAINER_ID" &>/dev/null || true
  "$DOCKER_BIN" container stop $DOCKER_STOP_TIME_ARG "$CONTAINER_ID" &>/dev/null || true
  "$DOCKER_BIN" container kill "$CONTAINER_ID" &>/dev/null || true
  "$DOCKER_BIN" container rm "$CONTAINER_ID" &>/dev/null || true

  if "$DOCKER_BIN" container inspect "$CONTAINER_ID" &>/dev/null; then
    dokku_log_warn "Unable to retire container ${CONTAINER_ID}"
    return 1
  fi
}

fn-scheduler-docker-local-run-retire() {
  declare desc="stop all run containers that have exceeded their active deadline"
  declare APP="$1"
  local containers=""

  # find all run containers for the specified app
  container_type_filter="label=com.dokku.container-type=run"
  if [[ -n "$APP" ]]; then
    app_filter="label=com.dokku.app-name=$APP"
    containers="$("$DOCKER_BIN" container ls --filter "$container_type_filter" --filter "$app_filter" --format '{{.ID}} {{.CreatedAt}}')"
  else
    containers="$("$DOCKER_BIN" container ls --filter "$container_type_filter" --format '{{.ID}} {{.CreatedAt}}')"
  fi

  if [[ -z "$containers" ]]; then
    return
  fi

  # iterate over all containers, ignoring the timezone in the last two columns
  echo "$containers" | awk '{NF-=2} 1' | while read -r container_id container_date container_time; do
    # get the cutoff time in seconds from the container's `com.dokku.active-deadline-seconds` label
    active_deadline_seconds="$("$DOCKER_BIN" container inspect "$container_id" --format '{{ index .Config.Labels "com.dokku.active-deadline-seconds" }}')"
    if [[ -z "$active_deadline_seconds" ]]; then
      continue
    fi

    # convert the active deadline seconds to a unix timestamp
    cutoff_time="$(date --date="$active_deadline_seconds seconds ago" "+%s")"

    # convert the container start time to unix timestamp
    start_time="$(date --date="$container_date $container_time" "+%s")"

    # if the container start time is before the cutoff time, stop the container
    if [[ "$start_time" -lt "$cutoff_time" ]]; then
      dokku_log_verbose_quiet "Retiring container ${container_id}"
      fn-scheduler-docker-local-run-retire-container "$container_id"
    fi
  done
}

trigger-scheduler-docker-local-scheduler-run-retire() {
  declare desc="retires all run containers for an app that have exceeded their active deadline"
  declare trigger="scheduler-run-retire"
  declare DOKKU_SCHEDULER="$1" APP="$2"

  if [[ -z "$DOKKU_SCHEDULER" ]]; then
    dokku_log_info1_quiet "Retiring all run containers"
    fn-scheduler-docker-local-run-retire
    return "$?"
  fi

  if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
    return
  fi

  dokku_log_info1_quiet "Retiring run containers for app ${APP}"
  fn-scheduler-docker-local-run-retire "$APP"
}

trigger-scheduler-docker-local-scheduler-run-retire "$@"
