forked from 404-Repo/three-gen-subnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_validator.sh
executable file
·95 lines (70 loc) · 2.45 KB
/
update_validator.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# Define the process names as variables
VALIDATION_CONFIG="validation/validation.config.js"
VALIDATOR_CONFIG="neurons/validator.config.js"
# Define the directory and script names
script_dir="$(dirname "${BASH_SOURCE[0]}")"
# Base directory for scripts
base_dir="$script_dir"
# Function to source script and check for errors
source_script() {
local script_path="$1"
# Source the script and check if it fails
# shellcheck disable=SC1090
if ! source "$script_path"; then
echo "Error: Failed to source $script_path"
exit 1
fi
}
# Navigate to the neurons directory and source update script
# Skipping env update for this update
cd "$base_dir/neurons" || exit 1
source_script "update_env.sh"
# Navigate to the validation directory and source update script
cd "../validation" || exit 1
source_script "update_env.sh"
# Return to the base directory
cd ".." || exit 1
# Extract the validation process name from the validation config file
validation_process=$(grep -oP "(?<=name: ').+?(?=')" "$VALIDATION_CONFIG")
if [ -z "$validation_process" ]; then
echo "Error: Could not find the validation process name in $VALIDATION_CONFIG"
exit 1
fi
# Function to check if a pm2 process is running successfully
is_process_running() {
local process_name="$1"
# Get the status of the process
local status
status=$(pm2 info "$validation_process" | grep -i "status" | awk '{print $4}')
if [[ "$status" == "online" ]] || [[ "$status" == "launching" ]]; then
return 0
else
return 1
fi
}
# Function to restart a pm2 process until it succeeds or max attempts reached
restart_with_retry() {
local process_name="$1"
local max_attempts="$2"
local attempt=0
while (( attempt < max_attempts )); do
pm2 stop "$process_name"
sleep 10 # Adding a delay to allow process to stop completely
pm2 restart "$process_name"
sleep 10 # Adding a delay to allow process to restart
if is_process_running "$process_name"; then
echo "$process_name restarted successfully."
return 0
else
echo "Attempt $(( attempt + 1 )) to restart $process_name failed."
fi
(( attempt++ ))
done
echo "Error: Failed to restart $process_name after $max_attempts attempts."
return 1
}
# Restart validation process up to 5 times
restart_with_retry "$VALIDATION_CONFIG" 5
# Restart validator process
pm2 restart "$VALIDATOR_CONFIG"