I am a beginner in azure functions and azure as whole. I want my piece of code to run as per my schedule for hourly or a daily once basis, so chose to go ahead with azure functions.
My objective is to take the data from an API and then save it to the cosmos db mongo api in azure.
I am able to achieve my objective through normal python codes, but when i integrate it into azure functions. I am finding it difficult to run it.
Below is my code for your reference.
__init__.py
print("Starting Importing Libraries")
import os
import pandas as pd
import numpy as np
import requests
import json
import pymongo
from azure.storage.blob import BlockBlobService
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.datafactory import DataFactoryManagementClient
from azure.mgmt.datafactory.models import *
from datetime import datetime, timedelta
import time
import azure.functions as func
print("Completing Importing Libraries")
def execute_timer():
#Business Logic to take the data from API and insert it to mongo db.
data_insert = mycol.insert_many(final_df)
return data_insert
def main(mytimer: func.TimerRequest, outdoc: func.Out[func.Document]):
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
if mytimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)
try:
# Get Blog feeds
outdata = {}
outdata['items'] = execute_timer()
# logging.info(outdata) # for debug
# Store output data using Cosmos DB output binding
outdoc.set(func.Document.from_json(json.dumps(outdata)))
except Exception as e:
logging.error('Error:')
logging.error(e)
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *"
},
{
"direction": "out",
"type": "cosmosDB",
"name": "outdoc",
"databaseName": "DB",
"collectionName": "example_test",
"leaseCollectionName": "leases",
"createLeaseCollectionIfNotExists": true,
"connectionStringSetting": "mongodb://*****",
"createIfNotExists": true
}
]
}
When i run it after deployment in azure i.e. test+run:
below error is obtained:
020-08-20T04:48:18Z [Information] Executing 'Functions.TimerTrigger_2' (Reason='This function was programmatically called via the host APIs.', Id=123586b0-41d5-4ebc-87eb-bee1c8cd3ae2)
2020-08-20T04:48:18Z [Verbose] Sending invocation id:123586b0-41d5-4ebc-87eb-bee1c8cd3ae2
2020-08-20T04:48:18Z [Verbose] Posting invocation id:123586b0-41d5-4ebc-87eb-bee1c8cd3ae2 on workerId:767e33e5-dc7f-400c-977b-fdaa108904f2
2020-08-20T04:48:18Z [Error] Executed 'Functions.TimerTrigger_2' (Failed, Id=123586b0-41d5-4ebc-87eb-bee1c8cd3ae2, Duration=27ms)
2020-08-20T04:50:00Z [Information] Executing 'Functions.TimerTrigger_2' (Reason='Timer fired at 2020-08-20T04:50:00.0009948+00:00', Id=04302a29-7521-42aa-a3c0-0b6d6a139d63)
2020-08-20T04:50:00Z [Verbose] Sending invocation id:04302a29-7521-42aa-a3c0-0b6d6a139d63
2020-08-20T04:50:00Z [Verbose] Posting invocation id:04302a29-7521-42aa-a3c0-0b6d6a139d63 on workerId:767e33e5-dc7f-400c-977b-fdaa108904f2
Also, in my local when i do run it. It is not navigating to execute_timer() for execution.
Is there anything which i am doing here ?
Also, when i try to start the host function in terminal window as "func host start" then below error is obtained in window.
func : The term 'func' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ func host start
+ ~~~~
+ CategoryInfo : ObjectNotFound: (func:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Is there anything which i am doing here or i need to do something otherwise. Please suggest!