forked from hummingbot/quants-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
315 lines (253 loc) · 11.6 KB
/
cli.py
File metadata and controls
315 lines (253 loc) · 11.6 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
"""
QuantsLab CLI - Main entry point for task management
"""
import asyncio
import argparse
import sys
import os
from dotenv import load_dotenv
from core.tasks.runner import TaskRunner
from core.tasks.base import TaskConfig
import logging
# Load environment variables from .env file
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser(
description='QuantsLab Task Management CLI',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Run tasks continuously from templates
python cli.py run-tasks --config template_1_candles_optimization.yml
python cli.py run-tasks --config template_2_candles_pools_screener.yml
python cli.py run-tasks --config template_3_periodic_reports.yml
python cli.py run-tasks --config template_4_notebook_execution.yml
# Run single task from config
python cli.py trigger-task --task candles_downloader --config template_1_candles_optimization.yml
python cli.py trigger-task --task market_screener --config template_2_candles_pools_screener.yml
# List available tasks
python cli.py list-tasks --config template_1_candles_optimization.yml
# Validate configuration
python cli.py validate-config --config template_3_periodic_reports.yml
# Start API server with tasks
python cli.py serve --config template_2_candles_pools_screener.yml --port 8000
"""
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Run tasks continuously
run_parser = subparsers.add_parser('run-tasks', help='Run tasks continuously')
run_parser.add_argument('--config', '-c',
default='template_1_candles_optimization.yml',
help='Configuration file name (from config/ directory)')
run_parser.add_argument('--verbose', '-v', action='store_true',
help='Enable verbose logging')
# Trigger single task
trigger_parser = subparsers.add_parser('trigger-task', help='Run a single task once')
trigger_parser.add_argument('--task', '-t', required=True,
help='Task name to trigger')
trigger_parser.add_argument('--config', '-c',
default='template_1_candles_optimization.yml',
help='Configuration file name (from config/ directory)')
trigger_parser.add_argument('--timeout', type=int, default=300,
help='Task timeout in seconds')
# Run task directly with built-in defaults
direct_parser = subparsers.add_parser('run', help='Run a task directly with built-in defaults')
direct_parser.add_argument('task_path',
help='Task module path (e.g., app.tasks.data_collection.pools_screener)')
direct_parser.add_argument('--timeout', type=int, default=600,
help='Task timeout in seconds')
# Serve API with tasks
serve_parser = subparsers.add_parser('serve', help='Start API server with background tasks')
serve_parser.add_argument('--config', '-c',
default='template_1_candles_optimization.yml',
help='Configuration file name (from config/ directory)')
serve_parser.add_argument('--port', '-p', type=int, default=8000,
help='API server port')
serve_parser.add_argument('--host', default='0.0.0.0',
help='API server host')
# List tasks
list_parser = subparsers.add_parser('list-tasks', help='List available tasks')
list_parser.add_argument('--config', '-c',
default='template_1_candles_optimization.yml',
help='Configuration file name (from config/ directory)')
# Validate config
validate_parser = subparsers.add_parser('validate-config', help='Validate task configuration')
validate_parser.add_argument('--config', '-c', required=True,
help='Configuration file name (from config/ directory)')
return parser.parse_args()
async def run_tasks(config_path: str, verbose: bool = False):
"""Run tasks continuously."""
if verbose:
logging.getLogger().setLevel(logging.DEBUG)
# Add config/ prefix if not present
if not config_path.startswith('config/') and not os.path.isabs(config_path):
config_path = f'config/{config_path}'
logger.info(f"Starting QuantsLab Task Runner v2.0")
logger.info(f"Config: {config_path}")
try:
# Run tasks without API server (API disabled by default)
runner = TaskRunner(config_path=config_path, enable_api=False)
await runner.start()
except KeyboardInterrupt:
logger.info("Shutting down...")
except Exception as e:
logger.error(f"Error running tasks: {e}")
sys.exit(1)
async def trigger_task(task_name: str, config_path: str, timeout: int):
"""Trigger a single task."""
# Add config/ prefix if not present
if not config_path.startswith('config/') and not os.path.isabs(config_path):
config_path = f'config/{config_path}'
logger.info(f"Triggering task: {task_name}")
logger.info(f"Config: {config_path}")
logger.info(f"Timeout: {timeout}s")
try:
runner = TaskRunner(config_path=config_path)
# Setup storage and orchestrator
storage_config = runner._get_storage_config()
from core.tasks.storage import MongoDBTaskStorage
storage = MongoDBTaskStorage(storage_config)
from core.tasks.orchestrator import TaskOrchestrator
max_concurrent = runner.config.get("max_concurrent_tasks", 10)
runner.orchestrator = TaskOrchestrator(
storage=storage,
max_concurrent_tasks=max_concurrent,
retry_failed_tasks=runner.config.get("retry_failed_tasks", True)
)
# Initialize tasks
tasks = await runner._initialize_tasks()
for task in tasks:
runner.orchestrator.add_task(task)
# Trigger specific task
result = await runner.orchestrator.execute_task(
task_name=task_name,
force=True
)
if result:
logger.info(f"Task {task_name} completed with status: {result.status}")
if result.error_message:
logger.error(f"Error: {result.error_message}")
sys.exit(1)
else:
logger.error(f"Task {task_name} not found or could not be executed")
sys.exit(1)
except Exception as e:
logger.error(f"Error triggering task: {e}")
sys.exit(1)
async def serve_api(config_path: str, host: str, port: int):
"""Start API server with background tasks."""
# Add config/ prefix if not present
if not config_path.startswith('config/') and not os.path.isabs(config_path):
config_path = f'config/{config_path}'
logger.info(f"Starting QuantsLab API Server")
logger.info(f"Config: {config_path}")
logger.info(f"Server: http://{host}:{port}")
try:
# Create runner with API enabled and configure host/port
runner = TaskRunner(config_path=config_path, enable_api=True)
runner.api_host = host
runner.api_port = port
await runner.start()
except KeyboardInterrupt:
logger.info("Shutting down...")
except Exception as e:
logger.error(f"Error running server: {e}")
sys.exit(1)
async def run_task_direct(task_path: str, timeout: int):
"""Run a task directly using its built-in main() function."""
logger.info(f"Running task directly: {task_path}")
logger.info(f"Timeout: {timeout}s")
try:
# Import the task module
import importlib
module = importlib.import_module(task_path)
if not hasattr(module, 'main'):
logger.error(f"Task module {task_path} does not have a main() function")
sys.exit(1)
# Execute with timeout
await asyncio.wait_for(module.main(), timeout=timeout)
logger.info(f"Task {task_path} completed successfully")
except asyncio.TimeoutError:
logger.error(f"Task {task_path} timed out after {timeout} seconds")
sys.exit(1)
except ImportError as e:
logger.error(f"Failed to import task {task_path}: {e}")
sys.exit(1)
except Exception as e:
logger.error(f"Error running task {task_path}: {e}")
sys.exit(1)
def list_tasks(config_path: str):
"""List available tasks from configuration."""
# Add config/ prefix if not present
if not config_path.startswith('config/') and not os.path.isabs(config_path):
config_path = f'config/{config_path}'
logger.info(f"Loading tasks from: {config_path}")
try:
if not os.path.exists(config_path):
logger.error(f"Config file not found: {config_path}")
sys.exit(1)
runner = TaskRunner(config_path=config_path)
tasks_config = runner.load_config()
print("\nAvailable Tasks:")
print("=" * 50)
for task_name, task_config in tasks_config.get('tasks', {}).items():
enabled = task_config.get('enabled', True)
status = "✓ enabled" if enabled else "✗ disabled"
task_class = task_config.get('task_class', 'Unknown')
schedule = task_config.get('schedule', {})
schedule_info = f"({schedule.get('type', 'unknown')})"
print(f"{task_name:30} {status:12} {task_class} {schedule_info}")
except Exception as e:
logger.error(f"Error listing tasks: {e}")
sys.exit(1)
def validate_config(config_path: str):
"""Validate task configuration file."""
# Add config/ prefix if not present
if not config_path.startswith('config/') and not os.path.isabs(config_path):
config_path = f'config/{config_path}'
logger.info(f"Validating config: {config_path}")
try:
if not os.path.exists(config_path):
logger.error(f"Config file not found: {config_path}")
sys.exit(1)
runner = TaskRunner(config_path=config_path)
tasks_config = runner.load_config()
# Validate each task config
errors = []
for task_name, task_config in tasks_config.get('tasks', {}).items():
try:
TaskConfig(**task_config, name=task_name)
except Exception as e:
errors.append(f"Task {task_name}: {e}")
if errors:
logger.error("Validation errors found:")
for error in errors:
logger.error(f" - {error}")
sys.exit(1)
else:
logger.info("✓ Config is valid")
except Exception as e:
logger.error(f"✗ Config validation failed: {e}")
sys.exit(1)
async def main():
args = parse_args()
if args.command == 'run-tasks':
await run_tasks(args.config, args.verbose)
elif args.command == 'trigger-task':
await trigger_task(args.task, args.config, args.timeout)
elif args.command == 'run':
await run_task_direct(args.task_path, args.timeout)
elif args.command == 'serve':
await serve_api(args.config, args.host, args.port)
elif args.command == 'list-tasks':
list_tasks(args.config)
elif args.command == 'validate-config':
validate_config(args.config)
else:
logger.error("No command specified. Use --help for usage.")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())