-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_task_processor.py
More file actions
392 lines (316 loc) Β· 15 KB
/
background_task_processor.py
File metadata and controls
392 lines (316 loc) Β· 15 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
Background Task Processor for Parallel Command Execution
This module implements Option 3 from improve.md - Immediate Response with Background Processing.
Commands are acknowledged immediately while actual processing happens in parallel background tasks.
"""
import asyncio
import time
import uuid
import logging
from typing import Set, Dict, Optional, Callable, Any
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from plugins.universal_plugin_base import CommandContext
class TaskStatus(Enum):
"""Task execution status"""
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
TIMEOUT = "timeout"
@dataclass
class BackgroundTask:
"""Represents a background command task"""
task_id: str
context: CommandContext
command: str
plugin_name: str
status: TaskStatus
created_at: datetime
started_at: Optional[datetime] = None
completed_at: Optional[datetime] = None
result: Optional[str] = None
error: Optional[str] = None
asyncio_task: Optional[asyncio.Task] = None
class BackgroundTaskProcessor:
"""
Processes commands in parallel background tasks with immediate user feedback.
Features:
- Immediate command acknowledgment
- Parallel execution of all commands
- Task tracking and status updates
- Configurable timeouts
- Resource monitoring
- Error isolation
"""
def __init__(self,
send_message_callback: Callable[[str, str], None],
default_timeout: int = 300, # 5 minutes default
max_concurrent_tasks: int = 50):
"""
Initialize the background task processor.
Args:
send_message_callback: Function to send messages back to users
default_timeout: Default timeout for commands in seconds
max_concurrent_tasks: Maximum number of concurrent background tasks
"""
self.send_message_callback = send_message_callback
self.default_timeout = default_timeout
self.max_concurrent_tasks = max_concurrent_tasks
# Task tracking
self.background_tasks: Set[asyncio.Task] = set()
self.active_tasks: Dict[str, BackgroundTask] = {}
self.completed_tasks: Dict[str, BackgroundTask] = {}
# Statistics
self.total_tasks = 0
self.successful_tasks = 0
self.failed_tasks = 0
self.timed_out_tasks = 0
# Configuration
self.task_timeouts = {
# Fast commands (should complete quickly)
"ping": 5,
"help": 10,
"status": 15,
"commands": 10,
# Medium commands
"plugins": 30,
"nist": 30,
"contacts": 30,
"groups": 30,
# Slow commands (can take time)
"loupe": 600, # 10 minutes for web scraping
"youtube": 300, # 5 minutes for video processing
"ai": 120, # 2 minutes for AI responses
"ask": 120, # 2 minutes for AI with context
"advice": 60, # 1 minute for advice
"song": 90, # 1.5 minutes for song generation
"transcribe": 180, # 3 minutes for audio transcription
}
self.logger = logging.getLogger("background_processor")
self.logger.info("π Background Task Processor initialized")
async def submit_command(self,
context: CommandContext,
plugin_name: str,
command_handler: Callable[[CommandContext], Any]) -> str:
"""
Submit a command for background processing with immediate acknowledgment.
Args:
context: Command context with user and message info
plugin_name: Name of the plugin handling the command
command_handler: Async function that executes the command
Returns:
Task ID for tracking
"""
# Check if we're at capacity
if len(self.background_tasks) >= self.max_concurrent_tasks:
return await self._handle_capacity_exceeded(context)
# Generate unique task ID
task_id = str(uuid.uuid4())
short_id = task_id[:8]
# Create task record
task_record = BackgroundTask(
task_id=task_id,
context=context,
command=context.command,
plugin_name=plugin_name,
status=TaskStatus.PENDING,
created_at=datetime.now()
)
self.active_tasks[task_id] = task_record
self.total_tasks += 1
# Send immediate acknowledgment to user
await self._send_immediate_response(context, short_id)
# Start background processing
asyncio_task = asyncio.create_task(
self._process_in_background(task_record, command_handler)
)
task_record.asyncio_task = asyncio_task
# Track the task for cleanup
self.background_tasks.add(asyncio_task)
asyncio_task.add_done_callback(lambda t: self.background_tasks.discard(t))
self.logger.info(f"π€ Submitted task {short_id} for command '{context.command}' from {context.user_display_name}")
return task_id
async def _send_immediate_response(self, context: CommandContext, short_id: str):
"""Send immediate acknowledgment to user"""
emoji_map = {
"loupe": "π",
"youtube": "πΊ",
"ai": "π€",
"ask": "π€",
"nist": "π²",
"advice": "π‘",
"song": "π΅",
"transcribe": "ποΈ",
"ping": "π",
"help": "β",
}
emoji = emoji_map.get(context.command, "β³")
# Estimate completion time based on command type
timeout = self.task_timeouts.get(context.command, self.default_timeout)
if timeout <= 30:
time_msg = "shortly"
elif timeout <= 120:
time_msg = "in 1-2 minutes"
elif timeout <= 300:
time_msg = "in 2-5 minutes"
else:
time_msg = "in several minutes"
response = f"{emoji} **Processing `{context.command}`**\n" \
f"Task ID: `{short_id}`\n" \
f"Expected completion: {time_msg}\n" \
f"You can continue using other commands while this processes."
await self.send_message_callback(context.chat_id, response)
async def _process_in_background(self,
task_record: BackgroundTask,
command_handler: Callable[[CommandContext], Any]):
"""Execute command in background with error handling and timeout"""
task_record.status = TaskStatus.RUNNING
task_record.started_at = datetime.now()
short_id = task_record.task_id[:8]
command = task_record.command
context = task_record.context
self.logger.info(f"π Starting background execution for task {short_id} ({command})")
try:
# Get timeout for this specific command
timeout = self.task_timeouts.get(command, self.default_timeout)
# Execute command with timeout
result = await asyncio.wait_for(
command_handler(context),
timeout=timeout
)
# Handle successful completion
task_record.status = TaskStatus.COMPLETED
task_record.completed_at = datetime.now()
task_record.result = result
await self._send_completion_response(task_record)
self.successful_tasks += 1
duration = (task_record.completed_at - task_record.started_at).total_seconds()
self.logger.info(f"β
Task {short_id} completed successfully in {duration:.1f}s")
except asyncio.TimeoutError:
# Handle timeout
task_record.status = TaskStatus.TIMEOUT
task_record.completed_at = datetime.now()
task_record.error = f"Command timed out after {timeout} seconds"
await self._send_timeout_response(task_record)
self.timed_out_tasks += 1
self.logger.warning(f"β° Task {short_id} timed out after {timeout}s")
except Exception as e:
# Handle other errors
task_record.status = TaskStatus.FAILED
task_record.completed_at = datetime.now()
task_record.error = str(e)
await self._send_error_response(task_record)
self.failed_tasks += 1
self.logger.error(f"β Task {short_id} failed: {e}", exc_info=True)
finally:
# Move task to completed list and clean up
self._cleanup_task(task_record)
async def _send_completion_response(self, task_record: BackgroundTask):
"""Send successful completion response"""
short_id = task_record.task_id[:8]
duration = (task_record.completed_at - task_record.started_at).total_seconds()
# Add completion header with timing info
completion_header = f"β
**Task `{short_id}` completed** ({duration:.1f}s)\n\n"
# Combine header with actual result
full_response = completion_header + (task_record.result or "Command completed successfully.")
await self.send_message_callback(task_record.context.chat_id, full_response)
async def _send_timeout_response(self, task_record: BackgroundTask):
"""Send timeout response"""
short_id = task_record.task_id[:8]
timeout = self.task_timeouts.get(task_record.command, self.default_timeout)
response = f"β° **Task `{short_id}` timed out**\n\n" \
f"Command `{task_record.command}` exceeded {timeout} second limit.\n" \
f"This may indicate the command is taking longer than expected or encountered an issue.\n" \
f"You can try running the command again."
await self.send_message_callback(task_record.context.chat_id, response)
async def _send_error_response(self, task_record: BackgroundTask):
"""Send error response"""
short_id = task_record.task_id[:8]
response = f"β **Task `{short_id}` failed**\n\n" \
f"Command `{task_record.command}` encountered an error:\n" \
f"```\n{task_record.error}\n```\n" \
f"Please check your command syntax and try again."
await self.send_message_callback(task_record.context.chat_id, response)
async def _handle_capacity_exceeded(self, context: CommandContext) -> str:
"""Handle when maximum concurrent tasks is exceeded"""
response = f"π« **System at capacity**\n\n" \
f"Maximum number of concurrent tasks ({self.max_concurrent_tasks}) reached.\n" \
f"Please wait for some tasks to complete and try again.\n" \
f"Use `!status` to see current system load."
await self.send_message_callback(context.chat_id, response)
return "capacity_exceeded"
def _cleanup_task(self, task_record: BackgroundTask):
"""Move completed task to history and clean up active tracking"""
task_id = task_record.task_id
# Move to completed tasks (keep last 100 for history)
self.completed_tasks[task_id] = task_record
if len(self.completed_tasks) > 100:
# Remove oldest completed task
oldest_id = min(self.completed_tasks.keys(),
key=lambda k: self.completed_tasks[k].completed_at)
del self.completed_tasks[oldest_id]
# Remove from active tasks
if task_id in self.active_tasks:
del self.active_tasks[task_id]
def get_status(self) -> Dict[str, Any]:
"""Get current processor status and statistics"""
active_count = len(self.active_tasks)
pending_count = sum(1 for t in self.active_tasks.values() if t.status == TaskStatus.PENDING)
running_count = sum(1 for t in self.active_tasks.values() if t.status == TaskStatus.RUNNING)
return {
"active_tasks": active_count,
"pending_tasks": pending_count,
"running_tasks": running_count,
"background_asyncio_tasks": len(self.background_tasks),
"capacity_used_percent": (active_count / self.max_concurrent_tasks) * 100,
"total_processed": self.total_tasks,
"successful": self.successful_tasks,
"failed": self.failed_tasks,
"timed_out": self.timed_out_tasks,
"success_rate": (self.successful_tasks / max(self.total_tasks, 1)) * 100,
}
def get_active_tasks(self) -> Dict[str, Dict[str, Any]]:
"""Get information about currently active tasks"""
result = {}
for task_id, task in self.active_tasks.items():
short_id = task_id[:8]
running_time = (datetime.now() - task.created_at).total_seconds()
result[short_id] = {
"command": task.command,
"plugin": task.plugin_name,
"status": task.status.value,
"user": task.context.user_display_name,
"chat": task.context.chat_id,
"running_time": f"{running_time:.1f}s",
"timeout": self.task_timeouts.get(task.command, self.default_timeout)
}
return result
async def cancel_task(self, task_id_or_short: str) -> bool:
"""Cancel a running task by ID or short ID"""
# Find task by full ID or short ID
task_record = None
for tid, task in self.active_tasks.items():
if tid == task_id_or_short or tid.startswith(task_id_or_short):
task_record = task
break
if not task_record:
return False
# Cancel the asyncio task
if task_record.asyncio_task and not task_record.asyncio_task.done():
task_record.asyncio_task.cancel()
# Update task record
task_record.status = TaskStatus.FAILED
task_record.completed_at = datetime.now()
task_record.error = "Task cancelled by user"
# Notify user
short_id = task_record.task_id[:8]
response = f"π **Task `{short_id}` cancelled**\n\nCommand `{task_record.command}` was cancelled."
await self.send_message_callback(task_record.context.chat_id, response)
# Clean up
self._cleanup_task(task_record)
self.failed_tasks += 1
self.logger.info(f"π Task {short_id} cancelled by user")
return True
return False