My use case is the following:
- F1: generate some data and write them to a CosmosDB (using a time trigger)
- F2: read the data that have just been written and add a username
- Orchestrator: control the workflow and call F2 after F1 is done
My problem: only F1 works, but F2 is not triggered at all. Why? Does F1 have to return a trigger or something?
That's how I know that only F1 is executed:
F1
import logging
import hashlib
import time
import datetime
from azure.cosmos import CosmosClient
import azure.functions as func
def generate_id(string=None, length=10):
'''This function generates a hash id to be attached to each new row'''
ts = time.time()
guid = hashlib.shake_128((str(string) + str(ts)).encode()).hexdigest(10)
return guid
def main(mytimer: func.TimerRequest, outputDocument: func.Out[func.Document]) -> None:
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)
result1 = {
"first_letter": "A",
"second_letter": "B",
"third_letter": "C",
"score": 0.001,
}
result1['id'] = generate_id()
outputDocument.set(func.Document.from_dict(result1))
return
F1 function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "*/30 * * * * *"
},
{
"type": "cosmosDB",
"direction": "out",
"name": "outputDocument",
"databaseName": myCosmosDB,
"collectionName": myContainer,
"createIfNotExists": "true",
"connectionStringSetting": myConnString,
"partitionKey": "id"
}
]
}
F2
import logging
import azure.functions as func
from azure.cosmos import CosmosClient
def add_username(string=None):
'''Generate username'''
name = "MyName"
surname = "MySurname"
username = name+" "+surname
return username
def main(F1activitytrigger, inputDocument: func.DocumentList) -> str:
if inputDocument:
logging.info('Document id: %s', inputDocument[0]['id'])
result2 = inputDocument[0].data
result2['username'] = add_username()
return result2
F2 function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "F1activitytrigger",
"type": "activityTrigger",
"direction": "in"
},
{
"type": "cosmosDB",
"direction": "in",
"name": "inputDocument",
"databaseName": myCosmosDB,
"collectionName": myContainer,
"createIfNotExists": "true",
"connectionStringSetting": myConnString,
"partitionKey": "id"
}
]
}
Orchestrator
import logging
import json
import azure.functions as func
import azure.durable_functions as df
def orchestrator_function(context: df.DurableOrchestrationContext):
result1 = yield context.call_activity('Test-F1')
result2 = yield context.call_activity('Test-F2')
return [result1, result2]
main = df.Orchestrator.create(orchestrator_function)
Orchestrator function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "context",
"type": "orchestrationTrigger",
"direction": "in"
}
]
}