Browse Source

feat(update): make use of flock

Signed-off-by: Tianling Shen <[email protected]>
(cherry picked from commit 32cc0dfa719e035b3b4ed17809a45ee109f4b7e4)
Tianling Shen 1 month ago
parent
commit
bcc9649484
2 changed files with 5 additions and 8 deletions
  1. 1 0
      Makefile
  2. 4 8
      root/usr/share/unblockneteasemusic/update.sh

+ 1 - 0
Makefile

@@ -6,6 +6,7 @@ include $(TOPDIR)/rules.mk
 
 LUCI_TITLE:=LuCI support for UnblockNeteaseMusic
 LUCI_DEPENDS:=+dnsmasq-full +ipset +node \
+	+@BUSYBOX_CONFIG_FLOCK \
 	@(PACKAGE_libustream-mbedtls||PACKAGE_libustream-openssl||PACKAGE_libustream-wolfssl)
 LUCI_PKGARCH:=all
 

+ 4 - 8
root/usr/share/unblockneteasemusic/update.sh

@@ -11,11 +11,11 @@ LOCK="$RUN_DIR/update_core.lock"
 LOG="$RUN_DIR/run.log"
 
 check_core_if_already_running() {
-	if [ -e "$LOCK" ]; then
+	exec 200>"$LOCK"
+
+	if ! flock -n 200 &> /dev/null; then
 		echo -e "\nA task is already running." >> "$LOG"
 		exit 2
-	else
-		touch "$LOCK"
 	fi
 }
 
@@ -25,7 +25,7 @@ clean_log(){
 
 check_core_latest_version() {
 	core_latest_ver="$(wget -qO- 'https://api.github.com/repos/UnblockNeteaseMusic/server/commits?sha=enhanced&path=precompiled' | jsonfilter -e '@[0].sha')"
-	[ -n "$core_latest_ver" ] || { echo -e "\nFailed to check latest core version, please try again later." >> "$LOG"; rm -f "$LOCK"; exit 1; }
+	[ -n "$core_latest_ver" ] || { echo -e "\nFailed to check latest core version, please try again later." >> "$LOG"; exit 1; }
 	if [ ! -e "$UNM_DIR/core_local_ver" ]; then
 		clean_log
 		echo -e "Local version: NOT FOUND, latest version: $core_latest_ver." >> "$LOG"
@@ -38,7 +38,6 @@ check_core_latest_version() {
 		else
 			echo -e "\nLocal version: $(cat $UNM_DIR/core_local_ver 2>"/dev/null"), latest version: $core_latest_ver." >> "$LOG"
 			echo -e "You're already using the latest version." >> "$LOG"
-			rm -f "$LOCK"
 			exit 3
 		fi
 	fi
@@ -55,7 +54,6 @@ update_core() {
 		wget "https://fastly.jsdelivr.net/gh/UnblockNeteaseMusic/server@$core_latest_ver/$file" -qO "$UNM_DIR/core/${file##*/}"
 		[ -s "$UNM_DIR/core/${file##*/}" ] || {
 			echo -e "Failed to download ${file##*/}." >> "$LOG"
-			rm -f "$LOCK"
 			exit 1
 		}
 	done
@@ -65,7 +63,6 @@ update_core() {
 		wget "https://fastly.jsdelivr.net/gh/UnblockNeteaseMusic/server@$core_latest_ver/$cert" -qO "$UNM_DIR/core/$cert"
 		[ -s "$UNM_DIR/core/${cert}" ] || {
 			echo -e "Failed to download ${cert}." >> "$LOG"
-			rm -f "$LOCK"
 			exit 1
 		}
 	done
@@ -76,7 +73,6 @@ update_core() {
 
 	echo -e "Succeeded in updating core." > "$LOG"
 	echo -e "Current core version: $core_latest_ver.\n" >> "$LOG"
-	rm -f "$LOCK"
 }
 
 case "$1" in