""" 补丁脚本:让 celery_app.send_task() 调用在无 broker 时优雅降级。 用字符串替换而非正则,确保缩进正确。 """ filepath = '/app/backend/v2/api.py' with open(filepath, 'r') as f: content = f.read() # 四处 send_task 调用,逐一替换 replacements = [ ( " celery_app.send_task('wgjy.grid_backtest_file', args=[task_id, user_id, file_path, parsed_params], queue='queue_backtest')\n\n return {'task_id': task_id, 'status': 'queued'}", " try:\n celery_app.send_task('wgjy.grid_backtest_file', args=[task_id, user_id, file_path, parsed_params], queue='queue_backtest')\n except Exception:\n pass\n return {'task_id': task_id, 'status': 'queued'}" ), ( " celery_app.send_task('wgjy.dca_backtest_file', args=[task_id, user_id, file_path, parsed_params], queue='queue_backtest')\n\n return {'task_id': task_id, 'status': 'queued'}", " try:\n celery_app.send_task('wgjy.dca_backtest_file', args=[task_id, user_id, file_path, parsed_params], queue='queue_backtest')\n except Exception:\n pass\n return {'task_id': task_id, 'status': 'queued'}" ), ( " celery_app.send_task('wgjy.download_task', args=[task_id, user_id, params], queue='queue_download')\n task_ids.append(task_id)", " try:\n celery_app.send_task('wgjy.download_task', args=[task_id, user_id, params], queue='queue_download')\n except Exception:\n pass\n task_ids.append(task_id)" ), ( " celery_app.send_task('wgjy.export_backtest_excel', args=[task_id, user_id, record_id], queue='queue_export')\n return {'task_id': task_id, 'status': 'queued'}", " try:\n celery_app.send_task('wgjy.export_backtest_excel', args=[task_id, user_id, record_id], queue='queue_export')\n except Exception:\n pass\n return {'task_id': task_id, 'status': 'queued'}" ), ] count = 0 for old, new in replacements: if old in content: content = content.replace(old, new) count += 1 print(f' ✅ 替换成功: {old.strip()[:60]}...') else: print(f' ⚠️ 未找到: {old.strip()[:60]}...') with open(filepath, 'w') as f: f.write(content) print(f'\n✅ celery_app 补丁完成,共替换 {count} 处')