Azure functions

Azure functions are small pieces of code that can be ran serverless. Because function are event driven they can be scaled easily. We used azure function to make post request to the Heroku server when a service bus is triggered. Functions can be written in many different languages like Python or JavaScript.

For more information: Azure Funcions documentation

What we need:

How create function:

Create a local function(Service bus trigger):

create function
  • choose resource group
  • select a language
  • select interpreter
  • select a template (we used Azure service Bus trigger)
  • choose a name
  • create new local app settings
  • select service bus
  • enter queue you want to use

Write your code:

import logging
import azure.functions as func
from requests import post
import json

def main(msg: func.ServiceBusMessage):
    message = msg.get_body().decode('utf-8')
    jsonString = json.loads(message)
    spots = jsonString["availableSpotNumber"]
    occupied = 12 - spots


    time = str(msg.enqueued_time_utc)
    time = int(time.split(":")[1])

    if  30 <= time <= 33 or 0 <= time <= 3:
        
            
        url = "https://project40.bravang.com/stats/parkingspot"
        headers = {
            "Authorization": "",
            "content-type": "application/json",
        }
        data = {
            "available": spots,
            "occupied": occupied
        }

        response = post(url, headers=headers, data=json.dumps(data))
        logging.info('executed: %s, response: %s', time, response)
    else:
        logging.info('not executed: %s', time)

    logging.info('occupied spots: %s', occupied)
    logging.info('available spots: %s', spots)
    # logging.info('occupied spots: %s', occupied)

Deploy your code:

deploy function
  • create a new Function app or choose existing
  • deploy code
    • check logs in output
upload settings
🛑

upload settings!! (otherwise there will be no connection from the service bus)

Use your function:

Check function app usage:

usage

Check logs and code:

functions code and logs