skip to Main Content

I have an azure function deployed which is giving this exception as follows:

[![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]

Along with the above exception, I am keep getting 502 or 503 error after some intervals and then it return to its ok response i.e 200.

I am deploying the azure function via the following yml file:



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

requirements.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

runtime version: 4
python version: 3.9

I am trying to figure out what might be causing this exception, but the exception is persistent.

Any help would be greatly appreciated!

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)

2

Answers


  1. Chosen as BEST ANSWER

    Just to help other who maybe facing such error where your function app is giving responses between 502 or 503 intermittently.

    Follow the following steps:

    1. Troubleshoot the issue using the diagnostic tool and solve problems through azure portal

    2. If above didn't gave you any insights, try looking into the runtime logs using log streaming found in left menu.

    3. (Only for those who has subscribe to premium provision)If you're still not getting any insight or leads that may help resolve issue, check the Kudu console.Link to Console : http://<your-function-name>.scm.azurewebsites.net(check logs or memory consumption)

    4. If you're still not getting any insights or unable to find exception, Then it might be region specific.Your server location might be experiencing some problem.

    Try migrating your app to new region (which is bit handy) or contact support team.

    I am attaching some doc for your reference:

    1. Azure function returns 503 after 30 seconds

    2. https://learn.microsoft.com/en-us/answers/questions/1287868/how-to-solve-azure-functions-runtime-is-unreachabl

    3. https://github.com/Azure/azure-functions-python-worker/issues/1567

      https://github.com/Azure/azure-functions-host/issues/8583


  2. Check if you have BlobSource under .venv/Lib/Site-packages/azure/functions/decorators/init.py:

    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'
    ]
    
    

    I have created a Python Azure function using your modules in requirements.txt and able to deploy the function to Azure using GitHub actions.

    requirements.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
    

    Workflow:

    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
    

    Deployment Status in GitHub:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search