Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.9 kB
2
Indexable
Never
#!/bin/bash
export containerid=`docker ps -a | grep "Up" | grep "analytics_v" | awk '{print $1}'`
echo $containerid
docker exec -it --user hobapp $containerid bash

webserver_status=0
scheduler_status=0 
timestamp=$(date +%Y%m%d%H%M%S)
airflow_home=$AIRFLOW_HOME
if ps -ef | grep webserver | grep -v grep > /dev/null; then
  webserver_status=1
  echo "Webserver is Up"
else
  webserver_status=0
  echo "Webserver is Down"
fi
if ps -ef | grep scheduler | grep -v grep > /dev/null; then
  scheduler_status=1
  echo "scheduler is Up"
else
  scheduler_status=0
  echo "scheduler is Down"
fi
#file names for backup
scheduler_err="${AIRFLOW_HOME}/airflow-scheduler.err"
scheduler_log="${AIRFLOW_HOME}/airflow-scheduler.log"
scheduler_out="${AIRFLOW_HOME}/airflow-scheduler.out"
webserver_err="${AIRFLOW_HOME}/airflow-webserver.err"
webserver_log="${AIRFLOW_HOME}/airflow-webserver.log"
webserver_out="${AIRFLOW_HOME}/airflow-webserver.out"
if [ $webserver_status -eq 1 ] && [ $scheduler_status -eq 1 ]; then
  echo "Both webserver and scheduler are up...killing processes and restarting both services"
  pkill -f webserver
  pkill -f scheduler
  if [ -f "$airflow_home/airflow-webserver.pid" ]; then
    echo "deleting webserver pid file"
	rm "$airflow_home/airflow-webserver.pid"
  fi
  if [ -f "$airflow_home/airflow-scheduler.pid" ]; then
    echo "deleting scheduler pid file"
	rm "$airflow_home/airflow-scheduler.pid"
  fi
  airflow webserver -D
  airflow scheduler -D
fi
if [ $webserver_status -eq 0 ] && [ $scheduler_status -eq 0 ]; then
  echo "Both webserver and scheduler are down...starting both webserver and scheduler"
  if [ -f "$airflow_home/airflow-scheduler.err" ]; then
    cp "$scheduler_err" "${scheduler_err}_${timestamp}.bak"
  else
    echo "$scheduler_err file not found"
  fi
  if [ -f "$airflow_home/airflow-scheduler.log" ]; then
    cp "$scheduler_log" "${scheduler_log}_${timestamp}.bak"
  else
    echo "$scheduler_log file not found"
  fi
  if [ -f "$airflow_home/airflow-scheduler.out" ]; then
    cp "$scheduler_out" "${scheduler_out}_${timestamp}.bak"
  else
    echo "$scheduler.out file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver.err" ]; then
    cp "$webserver_err" "${webserver_err}_${timestamp}.bak"
  else
    echo "$webserver_err file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver.log" ]; then
    cp "$webserver_log" "${webserver_log}_${timestamp}.bak"
  else
    echo "$webserver_log file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver.out" ]; then
    cp "$webserver_out" "${webserver_out}_${timestamp}.bak"
  else
    echo "$webserver_out file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver.pid" ]; then
    echo "deleting webserver pid file"
	rm "$airflow_home/airflow-webserver.pid"
  fi
  if [ -f "$airflow_home/airflow-scheduler.pid" ]; then
    echo "deleting scheduler pid file"
	rm "$airflow_home/airflow-scheduler.pid"
  fi
  airflow webserver -D
  airflow scheduler -D
fi
if [ $webserver_status -eq 1 ] && [ $scheduler_status -eq 0 ]; then
  echo "Webserver is up and scheduler is down...starting scheduler"
  if [ -f "$airflow_home/airflow-scheduler.err" ]; then
    cp "$scheduler_err" "${scheduler_err}_${timestamp}.bak"
  else
    echo "$scheduler_err file not found"
  fi
  if [ -f "$airflow_home/airflow-scheduler.log" ]; then
    cp "$scheduler_log" "${scheduler_log}_${timestamp}.bak"
  else
    echo "$scheduler_log file not found"
  fi
  if [ -f "$airflow_home/airflow-scheduler.out" ]; then
    cp "$scheduler_out" "${scheduler_out}_${timestamp}.bak"
  else
    echo "$scheduler_out file not found"
  fi
  if [ -f "$airflow_home/airflow-scheduler.pid" ]; then
    echo "deleting scheduler pid file"
	rm "$airflow_home/airflow-scheduler.pid"
  fi
  airflow scheduler -D
fi
if [ $webserver_status -eq 0 ] && [ $scheduler_status -eq 1 ]; then
  echo "Webserver is down and scheduler is up...starting webserver"
  if [ -f "$airflow_home/airflow-webserver.err" ]; then
    cp "$webserver_err" "${webserver_err}_${timestamp}.bak"
  else
    echo "$webserver_err file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver.log" ]; then
    cp "$webserver_log" "${webserver_log}_${timestamp}.bak"
  else
    echo "$webserver_log file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver_out" ]; then
    cp "$webserver_out" "${webserver_out}_${timestamp}.bak"
  else
    echo "$webserver_out file not found"
  fi
  if [ -f "$airflow_home/airflow-webserver.pid" ]; then
    echo "deleting webserver pid file"
	rm "$airflow_home/airflow-webserver.pid"
  fi
  airflow webserver -D
  sleep 10
  errors = $(cat $airflow_home/airflow_webserver.err)
  if [ -z "$errors" ]; then
  echo "Webserver has errors. Exiting..."
  fi
  exit 1
fi