-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduler.py
160 lines (143 loc) · 6.99 KB
/
scheduler.py
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
import firebase_admin
from firebase_admin import credentials, db
import logging
from datetime import datetime
import asyncio
import sys
import json
from typing import Dict, Any
import colorama
from colorama import Fore, Style
# Initialize colorama for cross-platform colored output
colorama.init()
# Configure logging with custom formatter
class ColoredFormatter(logging.Formatter):
COLORS = {
'DEBUG': Fore.CYAN,
'INFO': Fore.GREEN,
'WARNING': Fore.YELLOW,
'ERROR': Fore.RED,
'CRITICAL': Fore.RED + Style.BRIGHT
}
def format(self, record):
levelname = record.levelname
if levelname in self.COLORS:
record.levelname = f"{self.COLORS[levelname]}{levelname}{Style.RESET_ALL}"
timestamp = self.formatTime(record, self.datefmt)
return f"{timestamp} | {record.levelname:<20} | {record.threadName:<15} | {record.getMessage()}"
class DashScheduler:
def __init__(self, cred_path: str):
self._setup_logging()
self.logger.info("Initializing DASH Scheduler...")
try:
self._initialize_firebase(cred_path)
self.tasks_ref = db.reference('tasks')
self.clients_ref = db.reference('presence')
self.logger.info("Successfully connected to Firebase")
except Exception as e:
self.logger.critical(f"Failed to initialize Firebase: {e}")
raise
def _setup_logging(self):
self.logger = logging.getLogger('DashScheduler')
self.logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(ColoredFormatter('%(asctime)s.%(msecs)03d'))
file_handler = logging.FileHandler('dashscheduler.log')
file_handler.setFormatter(logging.Formatter('%(asctime)s | %(levelname)-8s | %(threadName)-15s | %(message)s'))
self.logger.addHandler(console_handler)
self.logger.addHandler(file_handler)
def _initialize_firebase(self, cred_path: str):
try:
firebase_admin.get_app()
self.logger.debug("Using existing Firebase app")
except ValueError:
try:
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred, {'databaseURL': "https://dash-b26cb-default-rtdb.firebaseio.com"})
self.logger.debug("Created new Firebase app")
except Exception as e:
self.logger.error(f"Failed to initialize Firebase: {str(e)}")
raise
async def update_presence(self):
self.logger.info("Starting presence monitoring...")
previous_clients = {}
while True:
try:
clients = self.clients_ref.get() or {}
active_clients = {cid: data for cid, data in clients.items() if data.get('status') == 'idle'}
self._log_client_changes(previous_clients, active_clients)
previous_clients = active_clients.copy()
if datetime.now().second == 0:
self._log_detailed_status(clients)
except Exception as e:
self.logger.error(f"Error updating presence: {str(e)}", exc_info=True)
await asyncio.sleep(30)
def _log_client_changes(self, previous: Dict[str, Any], current: Dict[str, Any]):
new_clients = set(current.keys()) - set(previous.keys())
disconnected_clients = set(previous.keys()) - set(current.keys())
for client in new_clients:
self.logger.info(f"New client connected: {client}")
for client in disconnected_clients:
self.logger.warning(f"Client disconnected: {client}")
def _log_detailed_status(self, clients: Dict[str, Any]):
total_clients = len(clients)
active_count = sum(1 for c in clients.values() if c.get('status') == 'idle')
busy_count = sum(1 for c in clients.values() if c.get('status') == 'busy')
status_msg = f"\nSystem Status Summary:\nTotal Clients: {total_clients}\nActive Clients: {active_count}\nBusy Clients: {busy_count}\n"
self.logger.info(status_msg)
async def process_tasks(self):
self.logger.info("Starting task processor...")
while True:
try:
tasks = self.tasks_ref.get() or {}
pending_tasks = {task_id: task for task_id, task in tasks.items() if task.get('status') == 'pending'}
if pending_tasks:
self.logger.debug(f"Found {len(pending_tasks)} pending tasks")
await self._process_pending_tasks(pending_tasks)
else:
self.logger.debug("No pending tasks")
except Exception as e:
self.logger.error(f"Error processing tasks: {str(e)}", exc_info=True)
await asyncio.sleep(5)
async def _process_pending_tasks(self, pending_tasks: Dict[str, Any]):
for task_id, task in pending_tasks.items():
try:
clients = self.clients_ref.get() or {}
active_clients = [cid for cid, data in clients.items() if data.get('status') == 'idle']
# if task.get('clientId') in active_clients:
# active_clients.remove(task['clientId'])
if active_clients:
assigned_client = active_clients[0]
await self._assign_task(task_id, task, assigned_client)
else:
self.logger.warning(f"No eligible clients available for task {task_id}")
except Exception as e:
self.logger.error(f"Error processing task {task_id}: {str(e)}")
async def _assign_task(self, task_id: str, task: Dict[str, Any], client: str):
try:
assignment_time = datetime.now().isoformat()
self.tasks_ref.child(task_id).update({'status': 'assigned', 'assignedTo': client, 'assignedAt': assignment_time})
self.logger.info(f"Task Assignment: ID={task_id} Client={client} Type={task.get('type', 'unknown')} Priority={task.get('priority', 'normal')}")
except Exception as e:
self.logger.error(f"Failed to assign task {task_id} to client {client}: {str(e)}")
raise
async def run(self):
self.logger.info("Starting DASH scheduler...")
try:
await asyncio.gather(self.update_presence(), self.process_tasks())
except asyncio.CancelledError:
self.logger.info("Received shutdown signal...")
except Exception as e:
self.logger.critical(f"Critical error in scheduler: {str(e)}", exc_info=True)
raise
finally:
self.logger.info("Cleaning up resources... Scheduler shutdown complete")
if __name__ == "__main__":
try:
scheduler = DashScheduler(cred_path='dash-b26cb-firebase-adminsdk-g5034-52af7a1672.json')
asyncio.run(scheduler.run())
except KeyboardInterrupt:
print("\nReceived keyboard interrupt, shutting down...")
except Exception as e:
print(f"Fatal error: {str(e)}")
sys.exit(1)