Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restart fetch only when it is idling #548

Merged
merged 8 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/restart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ jobs:
- name: restart gather
uses: cloud-gov/cg-cli-tools@main
with:
command: tools/restart_gather.sh
command: tools/restart_harvester.sh catalog-gather
cf_org: gsa-datagov
cf_space: prod
cf_username: ${{secrets.CF_SERVICE_USER}}
cf_password: ${{secrets.CF_SERVICE_AUTH}}
- name: restart fetch
uses: cloud-gov/cg-cli-tools@main
with:
command: cf restart catalog-fetch
command: tools/restart_harvester.sh catalog-fetch
cf_org: gsa-datagov
cf_space: prod
cf_username: ${{secrets.CF_SERVICE_USER}}
Expand Down
35 changes: 0 additions & 35 deletions tools/restart_gather.sh

This file was deleted.

47 changes: 47 additions & 0 deletions tools/restart_harvester.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Custom script to ensure harvester processes are safe to restart
# Based on
# - https://github.com/GSA/data.gov/issues/3796
# - https://github.com/GSA/data.gov/issues/3962

# Input either catalog-gather or catalog-fetch
app_to_restart=$1

###################
# Utility Functions
# Get the number of lines in the log (int)
log_count () { cf logs --recent $app_to_restart | wc -l; }

# Get the time of the last line in the log (int)
current_time=$(date --utc +%s)
log_last_time () {
date --utc --date="$(cf logs --recent $app_to_restart | tail -n 1 | awk '{split($0,time," "); print time[1]}')" +%s
}

# Restart if CPU usage is < 1
cpu_restart () {
instances=$(cf app $app_to_restart | grep '^instances:' | sed 's/.*\///')
i=0
cf app $app_to_restart | tail -n $instances | awk '{ split($4,cpu,"."); print cpu[1]}' | while read -r cpu ; do
if [[ $cpu < 1 ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do we get $cpu from?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The while read -r cpu iterates through the previous command and makes the iteration available through the cpu variable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I feel like this line might benefit from a comment explaining the pipes.

cf restart-app-instance $app_to_restart $i
fi
i=$(($i + 1))
done;
}

############
# Main Logic
# Check the age of the last log
# If there are only two lines, there are no logs
if [[ $((`log_count` > 2)) == '1' ]]; then
# if the timestamp is younger than 15 mins from start of script
if [[ $((current_time - `log_last_time` < 900)) == '1' ]]; then
echo "Logs are too new! Not going to restart"
exit 0
fi
fi

# if CPU status shows it is not busy, we do the restart
$(cpu_restart)