forked from piraterobot0/Tracking-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_watcher.sh
More file actions
109 lines (93 loc) · 2.92 KB
/
run_watcher.sh
File metadata and controls
109 lines (93 loc) · 2.92 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Derive Options Market Watcher Runner
#
# This script runs the market watcher continuously with configurable intervals.
# It handles logging, error recovery, and data management.
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WATCHER_SCRIPT="$SCRIPT_DIR/watcher.exs"
ANALYZER_SCRIPT="$SCRIPT_DIR/analyzer.exs"
DATA_DIR="${MARKET_DATA_DIR:-$SCRIPT_DIR/market_data}"
LOG_DIR="$SCRIPT_DIR/logs"
SCAN_INTERVAL=${SCAN_INTERVAL:-600} # Default 10 minutes
MAX_FILES=${MAX_FILES:-100} # Keep last 100 snapshots
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Create directories if they don't exist
mkdir -p "$DATA_DIR"
mkdir -p "$LOG_DIR"
# Log file with timestamp
LOG_FILE="$LOG_DIR/watcher_$(date +%Y%m%d).log"
# Function to log messages
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Function to cleanup old files
cleanup_old_files() {
local file_count=$(find "$DATA_DIR" -name "market_*.json" | wc -l)
if [ $file_count -gt $MAX_FILES ]; then
local files_to_delete=$((file_count - MAX_FILES))
log_message "Cleaning up $files_to_delete old snapshot files..."
find "$DATA_DIR" -name "market_*.json" -type f -printf '%T+ %p\n' | \
sort | head -n $files_to_delete | cut -d' ' -f2- | xargs rm -f
fi
}
# Function to run the watcher
run_watcher() {
log_message "Starting market scan..."
if elixir "$WATCHER_SCRIPT" "$DATA_DIR" 2>&1 | tee -a "$LOG_FILE"; then
echo -e "${GREEN}✓${NC} Scan completed successfully"
cleanup_old_files
return 0
else
echo -e "${RED}✗${NC} Scan failed with error code $?"
return 1
fi
}
# Function to analyze latest data
analyze_latest() {
log_message "Analyzing latest market data..."
elixir "$ANALYZER_SCRIPT" latest "$DATA_DIR" 2>&1 | tee -a "$LOG_FILE"
}
# Signal handlers
trap 'echo -e "\n${YELLOW}Stopping market watcher...${NC}"; exit 0' SIGINT SIGTERM
# Main loop
main() {
echo -e "${GREEN}=== Derive Options Market Watcher ===${NC}"
echo "Data directory: $DATA_DIR"
echo "Scan interval: ${SCAN_INTERVAL}s"
echo "Max files to keep: $MAX_FILES"
echo -e "Press ${YELLOW}Ctrl+C${NC} to stop\n"
# Run continuously
while true; do
run_watcher
# Optionally analyze after each scan
if [ "${ANALYZE_AFTER_SCAN:-false}" = "true" ]; then
analyze_latest
fi
echo -e "${YELLOW}Waiting ${SCAN_INTERVAL}s until next scan...${NC}\n"
sleep $SCAN_INTERVAL
done
}
# Parse command line arguments
case "${1:-}" in
once)
# Run once and exit
run_watcher
;;
analyze)
# Just run analysis
analyze_latest
;;
clean)
# Clean old files
cleanup_old_files
;;
*)
# Run continuous monitoring
main
;;
esac