This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import time
|
||||
import logging
|
||||
from django.core.management.base import BaseCommand
|
||||
from ai_services.models import TranscriptionTask
|
||||
from ai_services.services import AliyunTingwuService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Polls Aliyun Tingwu for transcription results every 10 seconds'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write(self.style.SUCCESS('Starting polling service...'))
|
||||
service = AliyunTingwuService()
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Find tasks that are PENDING or PROCESSING
|
||||
# Include PENDING because create() might set it to PENDING initially
|
||||
# though usually it sets to PROCESSING if task_id is obtained.
|
||||
# Just in case.
|
||||
tasks = TranscriptionTask.objects.filter(
|
||||
status__in=[TranscriptionTask.Status.PENDING, TranscriptionTask.Status.PROCESSING]
|
||||
).exclude(task_id__isnull=True).exclude(task_id='')
|
||||
|
||||
count = tasks.count()
|
||||
if count > 0:
|
||||
self.stdout.write(f'Found {count} pending/processing tasks.')
|
||||
|
||||
for task in tasks:
|
||||
self.stdout.write(f'Checking task {task.task_id} (Status: {task.status})...')
|
||||
try:
|
||||
result = service.get_task_info(task.task_id)
|
||||
|
||||
# Store old status to check for changes
|
||||
old_status = task.status
|
||||
|
||||
service.parse_and_update_task(task, result)
|
||||
|
||||
# Re-fetch or check updated object
|
||||
if task.status != old_status:
|
||||
if task.status == TranscriptionTask.Status.SUCCEEDED:
|
||||
self.stdout.write(self.style.SUCCESS(f'Task {task.task_id} SUCCEEDED'))
|
||||
elif task.status == TranscriptionTask.Status.FAILED:
|
||||
self.stdout.write(self.style.ERROR(f'Task {task.task_id} FAILED: {task.error_message}'))
|
||||
else:
|
||||
# Still processing
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking task {task.task_id}: {e}")
|
||||
self.stdout.write(self.style.ERROR(f"Error checking task {task.task_id}: {e}"))
|
||||
|
||||
# Wait for 10 seconds
|
||||
time.sleep(10)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
self.stdout.write(self.style.SUCCESS('Stopping polling service...'))
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f"Polling loop error: {e}")
|
||||
self.stdout.write(self.style.ERROR(f"Polling loop error: {e}"))
|
||||
time.sleep(10)
|
||||
Reference in New Issue
Block a user