Release Candidate v2.3.1
This commit is contained in:
@@ -2,40 +2,11 @@
|
||||
# AT Queue Client for OpenWRT
|
||||
# Located in /www/cgi-bin/services/at_queue_client
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
AUTH_FILE="/tmp/auth_success"
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
RESULTS_DIR="$QUEUE_DIR/results"
|
||||
QUEUE_MANAGER="/www/cgi-bin/services/at_queue_manager.sh"
|
||||
POLL_INTERVAL=0.01
|
||||
SCRIPT_NAME_LOG="at_queue_client"
|
||||
|
||||
# Logging function - uses both centralized and system logging
|
||||
log_at_queue_client() {
|
||||
local level="$1"
|
||||
local message="$2"
|
||||
|
||||
# Use centralized logging
|
||||
case "$level" in
|
||||
"error")
|
||||
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
"warn")
|
||||
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
"debug")
|
||||
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
*)
|
||||
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Also maintain system logging for compatibility
|
||||
logger -t at_queue -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [options] <AT command>"
|
||||
@@ -49,14 +20,14 @@ usage() {
|
||||
# Output JSON response
|
||||
output_json() {
|
||||
local content="$1"
|
||||
local headers="${2:-1}" # Default to showing headers
|
||||
local headers="${2:-1}" # Default to showing headers
|
||||
echo "$content"
|
||||
}
|
||||
|
||||
# URL decode function
|
||||
urldecode() {
|
||||
local encoded="$1"
|
||||
log_at_queue_client "debug" "urldecode: input='$encoded'"
|
||||
logger -t at_queue -p daemon.debug "urldecode: input='$encoded'"
|
||||
|
||||
# Handle %2B -> + and %22 -> " conversions
|
||||
local decoded="${encoded//%2B/+}"
|
||||
@@ -64,23 +35,10 @@ urldecode() {
|
||||
# Then handle other encoded characters
|
||||
decoded=$(printf '%b' "${decoded//%/\\x}")
|
||||
|
||||
log_at_queue_client "debug" "urldecode: output='$decoded'"
|
||||
logger -t at_queue -p daemon.debug "urldecode: output='$decoded'"
|
||||
echo "$decoded"
|
||||
}
|
||||
|
||||
# URL encode function (simplified for AT commands)
|
||||
urlencode() {
|
||||
local string="$1"
|
||||
# Simple encoding for common AT command characters
|
||||
string="${string// /%20}"
|
||||
string="${string//+/%2B}"
|
||||
string="${string//\"/%22}"
|
||||
string="${string//=/%3D}"
|
||||
string="${string//&/%26}"
|
||||
string="${string//?/%3F}"
|
||||
echo "$string"
|
||||
}
|
||||
|
||||
# Extract command ID from response with improved error handling
|
||||
get_command_id() {
|
||||
local response="$1"
|
||||
@@ -114,19 +72,19 @@ get_command_id() {
|
||||
# Normalize AT command
|
||||
normalize_at_command() {
|
||||
local cmd="$1"
|
||||
log_at_queue_client "debug" "normalize: input='$cmd'"
|
||||
logger -t at_queue -p daemon.debug "normalize: input='$cmd'"
|
||||
|
||||
# URL decode the command
|
||||
cmd=$(urldecode "$cmd")
|
||||
log_at_queue_client "debug" "normalize: after urldecode='$cmd'"
|
||||
logger -t at_queue -p daemon.debug "normalize: after urldecode='$cmd'"
|
||||
|
||||
# Remove any carriage returns or newlines
|
||||
cmd=$(echo "$cmd" | tr -d '\r\n')
|
||||
log_at_queue_client "debug" "normalize: after cleanup='$cmd'"
|
||||
logger -t at_queue -p daemon.debug "normalize: after cleanup='$cmd'"
|
||||
|
||||
# Trim leading/trailing whitespace while preserving quotes
|
||||
cmd=$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
log_at_queue_client "debug" "normalize: final output='$cmd'"
|
||||
logger -t at_queue -p daemon.debug "normalize: final output='$cmd'"
|
||||
|
||||
echo "$cmd"
|
||||
}
|
||||
@@ -143,7 +101,7 @@ submit_command() {
|
||||
|
||||
# Submit using appropriate method
|
||||
if [ "${SCRIPT_NAME}" != "" ]; then
|
||||
# CGI mode - direct execution like the original working version
|
||||
# CGI mode - direct execution
|
||||
local escaped_cmd=$(echo "$cmd" | sed 's/"/\\"/g')
|
||||
QUERY_STRING="action=enqueue&command=${escaped_cmd}&priority=$priority" "$QUEUE_MANAGER"
|
||||
else
|
||||
@@ -160,7 +118,7 @@ check_result() {
|
||||
if [ -f "$RESULTS_DIR/$cmd_id.json" ]; then
|
||||
local result_content=$(cat "$RESULTS_DIR/$cmd_id.json")
|
||||
if [ -z "$result_content" ]; then
|
||||
log_at_queue_client "error" "Empty result file for command ID: $cmd_id"
|
||||
logger -t at_queue -p daemon.error "Empty result file for command ID: $cmd_id"
|
||||
local error_json="{\"error\":\"Empty result file\",\"command_id\":\"$cmd_id\"}"
|
||||
output_json "$error_json" "$show_headers"
|
||||
return 1
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
# Location: /www/cgi-bin/quecmanager/home/speedtest/check_speedtest.sh
|
||||
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
|
||||
# Check if speedtest binary exists and is executable
|
||||
if ! command -v speedtest >/dev/null 2>&1; then
|
||||
echo '{"status":"error","message":"Speedtest binary not found in PATH","available":false}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Get speedtest binary location
|
||||
SPEEDTEST_PATH=$(which speedtest 2>/dev/null)
|
||||
|
||||
# Check if binary is executable
|
||||
if [ ! -x "$SPEEDTEST_PATH" ]; then
|
||||
echo '{"status":"error","message":"Speedtest binary is not executable","available":false,"path":"'$SPEEDTEST_PATH'"}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Try to get version (this also checks if binary works)
|
||||
VERSION_OUTPUT=$(speedtest --version 2>/dev/null | head -1)
|
||||
if [ $? -ne 0 ]; then
|
||||
echo '{"status":"error","message":"Speedtest binary exists but is not working properly","available":false,"path":"'$SPEEDTEST_PATH'"}'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if license is already accepted
|
||||
LICENSE_CHECK=$(timeout 5 speedtest --accept-license --help 2>/dev/null | grep -i "usage\|help" | head -1)
|
||||
if [ -z "$LICENSE_CHECK" ]; then
|
||||
echo '{"status":"warning","message":"Speedtest binary may need license acceptance","available":true,"path":"'$SPEEDTEST_PATH'","version":"'$VERSION_OUTPUT'"}'
|
||||
else
|
||||
echo '{"status":"ok","message":"Speedtest is properly installed and ready","available":true,"path":"'$SPEEDTEST_PATH'","version":"'$VERSION_OUTPUT'"}'
|
||||
fi
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
# Location: /www/cgi-bin/quecmanager/home/speedtest/cleanup_speedtest.sh
|
||||
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
|
||||
# Configuration
|
||||
STATUS_FILE="/tmp/speedtest_status.json"
|
||||
FINAL_RESULT="/tmp/speedtest_final.json"
|
||||
PID_FILE="/tmp/speedtest.pid"
|
||||
LOG_FILE="/tmp/speedtest.log"
|
||||
|
||||
CLEANED_FILES=""
|
||||
KILLED_PROCESSES=""
|
||||
|
||||
# Kill any running speedtest processes
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
PID=$(cat "$PID_FILE" 2>/dev/null)
|
||||
if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then
|
||||
kill -9 "$PID" 2>/dev/null
|
||||
KILLED_PROCESSES="$PID"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Also kill any speedtest processes that might be running without PID file
|
||||
STRAY_PIDS=$(ps | grep speedtest | grep -v grep | awk '{print $1}' 2>/dev/null)
|
||||
if [ -n "$STRAY_PIDS" ]; then
|
||||
for pid in $STRAY_PIDS; do
|
||||
kill -9 "$pid" 2>/dev/null
|
||||
if [ -n "$KILLED_PROCESSES" ]; then
|
||||
KILLED_PROCESSES="$KILLED_PROCESSES,$pid"
|
||||
else
|
||||
KILLED_PROCESSES="$pid"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Remove all speedtest-related files
|
||||
for file in "$STATUS_FILE" "$FINAL_RESULT" "$PID_FILE" "$LOG_FILE"; do
|
||||
if [ -f "$file" ]; then
|
||||
rm -f "$file"
|
||||
if [ -n "$CLEANED_FILES" ]; then
|
||||
CLEANED_FILES="$CLEANED_FILES,$(basename $file)"
|
||||
else
|
||||
CLEANED_FILES="$(basename $file)"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Prepare response
|
||||
if [ -n "$CLEANED_FILES" ] || [ -n "$KILLED_PROCESSES" ]; then
|
||||
echo '{"status":"cleaned","message":"Cleanup completed","cleaned_files":"'$CLEANED_FILES'","killed_processes":"'$KILLED_PROCESSES'","timestamp":'$(date +%s)'}'
|
||||
else
|
||||
echo '{"status":"clean","message":"No cleanup needed","timestamp":'$(date +%s)'}'
|
||||
fi
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
# Location: /www/cgi-bin/quecmanager/home/speedtest/stop_speedtest.sh
|
||||
|
||||
# Configuration
|
||||
STATUS_FILE="/tmp/speedtest_status.json"
|
||||
FINAL_RESULT="/tmp/speedtest_final.json"
|
||||
PID_FILE="/tmp/speedtest.pid"
|
||||
LOG_FILE="/tmp/speedtest.log"
|
||||
|
||||
# Set headers
|
||||
echo "Content-Type: application/json"
|
||||
echo ""
|
||||
|
||||
# Function to cleanup all speedtest files
|
||||
cleanup_all() {
|
||||
rm -f "$STATUS_FILE" "$FINAL_RESULT" "$PID_FILE" "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Check if speedtest is running
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
PID=$(cat "$PID_FILE" 2>/dev/null)
|
||||
if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then
|
||||
# Kill the process
|
||||
kill "$PID" 2>/dev/null
|
||||
sleep 1
|
||||
|
||||
# Force kill if still running
|
||||
if kill -0 "$PID" 2>/dev/null; then
|
||||
kill -9 "$PID" 2>/dev/null
|
||||
fi
|
||||
|
||||
# Wait for process to die
|
||||
count=0
|
||||
while kill -0 "$PID" 2>/dev/null && [ $count -lt 5 ]; do
|
||||
sleep 1
|
||||
count=$((count + 1))
|
||||
done
|
||||
|
||||
# Log the cancellation
|
||||
echo "Speedtest cancelled at $(date)" >> "$LOG_FILE" 2>/dev/null
|
||||
|
||||
# Cleanup files
|
||||
cleanup_all
|
||||
|
||||
echo '{"status":"cancelled","message":"Speedtest cancelled successfully","timestamp":'$(date +%s)'}'
|
||||
else
|
||||
# PID file exists but process is not running
|
||||
cleanup_all
|
||||
echo '{"status":"not_running","message":"No active speedtest found","timestamp":'$(date +%s)'}'
|
||||
fi
|
||||
else
|
||||
# No PID file, cleanup any stale files
|
||||
cleanup_all
|
||||
echo '{"status":"not_running","message":"No active speedtest found","timestamp":'$(date +%s)'}'
|
||||
fi
|
||||
@@ -2,9 +2,6 @@
|
||||
# AT Queue Manager for OpenWRT with Preemption Support and Token System
|
||||
# Located in /www/cgi-bin/services/at_queue_manager
|
||||
|
||||
# Load centralized logging
|
||||
. /www/cgi-bin/services/quecmanager_logger.sh
|
||||
|
||||
# Constants
|
||||
QUEUE_DIR="/tmp/at_queue"
|
||||
QUEUE_FILE="$QUEUE_DIR/queue"
|
||||
@@ -18,32 +15,6 @@ 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_LOG="at_queue_manager"
|
||||
|
||||
# Logging function - uses both centralized and system logging
|
||||
log_at_queue() {
|
||||
local level="$1"
|
||||
local message="$2"
|
||||
|
||||
# Use centralized logging
|
||||
case "$level" in
|
||||
"error")
|
||||
qm_log_error "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
"warn")
|
||||
qm_log_warn "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
"debug")
|
||||
qm_log_debug "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
*)
|
||||
qm_log_info "service" "$SCRIPT_NAME_LOG" "$message"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Also maintain system logging for compatibility
|
||||
logger -t at_queue -p "daemon.$level" "$message"
|
||||
}
|
||||
|
||||
# Utility function for JSON escaping
|
||||
escape_json() {
|
||||
@@ -68,7 +39,7 @@ acquire_lock() {
|
||||
|
||||
while [ $attempt -lt $timeout ]; do
|
||||
if mkdir "$LOCK_DIR" 2>/dev/null; then
|
||||
log_at_queue "debug" "Lock acquired"
|
||||
logger -t at_queue -p daemon.debug "Lock acquired"
|
||||
return 0
|
||||
fi
|
||||
|
||||
@@ -76,18 +47,18 @@ acquire_lock() {
|
||||
attempt=$((attempt + 1))
|
||||
done
|
||||
|
||||
log_at_queue "error" "Failed to acquire lock after $timeout attempts"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock after $timeout attempts"
|
||||
return 1
|
||||
}
|
||||
|
||||
release_lock() {
|
||||
if [ -d "$LOCK_DIR" ]; then
|
||||
rmdir "$LOCK_DIR" 2>/dev/null
|
||||
log_at_queue "debug" "Lock released"
|
||||
logger -t at_queue -p daemon.debug "Lock released"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_at_queue "error" "Lock directory doesn't exist"
|
||||
logger -t at_queue -p daemon.error "Lock directory doesn't exist"
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -98,7 +69,7 @@ init_queue_system() {
|
||||
chmod 755 "$QUEUE_DIR"
|
||||
chmod 644 "$QUEUE_FILE"
|
||||
chmod 755 "$RESULTS_DIR"
|
||||
log_at_queue "info" "Queue system initialized"
|
||||
logger -t at_queue -p daemon.info "Queue system initialized"
|
||||
}
|
||||
|
||||
# Cleanup old results and tracking files
|
||||
@@ -109,7 +80,7 @@ cleanup_old_results() {
|
||||
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"
|
||||
logger -t at_queue -p daemon.debug "Cleaned up old tracking files"
|
||||
|
||||
# Use find with -delete and basic timestamp check for OpenWRT
|
||||
find "$RESULTS_DIR" -name "*.json" -type f -mmin +60 -delete 2>/dev/null || {
|
||||
@@ -128,12 +99,12 @@ cleanup_old_results() {
|
||||
local token_time=$(cat "$TOKEN_FILE" | jsonfilter -e '@.timestamp')
|
||||
if [ $((current_time - token_time)) -gt $TOKEN_TIMEOUT ]; then
|
||||
local token_holder=$(cat "$TOKEN_FILE" | jsonfilter -e '@.id')
|
||||
log_at_queue "warn" "Removing expired token from $token_holder"
|
||||
logger -t at_queue -p daemon.warn "Removing expired token from $token_holder"
|
||||
rm -f "$TOKEN_FILE"
|
||||
fi
|
||||
fi
|
||||
|
||||
log_at_queue "info" "Cleanup: Removed files older than 1 hour"
|
||||
logger -t at_queue -p daemon.info "Cleanup: Removed files older than 1 hour"
|
||||
}
|
||||
|
||||
# Generate unique command ID
|
||||
@@ -151,7 +122,7 @@ start_execution_tracking() {
|
||||
echo "$pid" > "$QUEUE_DIR/pid.$cmd_id"
|
||||
chmod 644 "$QUEUE_DIR/start_time.$cmd_id"
|
||||
chmod 644 "$QUEUE_DIR/pid.$cmd_id"
|
||||
log_at_queue "debug" "Started tracking command $cmd_id (PID: $pid)"
|
||||
logger -t at_queue -p daemon.debug "Started tracking command $cmd_id (PID: $pid)"
|
||||
}
|
||||
|
||||
# Check if running command should be preempted
|
||||
@@ -160,7 +131,7 @@ should_preempt() {
|
||||
local new_priority="$2"
|
||||
|
||||
if [ ! -f "$QUEUE_DIR/start_time.$current_cmd_id" ]; then
|
||||
log_at_queue "debug" "No start time found for $current_cmd_id"
|
||||
logger -t at_queue -p daemon.debug "No start time found for $current_cmd_id"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -173,16 +144,16 @@ should_preempt() {
|
||||
if [ -f "$ACTIVE_FILE" ]; then
|
||||
current_priority=$(cat "$ACTIVE_FILE" | jsonfilter -e '@.priority')
|
||||
else
|
||||
log_at_queue "debug" "No active command found"
|
||||
logger -t at_queue -p daemon.debug "No active command found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ $execution_time -gt $PREEMPTION_THRESHOLD ] && [ $new_priority -lt $current_priority ]; then
|
||||
log_at_queue "info" "Command $current_cmd_id (priority $current_priority) running for ${execution_time}s is eligible for preemption by priority $new_priority"
|
||||
logger -t at_queue -p daemon.info "Command $current_cmd_id (priority $current_priority) running for ${execution_time}s is eligible for preemption by priority $new_priority"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_at_queue "debug" "Command $current_cmd_id not eligible for preemption (time: ${execution_time}s, current priority: $current_priority, new priority: $new_priority)"
|
||||
logger -t at_queue -p daemon.debug "Command $current_cmd_id not eligible for preemption (time: ${execution_time}s, current priority: $current_priority, new priority: $new_priority)"
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -193,7 +164,7 @@ preempt_command() {
|
||||
|
||||
if [ -f "$pid_file" ]; then
|
||||
local pid=$(cat "$pid_file")
|
||||
log_at_queue "info" "Preempting command $cmd_id (PID: $pid)"
|
||||
logger -t at_queue -p daemon.info "Preempting command $cmd_id (PID: $pid)"
|
||||
|
||||
# Send SIGTERM first
|
||||
kill -TERM $pid 2>/dev/null
|
||||
@@ -204,7 +175,7 @@ preempt_command() {
|
||||
# Force kill if still running
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
kill -KILL $pid 2>/dev/null
|
||||
log_at_queue "warn" "Forced termination of command $cmd_id"
|
||||
logger -t at_queue -p daemon.warn "Forced termination of command $cmd_id"
|
||||
fi
|
||||
|
||||
# Record preemption result
|
||||
@@ -214,11 +185,11 @@ preempt_command() {
|
||||
rm -f "$pid_file" "$QUEUE_DIR/start_time.$cmd_id" "$QUEUE_DIR/$cmd_id.exit"
|
||||
[ -f "$ACTIVE_FILE" ] && rm -f "$ACTIVE_FILE"
|
||||
|
||||
log_at_queue "info" "Command $cmd_id preemption complete"
|
||||
logger -t at_queue -p daemon.info "Command $cmd_id preemption complete"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_at_queue "warn" "No PID file found for command $cmd_id"
|
||||
logger -t at_queue -p daemon.warn "No PID file found for command $cmd_id"
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -256,7 +227,7 @@ EOF
|
||||
|
||||
printf "%s" "$response" > "$RESULTS_DIR/$cmd_id.json"
|
||||
chmod 644 "$RESULTS_DIR/$cmd_id.json"
|
||||
log_at_queue "info" "Recorded preemption result for command $cmd_id (duration: ${duration}ms)"
|
||||
logger -t at_queue -p daemon.info "Recorded preemption result for command $cmd_id (duration: ${duration}ms)"
|
||||
}
|
||||
|
||||
# Request a token for direct sms_tool execution
|
||||
@@ -267,7 +238,7 @@ request_token() {
|
||||
|
||||
# Acquire lock first
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for token request"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for token request"
|
||||
echo "{\"error\":\"Could not acquire lock\",\"status\":\"denied\"}"
|
||||
return 1
|
||||
fi
|
||||
@@ -281,11 +252,11 @@ request_token() {
|
||||
|
||||
# Check for expired token (> TOKEN_TIMEOUT seconds old)
|
||||
if [ $((current_time - timestamp)) -gt $TOKEN_TIMEOUT ]; then
|
||||
log_at_queue "warn" "Found expired token from $current_holder, releasing"
|
||||
logger -t at_queue -p daemon.warn "Found expired token from $current_holder, releasing"
|
||||
rm -f "$TOKEN_FILE"
|
||||
# Check for priority preemption
|
||||
elif [ $priority -lt $current_priority ]; then
|
||||
log_at_queue "info" "Preempting token from $current_holder (priority: $current_priority) for $requestor_id (priority: $priority)"
|
||||
logger -t at_queue -p daemon.info "Preempting token from $current_holder (priority: $current_priority) for $requestor_id (priority: $priority)"
|
||||
rm -f "$TOKEN_FILE"
|
||||
else
|
||||
# Token in use and cannot be preempted
|
||||
@@ -307,7 +278,7 @@ request_token() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_at_queue "info" "Direct execution with higher priority than active queue command"
|
||||
logger -t at_queue -p daemon.info "Direct execution with higher priority than active queue command"
|
||||
fi
|
||||
|
||||
# Grant token
|
||||
@@ -325,7 +296,7 @@ release_token() {
|
||||
local requestor_id="$1"
|
||||
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for token release"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for token release"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -334,15 +305,15 @@ release_token() {
|
||||
|
||||
if [ "$current_holder" = "$requestor_id" ]; then
|
||||
rm -f "$TOKEN_FILE"
|
||||
log_at_queue "debug" "Token released by $requestor_id"
|
||||
logger -t at_queue -p daemon.debug "Token released by $requestor_id"
|
||||
release_lock
|
||||
echo "{\"status\":\"released\"}"
|
||||
return 0
|
||||
else
|
||||
log_at_queue "warn" "Token release attempted by $requestor_id but held by $current_holder"
|
||||
logger -t at_queue -p daemon.warn "Token release attempted by $requestor_id but held by $current_holder"
|
||||
fi
|
||||
else
|
||||
log_at_queue "warn" "Token release attempted but no token exists"
|
||||
logger -t at_queue -p daemon.warn "Token release attempted but no token exists"
|
||||
fi
|
||||
|
||||
release_lock
|
||||
@@ -360,11 +331,11 @@ enqueue_command() {
|
||||
# Ensure queue directory exists
|
||||
[ ! -d "$QUEUE_DIR" ] && init_queue_system
|
||||
|
||||
log_at_queue "info" "Enqueuing command: $cmd (priority: $priority, id: $cmd_id)"
|
||||
logger -t at_queue -p daemon.info "Enqueuing command: $cmd (priority: $priority, id: $cmd_id)"
|
||||
|
||||
# Acquire lock for queue modification
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for enqueuing command"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for enqueuing command"
|
||||
echo "{\"error\":\"Queue lock acquisition failed\",\"command\":\"$cmd\"}"
|
||||
return 1
|
||||
fi
|
||||
@@ -387,11 +358,11 @@ enqueue_command() {
|
||||
cat "$QUEUE_FILE" >> "$temp_file"
|
||||
mv "$temp_file" "$QUEUE_FILE"
|
||||
chmod 644 "$QUEUE_FILE"
|
||||
log_at_queue "info" "Added high priority command to front of queue"
|
||||
logger -t at_queue -p daemon.info "Added high priority command to front of queue"
|
||||
else
|
||||
# Normal priority - append to queue
|
||||
echo "$entry" >> "$QUEUE_FILE"
|
||||
log_at_queue "info" "Added normal priority command to end of queue"
|
||||
logger -t at_queue -p daemon.info "Added normal priority command to end of queue"
|
||||
fi
|
||||
|
||||
# Release lock
|
||||
@@ -408,7 +379,7 @@ dequeue_command() {
|
||||
|
||||
# Acquire lock
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for dequeuing command"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for dequeuing command"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -424,7 +395,7 @@ dequeue_command() {
|
||||
# Release lock
|
||||
release_lock
|
||||
|
||||
log_at_queue "debug" "Dequeued command: $(echo "$cmd_entry" | jsonfilter -e '@.command')"
|
||||
logger -t at_queue -p daemon.debug "Dequeued command: $(echo "$cmd_entry" | jsonfilter -e '@.command')"
|
||||
echo "$cmd_entry"
|
||||
}
|
||||
|
||||
@@ -462,7 +433,7 @@ execute_with_timeout() {
|
||||
# Start execution tracking
|
||||
start_execution_tracking "$cmd_id" "$pid"
|
||||
|
||||
log_at_queue "debug" "Started command execution: $command (PID: $pid)"
|
||||
logger -t at_queue -p daemon.debug "Started command execution: $command (PID: $pid)"
|
||||
|
||||
# Wait for completion with shorter polling interval
|
||||
local start_time=$(date +%s)
|
||||
@@ -476,7 +447,7 @@ execute_with_timeout() {
|
||||
# Cleanup
|
||||
rm -f "$QUEUE_DIR/pid.$cmd_id" "$QUEUE_DIR/$cmd_id.exit" "$output_file" "$QUEUE_DIR/start_time.$cmd_id"
|
||||
|
||||
log_at_queue "debug" "Command completed with exit code $exit_code"
|
||||
logger -t at_queue -p daemon.debug "Command completed with exit code $exit_code"
|
||||
echo "$output"
|
||||
return $exit_code
|
||||
fi
|
||||
@@ -500,7 +471,7 @@ execute_with_timeout() {
|
||||
# Cleanup
|
||||
rm -f "$QUEUE_DIR/pid.$cmd_id" "$QUEUE_DIR/$cmd_id.exit" "$output_file" "$QUEUE_DIR/start_time.$cmd_id"
|
||||
|
||||
log_at_queue "warn" "Command timed out after $timeout seconds"
|
||||
logger -t at_queue -p daemon.warn "Command timed out after $timeout seconds"
|
||||
echo "${partial_output:-Command timed out after $timeout seconds}"
|
||||
fi
|
||||
|
||||
@@ -516,7 +487,7 @@ execute_command() {
|
||||
|
||||
local start_time=$(date +%s%3N)
|
||||
|
||||
log_at_queue "info" "Executing command $cmd_id: $cmd_text (priority: $priority)"
|
||||
logger -t at_queue -p daemon.info "Executing command $cmd_id: $cmd_text (priority: $priority)"
|
||||
|
||||
# Execute command with timeout
|
||||
local result=$(execute_with_timeout "$cmd_text" $MAX_TIMEOUT "$cmd_id")
|
||||
@@ -530,16 +501,16 @@ execute_command() {
|
||||
|
||||
if [ $exit_code -eq 124 ]; then
|
||||
status="timeout"
|
||||
log_at_queue "error" "Command $cmd_id timed out after ${duration}ms"
|
||||
logger -t at_queue -p daemon.error "Command $cmd_id timed out after ${duration}ms"
|
||||
elif echo "$result" | grep -q "OK"; then
|
||||
status="success"
|
||||
log_level="info"
|
||||
log_at_queue "info" "Command $cmd_id completed successfully in ${duration}ms"
|
||||
logger -t at_queue -p daemon.info "Command $cmd_id completed successfully in ${duration}ms"
|
||||
elif echo "$result" | grep -q "CME ERROR"; then
|
||||
status="cme_error"
|
||||
log_at_queue "error" "Command $cmd_id failed with CME ERROR in ${duration}ms"
|
||||
logger -t at_queue -p daemon.error "Command $cmd_id failed with CME ERROR in ${duration}ms"
|
||||
else
|
||||
log_at_queue "error" "Command $cmd_id failed with general error in ${duration}ms"
|
||||
logger -t at_queue -p daemon.error "Command $cmd_id failed with general error in ${duration}ms"
|
||||
fi
|
||||
|
||||
# Clean and escape the output
|
||||
@@ -565,7 +536,7 @@ EOF
|
||||
|
||||
# Acquire lock for writing result
|
||||
if ! acquire_lock; then
|
||||
log_at_queue "error" "Failed to acquire lock for writing result"
|
||||
logger -t at_queue -p daemon.error "Failed to acquire lock for writing result"
|
||||
else
|
||||
# Save response
|
||||
printf "%s" "$response" > "$RESULTS_DIR/$cmd_id.json"
|
||||
@@ -590,7 +561,7 @@ process_queue() {
|
||||
# Make sure the lock directory doesn't exist at startup
|
||||
[ -d "$LOCK_DIR" ] && rmdir "$LOCK_DIR" 2>/dev/null
|
||||
|
||||
log_at_queue "info" "Started queue processing daemon"
|
||||
logger -t at_queue -p daemon.info "Started queue processing daemon"
|
||||
|
||||
while true; do
|
||||
# Quick cleanup check
|
||||
@@ -608,12 +579,12 @@ process_queue() {
|
||||
|
||||
# Check for expired token
|
||||
if [ $((current_time - token_time)) -gt $TOKEN_TIMEOUT ]; then
|
||||
log_at_queue "warn" "Removing expired token from $token_holder"
|
||||
logger -t at_queue -p daemon.warn "Removing expired token from $token_holder"
|
||||
rm -f "$TOKEN_FILE"
|
||||
else
|
||||
# Log pause status only every 5 seconds to reduce log spam
|
||||
if [ $((current_time - last_log)) -ge 5 ]; then
|
||||
log_at_queue "debug" "Queue processing paused, token held by $token_holder"
|
||||
logger -t at_queue -p daemon.debug "Queue processing paused, token held by $token_holder"
|
||||
last_log=$current_time
|
||||
fi
|
||||
sleep $POLL_INTERVAL
|
||||
@@ -647,42 +618,42 @@ if [ "${SCRIPT_NAME}" != "" ]; then
|
||||
case "$action" in
|
||||
"enqueue")
|
||||
if [ -n "$command" ]; then
|
||||
log_at_queue "info" "CGI: Received enqueue request for command: $command"
|
||||
logger -t at_queue -p daemon.info "CGI: Received enqueue request for command: $command"
|
||||
enqueue_command "$command" "$priority"
|
||||
else
|
||||
log_at_queue "error" "CGI: Empty command received"
|
||||
logger -t at_queue -p daemon.error "CGI: Empty command received"
|
||||
echo "{\"error\":\"No command specified\"}"
|
||||
fi
|
||||
;;
|
||||
"status")
|
||||
if [ -f "$ACTIVE_FILE" ]; then
|
||||
log_at_queue "debug" "CGI: Status request - queue active"
|
||||
logger -t at_queue -p daemon.debug "CGI: Status request - queue active"
|
||||
cat "$ACTIVE_FILE"
|
||||
else
|
||||
log_at_queue "debug" "CGI: Status request - queue idle"
|
||||
logger -t at_queue -p daemon.debug "CGI: Status request - queue idle"
|
||||
echo "{\"status\":\"idle\"}"
|
||||
fi
|
||||
;;
|
||||
"request_token")
|
||||
if [ -n "$id" ]; then
|
||||
log_at_queue "info" "Token request from $id (priority: ${priority:-10})"
|
||||
logger -t at_queue -p daemon.info "Token request from $id (priority: ${priority:-10})"
|
||||
request_token "$id" "${priority:-10}" "${timeout:-10}"
|
||||
else
|
||||
log_at_queue "error" "Token request missing ID"
|
||||
logger -t at_queue -p daemon.error "Token request missing ID"
|
||||
echo "{\"error\":\"No requestor ID specified\",\"status\":\"denied\"}"
|
||||
fi
|
||||
;;
|
||||
"release_token")
|
||||
if [ -n "$id" ]; then
|
||||
log_at_queue "info" "Token release from $id"
|
||||
logger -t at_queue -p daemon.info "Token release from $id"
|
||||
release_token "$id"
|
||||
else
|
||||
log_at_queue "error" "Token release missing ID"
|
||||
logger -t at_queue -p daemon.error "Token release missing ID"
|
||||
echo "{\"error\":\"No requestor ID specified\",\"status\":\"denied\"}"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
log_at_queue "error" "CGI: Invalid action received: $action"
|
||||
logger -t at_queue -p daemon.error "CGI: Invalid action received: $action"
|
||||
echo "{\"error\":\"Invalid action\"}"
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user