Updated scirpts for Hot Fix

This commit is contained in:
Russel Yasol
2025-08-27 11:39:02 +08:00
parent 6bd2c7ea52
commit 96dd20a758
5 changed files with 125 additions and 67 deletions

View File

@@ -18,7 +18,7 @@ RESULTS_MAX_AGE=3600 # 1 hour in seconds
POLL_INTERVAL=0.01
PREEMPTION_THRESHOLD=2 # 3 seconds threshold for preemption
TOKEN_TIMEOUT=30 # seconds before token expires
SCRIPT_NAME="at_queue_manager"
SCRIPT_NAME_LOG="at_queue_manager"
# Logging function - uses both centralized and system logging
log_at_queue() {
@@ -28,16 +28,16 @@ log_at_queue() {
# Use centralized logging
case "$level" in
"error")
qm_log_error "service" "$SCRIPT_NAME" "$message"
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;;
"warn")
qm_log_warn "service" "$SCRIPT_NAME" "$message"
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;;
"debug")
qm_log_debug "service" "$SCRIPT_NAME" "$message"
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;;
*)
qm_log_info "service" "$SCRIPT_NAME" "$message"
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;;
esac
@@ -81,20 +81,23 @@ acquire_lock() {
}
release_lock() {
if rmdir "$LOCK_DIR" 2>/dev/null; then
if [ -d "$LOCK_DIR" ]; then
rmdir "$LOCK_DIR" 2>/dev/null
log_at_queue "debug" "Lock released"
return 0
else
log_at_queue "error" "Lock directory doesn't exist"
return 1
fi
log_at_queue "error" "Lock directory doesn't exist"
return 1
}
# Ensure required directories exist
initialize_queue() {
init_queue_system() {
mkdir -p "$QUEUE_DIR" "$RESULTS_DIR"
touch "$QUEUE_FILE" "$ACTIVE_FILE"
chmod 666 "$QUEUE_FILE" "$ACTIVE_FILE"
touch "$QUEUE_FILE"
chmod 755 "$QUEUE_DIR"
chmod 644 "$QUEUE_FILE"
chmod 755 "$RESULTS_DIR"
log_at_queue "info" "Queue system initialized"
}
@@ -102,10 +105,13 @@ initialize_queue() {
cleanup_old_results() {
local current_time=$(date +%s)
# Remove old tracking files
find "$QUEUE_DIR" -name "start_time.*" -o -name "pid.*" -type f -mmin +60 -delete 2>/dev/null
# Clean up old execution tracking files
find "$QUEUE_DIR" -name "pid.*" -type f -mmin +60 -delete 2>/dev/null
find "$QUEUE_DIR" -name "*.exit" -type f -mmin +60 -delete 2>/dev/null
find "$QUEUE_DIR" -name "start_time.*" -type f -mmin +60 -delete 2>/dev/null
log_at_queue "debug" "Cleaned up old tracking files"
log_at_queue "debug" "Cleaned up old tracking files" # Use find with -delete and basic timestamp check for OpenWRT
# Use find with -delete and basic timestamp check for OpenWRT
find "$RESULTS_DIR" -name "*.json" -type f -mmin +60 -delete 2>/dev/null || {
# Fallback method if find fails
for file in "$RESULTS_DIR"/*.json; do
@@ -635,24 +641,9 @@ if [ "${SCRIPT_NAME}" != "" ]; then
echo ""
fi
# Log the incoming request for debugging
log_at_queue "debug" "CGI: Incoming request - QUERY_STRING='$QUERY_STRING', REQUEST_METHOD='$REQUEST_METHOD', HTTP_USER_AGENT='$HTTP_USER_AGENT'"
# Parse query string for CGI mode
eval $(echo "$QUERY_STRING" | sed 's/&/;/g')
# Handle empty action parameter specifically
if [ -z "$action" ]; then
if [ -z "$QUERY_STRING" ]; then
log_at_queue "warn" "CGI: No query string provided - possible health check or browser prefetch"
echo "{\"error\":\"No action specified\",\"help\":\"Valid actions: enqueue, status, request_token, release_token\"}"
else
log_at_queue "warn" "CGI: Query string present but no action parameter: '$QUERY_STRING'"
echo "{\"error\":\"Missing action parameter\",\"query_string\":\"$QUERY_STRING\"}"
fi
exit 0
fi
case "$action" in
"enqueue")
if [ -n "$command" ]; then
@@ -691,8 +682,8 @@ if [ "${SCRIPT_NAME}" != "" ]; then
fi
;;
*)
log_at_queue "error" "CGI: Invalid action received: '$action' (QUERY_STRING: '$QUERY_STRING')"
echo "{\"error\":\"Invalid action: $action\",\"valid_actions\":[\"enqueue\",\"status\",\"request_token\",\"release_token\"]}"
log_at_queue "error" "CGI: Invalid action received: $action"
echo "{\"error\":\"Invalid action\"}"
;;
esac
exit 0
@@ -707,4 +698,4 @@ fi
# If not run as CGI, start queue processing
if [ "${SCRIPT_NAME}" = "" ] && [ -z "$1" ]; then
process_queue
fi
fi

View File

@@ -12,39 +12,57 @@ TRACK_FILE="/tmp/quecprofiles_active"
CHECK_TRIGGER="/tmp/quecprofiles_check"
STATUS_FILE="/tmp/quecprofiles_status.json"
APPLIED_FLAG="/tmp/quecprofiles_applied"
DEBUG_LOG="/tmp/quecprofiles_debug.log"
DETAILED_LOG="/tmp/quecprofiles_detailed.log"
DEFAULT_CHECK_INTERVAL=60 # Default check interval in seconds
COMMAND_TIMEOUT=10 # Default timeout for AT commands in seconds
QUEUE_PRIORITY=3 # Medium-high priority (1 is highest for cell scan)
MAX_TOKEN_WAIT=15 # Maximum seconds to wait for token acquisition
SCRIPT_NAME="quecprofile"
SCRIPT_NAME_LOG="quecprofiles_daemon"
# Initialize logging
qm_log_info "service" "$SCRIPT_NAME" "Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)"
# Initialize log files and use centralized logging
mkdir -p "$(dirname "$DEBUG_LOG")" "$(dirname "$DETAILED_LOG")"
touch "$DEBUG_LOG" "$DETAILED_LOG"
chmod 644 "$DEBUG_LOG" "$DETAILED_LOG"
# Function to log messages
# Log startup message using centralized logging
qm_log_info "service" "$SCRIPT_NAME_LOG" "Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)"
# Also maintain file logging for compatibility
echo "$(date) - Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" >"$DEBUG_LOG"
echo "$(date) - Starting QuecProfiles daemon with SA/NSA NR5G and TTL support (PID: $$)" >"$DETAILED_LOG"
# Function to log messages - now uses centralized logging
log_message() {
local message="$1"
local level="${2:-info}"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
# Use centralized logging
case "$level" in
"error")
qm_log_error "service" "$SCRIPT_NAME" "$message"
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;;
"warn")
qm_log_warn "service" "$SCRIPT_NAME" "$message"
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;;
"debug")
qm_log_debug "service" "$SCRIPT_NAME" "$message"
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;;
*)
qm_log_info "service" "$SCRIPT_NAME" "$message"
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;;
esac
# Also log to system log for important messages
if [ "$level" = "error" ] || [ "$level" = "warn" ] || [ "$level" = "info" ]; then
logger -t quecprofiles_daemon -p "daemon.$level" "$message"
# Also maintain system logging for compatibility
logger -t quecprofiles_daemon -p "daemon.$level" "$message"
# Log to debug file (maintain existing behavior)
echo "[$timestamp] [$level] $message" >>"$DEBUG_LOG"
# For detailed logs or errors (maintain existing behavior)
if [ "$level" = "error" ] || [ "$level" = "debug" ]; then
echo "[$timestamp] [$level] $message" >>"$DETAILED_LOG"
fi
}

View File

@@ -3,55 +3,59 @@
# QuecWatch Daemon
# Monitors cellular connectivity and performs recovery actions
# Load UCI configuration functions
. /lib/functions.sh
# Load centralized logging
. /www/cgi-bin/services/quecmanager_logger.sh
# Load UCI configuration functions
. /lib/functions.sh
# Configuration
QUEUE_DIR="/tmp/at_queue"
TOKEN_FILE="$QUEUE_DIR/token"
LOG_DIR="/tmp/log/quecwatch"
LOG_FILE="$LOG_DIR/quecwatch.log"
PID_FILE="/var/run/quecwatch.pid"
STATUS_FILE="/tmp/quecwatch_status.json"
RETRY_COUNT_FILE="/tmp/quecwatch_retry_count"
UCI_CONFIG="quecmanager"
MAX_TOKEN_WAIT=10 # Maximum seconds to wait for token acquisition
TOKEN_PRIORITY=15 # Medium priority (between profiles and metrics)
SCRIPT_NAME="quecwatch"
SCRIPT_NAME_LOG="quecwatch"
# Ensure directories exist
mkdir -p "$QUEUE_DIR"
mkdir -p "$LOG_DIR" "$QUEUE_DIR"
# Store PID
echo "$$" > "$PID_FILE"
chmod 644 "$PID_FILE"
# Function to log messages
# Function to log messages - now uses centralized logging
log_message() {
local level="${2:-info}"
local message="$1"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
# Use centralized logging
case "$level" in
"error")
qm_log_error "service" "$SCRIPT_NAME" "$message"
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
;;
"warn")
qm_log_warn "service" "$SCRIPT_NAME" "$message"
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
;;
"debug")
qm_log_debug "service" "$SCRIPT_NAME" "$message"
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
;;
*)
qm_log_info "service" "$SCRIPT_NAME" "$message"
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
;;
esac
# Also log to system log for important messages
if [ "$level" = "error" ] || [ "$level" = "warn" ] || [ "$level" = "info" ]; then
logger -t quecwatch -p "daemon.$level" "$message"
fi
# Also maintain system logging for compatibility
logger -t quecwatch -p "daemon.$level" "$message"
# Log to file (maintain existing behavior)
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
}
# Function to update status