azure 函数在一定时间间隔后响应 503 或 502 错误

问题描述 投票:0回答:1

我部署了一个天蓝色函数,该函数给出了此异常,如下所示:

[![

Unable to reload azure.functions. Using default. Exception: cannot import name 'BlobSource' from 'azure.functions.decorators' (/azure-functions-host/workers/python/3.9/LINUX/X64/azure/functions/decorators/init.py)
][1]][1]

除了上述异常之外,一段时间后我不断收到 502 或 503 错误,然后返回到正常响应,即 200。

我通过以下 yml 文件部署 azure 功能:



name: Deploy Python project to Azure Function App



on:

  push:

    branches: ["main"]



env:

  AZURE_FUNCTIONAPP_NAME: 'my-function-name'   

  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.'       

  PYTHON_VERSION: '3.9'                     



jobs:

  test:

    runs-on: ubuntu-latest



    steps:

    - uses: actions/checkout@v4

    - name: Set up Python 3.9

      uses: actions/setup-python@v3

      with:

        python-version: ${{ env.PYTHON_VERSION }}

    - name: Install dependencies

      run: |

        python -m pip install --upgrade pip

        pip install flake8 pytest

        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

    - name: Test with pytest

      run: |

        pytest --disable-warnings



  build-and-deploy:

    runs-on: ubuntu-latest

    needs: test

    if: ${{ needs.test.result == 'success' }}

    environment: dev

    steps:

    - name: 'Checkout GitHub Action'

      uses: actions/checkout@v4



    - name: Setup Python ${{ env.PYTHON_VERSION }} Environment

      uses: actions/setup-python@v4

      with:

        python-version: ${{ env.PYTHON_VERSION }}



    - name: 'Resolve Project Dependencies Using Pip'

      shell: bash

      run: |

        pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'

        python -m pip install --upgrade pip

        pip install -r requirements.txt --target=".python_packages/lib/site-packages"

        popd



    - name: 'Run Azure Functions Action'

      uses: Azure/functions-action@v1

      id: fa

      with:

        app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}

        package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}

        publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }} # Remove publish-profile to use Azure RBAC

        scm-do-build-during-deployment: true

        enable-oryx-build: true

需求.txt



azure-functions==1.20.0

pandas==2.2.2

pydantic==2.8.2

pydantic_core==2.20.1

pytest==8.3.2

pytest-asyncio==0.23.8

requests==2.32.3

运行版本:4 蟒蛇版本:3.9

我试图找出可能导致此异常的原因,但该异常是持久的。

任何帮助将不胜感激!

function_app.py

import azure.functions as func
import logging
from src.funcmain import *

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="register-account",methods=['POST'])
async def account_registration(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')

    try:
        return await register_account(req=req)
    
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error :{str(e)}", status_code=500)
    
@app.route(route="contact",methods=['POST'])
async def contact(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')

    try:
        if req.params.get('action') == 'update':
            return await update_contact(req=req)
        
        elif req.params.get('action') == 'create':
            return await register_contact(req=req)
        
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error :{str(e)}", status_code=500)

@app.route(route="lead", methods=['POST'])
async def lead_operation(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')
    try:
        if req.params.get('action') == 'update':
            return await update_lead(req)
        elif req.params.get('action') == 'create':
            return await create_lead(req)
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error: {str(e)}", status_code=500)

@app.route(route="offer", methods=['POST'])
async def offer_operation(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')
    try:
        if req.params.get('action') == 'create':
            return await create_offer(req)
        elif req.params.get('action') == 'update':
            return await update_offer(req)
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error: {str(e)}", status_code=500)

@app.route(route="watchlist", methods=['POST'])
async def watchlist_operation(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Request received from {req.url}')
    try:
        if req.params.get('action') == 'create':
            return await add_watchlist(req)
        elif req.params.get('action') == 'update':
            return await update_watchlist(req)
        else:
            return func.HttpResponse(f"Invalid action: {req.params.get('action')}", status_code=400)
    except Exception as e:
        logging.error(f'Error processing request: {str(e)}')
        return func.HttpResponse(f"Internal server error: {str(e)}", status_code=500)

@app.route(route="ping", methods=['GET'])
async def ping(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Ping request received.')
    return func.HttpResponse("Service is Up!", status_code=200)
python-3.x azure azure-functions dependencies continuous-deployment
1个回答
0
投票

检查.venv/Lib/Site-packages/azure/functions/decorators/init.py下是否有

BlobSource

from .core import Cardinality, AccessRights
from .function_app import FunctionApp, Function, DecoratorApi, DataType, \
    AuthLevel, Blueprint, ExternalHttpFunctionApp, AsgiFunctionApp, \
    WsgiFunctionApp, FunctionRegister, TriggerApi, BindingApi, \
    SettingsApi, BlobSource
from .http import HttpMethod

__all__ = [
    'FunctionApp',
    'Function',
    'FunctionRegister',
    'DecoratorApi',
    'TriggerApi',
    'BindingApi',
    'SettingsApi',
    'Blueprint',
    'ExternalHttpFunctionApp',
    'AsgiFunctionApp',
    'WsgiFunctionApp',
    'DataType',
    'AuthLevel',
    'Cardinality',
    'AccessRights',
    'HttpMethod',
    'BlobSource'
]

我已使用

requirements.txt
中的模块创建了一个 Python Azure 函数,并且能够使用 GitHub 操作将该函数部署到 Azure。

需求.txt:

azure-functions==1.20.0
pandas==2.2.2
pydantic==2.8.2
pydantic_core==2.20.1
pytest==8.3.2
pytest-asyncio==0.23.8
requests==2.32.3

工作流程:

name: Build and deploy Python project to Azure Function App - appname

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  PYTHON_VERSION: '3.9' # set this to the python version to use (supports 3.6, 3.7, 3.8)

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Python version
        uses: actions/setup-python@v1
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate
      - name: Install dependencies
        run: pip install -r requirements.txt

      # Optional: Add step to run tests here

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: python-app
          path: |
            release.zip
            !venv/
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-function.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: python-app

      - name: Unzip artifact for deployment
        run: unzip release.zip

      - name: 'Deploy to Azure Functions'
        uses: Azure/functions-action@v1
        id: deploy-to-function
        with:
          app-name: 'appname'
          slot-name: 'Production'
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_8212BBXXXD0E6 }}
          scm-do-build-during-deployment: true
          enable-oryx-build: true

GitHub 中的部署状态:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.