aboutsummaryrefslogtreecommitdiff
path: root/common.sh
blob: aae076a1d2894138fa383581fcd76f7a8677fca9 (plain)
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
#!/bin/bash
# This script is not meant to be executed directly. It should be sourced by other scripts.

# Single notification function
notify() {
	local title="$1" message="$2" priority="${3:-5}"
	[ -n "${GOTIFY_URL:-}" ] && [ -n "${GOTIFY_TOKEN:-}" ] || return 0
	curl -sS "$GOTIFY_URL/message?token=$GOTIFY_TOKEN" \
		-F "title=$title" -F "message=$message" -F "priority=$priority" \
		>/dev/null 2>&1 || true
}

# Pre-warm rclone connection function
pre_warm_connection() {
	local script_title="$1"
	echo "Pre-warming rclone connection to MEGA..."
	local prewarm_success=false
	for attempt in 1 2 3; do
		echo "Connection attempt $attempt/3..."
		if timeout 120 rclone ls backup_remote: >/dev/null 2>&1; then
			echo "✓ Connection established on attempt $attempt"
			prewarm_success=true
			break
		else
			echo "✗ Attempt $attempt failed"
			[ $attempt -lt 3 ] && sleep 30
		fi
	done

	if [ "$prewarm_success" = false ]; then
		echo "Warning: All pre-warm attempts failed. Operation may be slower or fail."
		notify "$script_title ($(whoami)@$(hostname))" "Pre-warm connection failed - operation may have issues" 6
	fi
}