-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-safe.sh
More file actions
executable file
·95 lines (78 loc) · 2.43 KB
/
upload-safe.sh
File metadata and controls
executable file
·95 lines (78 loc) · 2.43 KB
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
#!/bin/bash
# Safe uploader wrapper script with process locking
# This script prevents multiple instances from running with the same account
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UPLOADER_BIN="${SCRIPT_DIR}/dist/uploader-linux"
# Check if uploader binary exists
if [ ! -f "$UPLOADER_BIN" ]; then
echo -e "${RED}Error: Uploader binary not found at $UPLOADER_BIN${NC}"
echo "Please build the project first: npm run build-native"
exit 1
fi
# Make sure binary is executable
chmod +x "$UPLOADER_BIN"
# Parse account name from arguments
ACCOUNT=""
for ((i=1; i<=$#; i++)); do
if [ "${!i}" = "-a" ] || [ "${!i}" = "--account" ]; then
j=$((i+1))
ACCOUNT="${!j}"
break
fi
done
if [ -z "$ACCOUNT" ]; then
echo -e "${RED}Error: Account name not specified${NC}"
echo "Usage: $0 -a ACCOUNT_NAME [other options]"
exit 1
fi
# Lock file location
LOCK_DIR="$HOME/.tgmanager/locks"
LOCK_FILE="$LOCK_DIR/${ACCOUNT}.lock"
# Create lock directory
mkdir -p "$LOCK_DIR"
# Function to cleanup lock file
cleanup() {
if [ -f "$LOCK_FILE" ]; then
rm -f "$LOCK_FILE"
echo -e "${GREEN}✓ Cleanup complete${NC}"
fi
}
# Set trap to cleanup on exit
trap cleanup EXIT INT TERM
# Check for existing lock
if [ -f "$LOCK_FILE" ]; then
PID=$(cat "$LOCK_FILE" 2>/dev/null || echo "")
if [ -n "$PID" ] && ps -p "$PID" > /dev/null 2>&1; then
echo -e "${RED}Error: Another instance is already running with account '$ACCOUNT' (PID: $PID)${NC}"
echo ""
echo "Options:"
echo " 1. Wait for the other instance to finish"
echo " 2. Stop the other instance: kill $PID"
echo " 3. Force remove lock (if process is stuck): rm $LOCK_FILE"
exit 1
else
echo -e "${YELLOW}Warning: Removing stale lock file${NC}"
rm -f "$LOCK_FILE"
fi
fi
# Create lock with current PID
echo $$ > "$LOCK_FILE"
echo -e "${GREEN}✓ Lock acquired for account '$ACCOUNT'${NC}"
# Run the uploader with all arguments
echo -e "${GREEN}Starting upload...${NC}"
"$UPLOADER_BIN" "$@"
EXIT_CODE=$?
# Cleanup is handled by trap
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ Upload completed successfully${NC}"
else
echo -e "${RED}✗ Upload failed with exit code $EXIT_CODE${NC}"
fi
exit $EXIT_CODE