|
| 1 | +#! /bin/bash |
| 2 | +# |
| 3 | +# Copyright 2018 ABSA Group Limited |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +RECIPIENTS=$1 |
| 18 | +RETENTION_DAYS=$2 |
| 19 | +ENV=$3 |
| 20 | +CONFIG_FILE_LOCATION=${4:-"../../application.properties"} |
| 21 | + |
| 22 | +if [[ -n "$RECIPIENTS" && -n "$RETENTION_DAYS" && -n "$ENV" ]]; then |
| 23 | + echo "Starting runs clean up with retention days: $RETENTION_DAYS, env: $ENV and recipients: $RECIPIENTS" |
| 24 | +else |
| 25 | + echo "Incorrect input. Exiting the program." |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +db_url=$(grep '^db.url=' "$CONFIG_FILE_LOCATION" | awk -F'jdbc:postgresql://' '{print $2}' | awk -F'\\?' '{print $1}') |
| 30 | +db_user=$(grep '^db.user=' "$CONFIG_FILE_LOCATION" | awk -F'=' '{print $2}') |
| 31 | +db_password=$(grep '^db.password=' "$CONFIG_FILE_LOCATION" | awk -F'=' '{print $2}') |
| 32 | +connection_url="postgresql://$db_user:$db_password@$db_url" |
| 33 | + |
| 34 | +obtain_lock_response=$(psql -X $connection_url -c "UPDATE housekeepinglock SET locked = 'true', started_at = now() WHERE locked = false AND started_at is null;" 2>&1) |
| 35 | + |
| 36 | +if [[ $obtain_lock_response == "UPDATE 1" ]]; then |
| 37 | + echo "Exclusive lock for running runs clean up job obtained." |
| 38 | + |
| 39 | + archive_dag_instances_response=$(psql -X $connection_url -c "CALL archive_dag_instances(i_to_ts => (now() - '$RETENTION_DAYS days'::interval)::timestamp without time zone);" 2>&1) |
| 40 | + release_lock_response=$(psql -X $connection_url -c "UPDATE housekeepinglock SET locked = 'false', started_at = null;" 2>&1) |
| 41 | + |
| 42 | + message="" |
| 43 | + subject="" |
| 44 | + if [[ $archive_dag_instances_response == *"ERROR"* ]]; then |
| 45 | + subject="Hyperdrive Notifications - $ENV - Runs clean up failed!" |
| 46 | + message+="Runs clean up job failed with following output:" |
| 47 | + else |
| 48 | + subject="Hyperdrive Notifications - $ENV - Runs clean up succeeded" |
| 49 | + message+="Runs clean up job succeeded with following output:" |
| 50 | + fi |
| 51 | + message+="\n$archive_dag_instances_response" |
| 52 | + |
| 53 | + if [[ $release_lock_response == "UPDATE 1" ]]; then |
| 54 | + message+="\n\nLock for running runs clean job was successfully released" |
| 55 | + else |
| 56 | + message+="\n\nLock for running runs clean job was not released!" |
| 57 | + fi |
| 58 | + echo -e "$message" |
| 59 | + echo -e "$message" | mailx -s "$subject" -r "[email protected]" "$RECIPIENTS" |
| 60 | + |
| 61 | +else |
| 62 | + obtain_lock_time_response=$(psql -X $connection_url -c "SELECT started_at FROM housekeepinglock;" 2>&1) |
| 63 | + |
| 64 | + timestamp_string=$(echo "$obtain_lock_time_response" | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}') |
| 65 | + timestamp_unix=$(date -jf "%Y-%m-%d %H:%M:%S" "$timestamp_string" +%s) |
| 66 | + current_unix=$(date +%s) |
| 67 | + time_difference=$((current_unix - timestamp_unix)) |
| 68 | + days_difference=$((time_difference / 86400)) |
| 69 | + |
| 70 | + if [ "$days_difference" -gt "2" ]; then |
| 71 | + message="Runs clean up has been running for more than 3 days." |
| 72 | + echo -e "$message" |
| 73 | + echo -e "$message" | mailx -s "Hyperdrive Notifications - $ENV - Runs clean up locked" -r "[email protected]" "$RECIPIENTS" |
| 74 | + else |
| 75 | + echo "Could not execute runs clean up job because there is another instance currently running for ($((time_difference / 60)) minutes)." |
| 76 | + fi |
| 77 | +fi |
0 commit comments