-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py.backup
More file actions
208 lines (184 loc) · 6.95 KB
/
scheduler.py.backup
File metadata and controls
208 lines (184 loc) · 6.95 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Daily Scheduler & Wi-Fi Checker
Logic:
1. Runs continuously (or can be scheduled).
2. Checks date: Has the bot run for TODAY already?
3. Checks time: Is it between 08:00 and 00:00 (Midnight)?
4. Checks Wi-Fi: Is internet available?
5. If all YES -> Run `main.py`.
6. If NO -> Sleep 5 minutes and retry.
"""
import time
import os
import sys
import logging
import subprocess
import socket
import json
from datetime import datetime, time as dtime, timezone, timedelta
# Config
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
START_TIME = dtime(8, 0) # 8:00 AM
END_TIME = dtime(23, 59) # Almost Midnight
CHECK_INTERVAL = 300 # 5 minutes
SCRIPT_TO_RUN = os.path.join(BASE_DIR, "main.py")
RESULTS_DIR = os.path.join(BASE_DIR, "results")
STATE_FILE = os.path.join(BASE_DIR, "scheduler_state.json")
LOCK_FILE = os.path.join(BASE_DIR, "scheduler.lock")
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - SCHEDULER - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(os.path.join(BASE_DIR, "scheduler.log")),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
def is_connected():
"""Check for internet connectivity by pinging Google DNS"""
try:
# Connect to 8.8.8.8 on port 53 (DNS) - fast and reliable
socket.create_connection(("8.8.8.8", 53), timeout=3)
return True
except OSError:
return False
def _load_state():
if not os.path.exists(STATE_FILE):
return {}
try:
with open(STATE_FILE, "r") as handle:
return json.load(handle)
except Exception:
return {}
def _save_state(state):
try:
with open(STATE_FILE, "w") as handle:
json.dump(state, handle, indent=2)
except Exception as exc:
logger.warning(f"Failed to persist scheduler state: {exc}")
def _pid_alive(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False
def _acquire_lock():
if os.path.exists(LOCK_FILE):
try:
with open(LOCK_FILE, "r") as handle:
payload = json.load(handle)
pid = int(payload.get("pid", 0))
if pid and _pid_alive(pid):
logger.error("Scheduler already running (pid=%s). Exiting.", pid)
return False
except Exception:
pass
payload = {
"pid": os.getpid(),
"started_at": datetime.now().isoformat()
}
try:
with open(LOCK_FILE, "w") as handle:
json.dump(payload, handle, indent=2)
except Exception as exc:
logger.warning(f"Failed to write scheduler lock: {exc}")
return True
def has_run_today():
"""Check if a successful run (with email sent) exists for today's date"""
today_str = get_sgt_time().strftime('%Y%m%d')
state = _load_state()
if state.get("last_run_date") == today_str and state.get("last_status") == "success":
return True
return False
def run_pipeline():
"""Execute the main trading pipeline"""
logger.info("Conditions met. Starting Trading Bot Pipeline...")
try:
# Run main.py using the same python interpreter
result = subprocess.run(
[sys.executable, SCRIPT_TO_RUN],
capture_output=True,
text=True
)
if result.returncode == 0:
logger.info("Pipeline completed successfully.")
logger.info(result.stdout)
return True
else:
logger.error("Pipeline failed!")
logger.error(result.stderr)
return False
except Exception as e:
logger.error(f"Failed to launch pipeline: {e}")
return False
def get_sgt_time():
"""Get current time in Singapore (UTC+8)"""
# Create SGT timezone (UTC+8)
sgt_tz = timezone(timedelta(hours=8))
return datetime.now(sgt_tz)
def main_loop():
logger.info("Scheduler started. Monitoring for 8am start time (Singapore Time)...")
if not _acquire_lock():
return
while True:
try:
# Get current time in SGT
now_sgt = get_sgt_time()
current_time = now_sgt.time()
current_day = now_sgt.weekday() # 0=Monday, 6=Sunday
today_str = now_sgt.strftime('%Y%m%d')
# 1. Day of Week Check
# User requested: Don't run on Sunday (6) or Monday (0)
# Because US Market is closed Sat/Sun, so Mon morning SGT (Sun night US) and Sun morning SGT (Sat night US) have no data.
# We run Tue-Sat SGT (processing Mon-Fri US data).
if current_day in [0, 6]: # 0=Mon, 6=Sun
day_name = "Sunday" if current_day == 6 else "Monday"
# Log only once per hour to avoid spam
if current_time.minute == 0:
logger.info(f"Today is {day_name} (SGT). Market closed. Skipping run.")
time.sleep(3600) # Sleep 1 hour
continue
# 2. Check if already ran today
# Note: has_run_today uses local file timestamps. Ideally we check against SGT date.
# But for valid days, we just rely on the file existence for 'today'.
if has_run_today():
logger.info("Bot has already completed its run for today. Waiting for tomorrow...")
time.sleep(3600)
continue
# 3. Check Time Window (8am - 12am SGT)
if not (START_TIME <= current_time <= END_TIME):
# Only log occasionally
if current_time.minute % 30 == 0:
logger.info(f"Outside execution window ({START_TIME}-{END_TIME}). Current SGT time: {current_time.strftime('%H:%M')}.")
time.sleep(CHECK_INTERVAL)
continue
# 4. Check Wi-Fi
if not is_connected():
logger.warning("Inside window but NO INTERNET. Retrying in 5 minutes...")
time.sleep(CHECK_INTERVAL)
continue
# 5. All Green - RUN!
state = _load_state()
state["last_attempt_at"] = now_sgt.isoformat()
state["last_status"] = "running"
_save_state(state)
success = run_pipeline()
if success:
logger.info("Daily run finished. Notification should have been sent.")
state["last_run_date"] = today_str
state["last_status"] = "success"
else:
logger.error("Run failed. Will retry in 5 minutes (unless fixed).")
state["last_status"] = "failed"
state["last_finished_at"] = get_sgt_time().isoformat()
_save_state(state)
time.sleep(CHECK_INTERVAL)
except Exception as exc:
logger.exception(f"Scheduler loop error: {exc}")
time.sleep(CHECK_INTERVAL)
if __name__ == "__main__":
try:
main_loop()
except KeyboardInterrupt:
logger.info("Scheduler stopped by user.")