-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_loop.py
More file actions
73 lines (55 loc) · 2.35 KB
/
live_loop.py
File metadata and controls
73 lines (55 loc) · 2.35 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
import os
import subprocess
import sys
import time
from datetime import datetime, timezone
from dotenv import load_dotenv
load_dotenv()
INTERVAL_SECONDS = float(os.getenv('LIVE_LOOP_INTERVAL_SECONDS', '20'))
RUN_FEATURES = os.getenv('LIVE_LOOP_RUN_FEATURES', 'true').lower() in ('1', 'true', 'yes', 'on')
RUN_SIGNALS = os.getenv('LIVE_LOOP_RUN_SIGNALS', 'true').lower() in ('1', 'true', 'yes', 'on')
def utcnow():
return datetime.now(timezone.utc).replace(tzinfo=None)
def run_script(script, extra_env=None):
print(f'\n{utcnow()} UTC | START | {script}', flush=True)
env = os.environ.copy()
if extra_env:
env.update(extra_env)
proc = subprocess.run(
[sys.executable, script],
text=True,
capture_output=True,
env=env,
)
if proc.stdout:
print(proc.stdout.strip(), flush=True)
if proc.stderr:
print(proc.stderr.strip(), flush=True)
if proc.returncode != 0:
print(f'{utcnow()} UTC | ERROR | {script} | code={proc.returncode}', flush=True)
return False
print(f'{utcnow()} UTC | OK | {script}', flush=True)
return True
def main():
print('live loop started', flush=True)
print(f'interval={INTERVAL_SECONDS}s run_features={RUN_FEATURES} run_signals={RUN_SIGNALS}', flush=True)
print('Keep official_api_collector.py running in another terminal/service.', flush=True)
print('Keep signal_broadcaster.py running in another terminal/service for Telegram + paper trade tracking.', flush=True)
signals_schema_ready = False
while True:
cycle_started = utcnow()
print(f'\n================================================================================', flush=True)
print(f'{cycle_started} UTC | LIVE CYCLE START', flush=True)
print(f'================================================================================', flush=True)
if RUN_FEATURES:
run_script('live_features.py')
if RUN_SIGNALS:
ok = run_script(
'live_signals_v2.py',
{'LIVE_SIGNALS_SKIP_SCHEMA_ENSURE': 'true'} if signals_schema_ready else None,
)
signals_schema_ready = signals_schema_ready or ok
print(f'{utcnow()} UTC | LIVE CYCLE DONE | sleeping {INTERVAL_SECONDS}s', flush=True)
time.sleep(INTERVAL_SECONDS)
if __name__ == '__main__':
main()