apiVersion: v1
kind: ConfigMap
metadata:
  name: scripts
  annotations:
      argocd.argoproj.io/hook: PreSync
      argocd.argoproj.io/sync-wave: "1"
      argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
  labels:
    app: scripts
data:
  kate-termination.sh: |
    #!/bin/sh -e
    bash -c ' celery -A autoai.celery.core.celery worker -l INFO -Ofair --autoscale=0,0 --without-heartbeat '
    sleep 3
    kill -9 $(ps aux | grep celery | grep -v grep | awk '{print $2}' | tr '\n' ' ') > /dev/null 2>&1
  stop-worker.sh: |
    #!/bin/sh -e
    echo "Shuting down celery@$HOSTNAME"
    celery control shutdown -d celery@$HOSTNAME
    curl -X POST -H 'Content-type: application/json' --data '{"text":"'"Gracefully terminating $HOSTNAME, cluster: $cluster namespace: $namespace "'"}' https://hooks.slack.com/services/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
  stop-gunicorn.sh: |
    #!/bin/sh -e
    pkill -HUP -f gunicorn.*master
  # check-running-projects.sh: |
  #   #!/bin/bash -e
  #   last=$(ls migrations/db/versions | grep -v Pipfile | grep -v __pycache__ | tail -1)
  #   echo "$last"
  #   test="migrations/db/versions/$last"
  #   revision=$(cat $test | grep revision | awk '{print $3}' | sed -n 2p)
  #   finalrevision=$(echo $revision | tr -d \')
  #   echo "$finalrevision"

  #   checkcurrent1=$(pipenv run alembic --name db current > currentrevision.txt)
  #   checkcurrent2=$(cat currentrevision.txt | awk '{print $1}')

  #   echo "current revision $checkcurrent2"

  #    if [ $finalrevision = $checkcurrent2 ]; then
  #     echo "no migration is needed"
  #     else
  #     echo "Need to run migration!"

  #    CurrentRunningProjects=$(psql -t "$SQLALCHEMY_DATABASE_URI" -c "SELECT COUNT (*) FROM engine_audits WHERE _engine_status NOT in ('Complete') and _engine_status NOT in ('Terminated') and _engine_status NOT in ('Engine Stopped Manually')" | xargs)
  #    RunningProjectIDS=$(psql -t "$SQLALCHEMY_DATABASE_URI" -c "SELECT project_id FROM engine_audits WHERE _engine_status NOT in ('Complete') and _engine_status NOT in ('Terminated') and _engine_status NOT in ('Engine Stopped Manually')")
  #    echo "Current Active Projects: $CurrentRunningProjects"
  #    echo "Project ID's: $RunningProjectIDS"
  #    # wait 10 second before checking
  #    sleep 10
  #    while [ $CurrentRunningProjects -ge 1 ]; do
  #    echo "Projects are still running... waiting"
  #    sleep 25
  #    CurrentRunningProjects=$(psql -t "$SQLALCHEMY_DATABASE_URI" -c "SELECT COUNT (*) FROM engine_audits WHERE _engine_status NOT in ('Complete') and _engine_status NOT in ('Terminated') and _engine_status NOT in ('Engine Stopped Manually')")
  #    RunningProjectIDS=$(psql -t "$SQLALCHEMY_DATABASE_URI" -c "SELECT project_id FROM engine_audits WHERE _engine_status NOT in ('Complete') and _engine_status NOT in ('Terminated') and _engine_status NOT in ('Engine Stopped Manually')")
  #    echo "Current Active Projects: $CurrentRunningProjects"
  #    echo "Project ID's: $RunningProjectIDS"
  #    done

  #    echo "Running migration and Deploying the version!"
  #    fi
