Skip to content

Commit b6d2124

Browse files
authored
Merge pull request Blockstream#59 from mempool/junderw/start-script-fix
Feature: Install popular-scripts as a cronjob
2 parents 89f0735 + 67ddaca commit b6d2124

1 file changed

Lines changed: 67 additions & 25 deletions

File tree

start

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FEATURES=default
77
DB_FOLDER=/electrs
88
NODENAME=$(hostname|cut -d . -f1)
99
LOCATION=$(hostname|cut -d . -f2)
10+
USAGE="Usage: $0 (mainnet|testnet|signet|liquid|liquidtestnet) [popular-scripts]"
1011

1112
# load rust if necessary
1213
if [ -e "${HOME}/.cargo/env" ];then
@@ -42,34 +43,93 @@ esac
4243
case "${1}" in
4344
mainnet)
4445
THREADS=$((NPROC / 3))
46+
CRONJOB_TIMING="20 4 * * *"
4547
;;
4648
testnet)
4749
NETWORK=testnet
4850
THREADS=$((NPROC / 6))
51+
CRONJOB_TIMING="2 4 * * *"
4952
;;
5053
testnet4)
5154
NETWORK=testnet4
5255
MAGIC=283f161c
5356
THREADS=$((NPROC / 6))
57+
CRONJOB_TIMING="17 4 * * *"
5458
;;
5559
signet)
5660
NETWORK=signet
5761
THREADS=$((NPROC / 6))
62+
CRONJOB_TIMING="9 4 * * *"
5863
;;
5964
liquid)
6065
DAEMON=elements
6166
NETWORK=liquid
6267
FEATURES=liquid
6368
THREADS=$((NPROC / 6))
69+
CRONJOB_TIMING="12 4 * * *"
6470
;;
6571
liquidtestnet)
6672
DAEMON=elements
6773
NETWORK=liquidtestnet
6874
FEATURES=liquid
6975
THREADS=$((NPROC / 6))
76+
CRONJOB_TIMING="17 4 * * *"
7077
;;
7178
*)
72-
echo "Usage: $0 (mainnet|testnet|testnet4|signet|liquid|liquidtestnet)"
79+
echo "${USAGE}"
80+
exit 1
81+
;;
82+
esac
83+
84+
# Run the popular address txt file generator before each run
85+
POPULAR_SCRIPTS_FOLDER="${HOME}/popular-scripts/${NETWORK}"
86+
POPULAR_SCRIPTS_FILE_RAW="${POPULAR_SCRIPTS_FOLDER}/popular-scripts-raw.txt"
87+
POPULAR_SCRIPTS_FILE="${POPULAR_SCRIPTS_FOLDER}/popular-scripts.txt"
88+
89+
# This function runs the job for generating the popular scripts text file for the precache arg
90+
generate_popular_scripts() {
91+
mkdir -p "${POPULAR_SCRIPTS_FOLDER}"
92+
rm -f "${POPULAR_SCRIPTS_FILE_RAW}" "${POPULAR_SCRIPTS_FILE}"
93+
94+
## Use nproc * 4 threads to generate the txt file (lots of iowait, so 2x~4x core count is ok)
95+
## Only pick up addresses with 101 history events or more
96+
## (Without lowering MIN_HISTORY_ITEMS_TO_CACHE this is the lowest we can go)
97+
## It prints out progress to STDERR
98+
echo "[*] Generating popular-scripts using ${THREADS} threads..."
99+
cd "${HOME}/electrs"
100+
HIGH_USAGE_THRESHOLD=101 \
101+
JOB_THREAD_COUNT=${THREADS} \
102+
nice cargo run \
103+
--release \
104+
--bin popular-scripts \
105+
--features "${FEATURES}" \
106+
-- \
107+
--network "${NETWORK}" \
108+
--db-dir "${DB_FOLDER}" \
109+
> "${POPULAR_SCRIPTS_FILE_RAW}"
110+
111+
## Sorted and deduplicated just in case
112+
sort "${POPULAR_SCRIPTS_FILE_RAW}" | uniq > "${POPULAR_SCRIPTS_FILE}"
113+
rm "${POPULAR_SCRIPTS_FILE_RAW}"
114+
}
115+
116+
# This function is for inserting the cronjob for generating the popular scripts
117+
CRONJOB_CMD="\"${HOME}/electrs/start\" \"${NETWORK}\" popular-scripts"
118+
insert_cronjob() {
119+
(crontab -l 2>/dev/null; echo "${CRONJOB_TIMING} ${CRONJOB_CMD}") | crontab -
120+
}
121+
122+
case "${2}" in
123+
popular-scripts)
124+
echo "[*] Only generate popular-scripts, then exit"
125+
generate_popular_scripts
126+
exit 0
127+
;;
128+
"")
129+
# If the 2nd arg isn't passed, just run the normal electrs script as-is
130+
;;
131+
*)
132+
echo "${USAGE}"
73133
exit 1
74134
;;
75135
esac
@@ -119,31 +179,13 @@ do
119179
ELECTRUM_TXS_LIMIT=9000
120180
fi
121181

122-
# Run the popular address txt file generator before each run
123-
POPULAR_SCRIPTS_FOLDER="${HOME}/popular-scripts/${NETWORK}"
124-
POPULAR_SCRIPTS_FILE_RAW="${POPULAR_SCRIPTS_FOLDER}/popular-scripts-raw.txt"
125-
POPULAR_SCRIPTS_FILE="${POPULAR_SCRIPTS_FOLDER}/popular-scripts.txt"
126-
mkdir -p "${POPULAR_SCRIPTS_FOLDER}"
127-
rm -f "${POPULAR_SCRIPTS_FILE_RAW}" "${POPULAR_SCRIPTS_FILE}"
128-
129-
## Use nproc * 4 threads to generate the txt file (lots of iowait, so 2x~4x core count is ok)
130-
## Only pick up addresses with 101 history events or more
131-
## (Without lowering MIN_HISTORY_ITEMS_TO_CACHE this is the lowest we can go)
132-
## It prints out progress to STDERR
133-
echo "[*] Generating popular-scripts using ${THREADS} threads..."
134-
HIGH_USAGE_THRESHOLD=101 \
135-
JOB_THREAD_COUNT=${THREADS} \
136-
cargo run \
137-
--release \
138-
--bin popular-scripts \
139-
--features "${FEATURES}" \
140-
-- \
141-
--network "${NETWORK}" \
142-
--db-dir "${DB_FOLDER}" \
143-
> "${POPULAR_SCRIPTS_FILE_RAW}"
182+
# if [ ! -e "${POPULAR_SCRIPTS_FILE}" ];then
183+
# generate_popular_scripts
184+
# fi
144185

145-
## Sorted and deduplicated just in case
146-
sort "${POPULAR_SCRIPTS_FILE_RAW}" | uniq > "${POPULAR_SCRIPTS_FILE}"
186+
if [ ! (crontab -l | grep "${CRONJOB_CMD}") ];then
187+
insert_cronjob
188+
fi
147189

148190
# Run the electrs process (Note: db-dir is used in both commands)
149191
cargo run \

0 commit comments

Comments
 (0)