Question:
You are working on a project that requires automatic clean-up of older records in a database every day at a specific time. The clean-up task should be an Azure function that gets triggered at the specified time. The target database has high-throughput transactional capacity, and tunable consistency levels, can you delineate a solution for this task? Please provide the triggers and bindings in the given code.
[FunctionName("CleanUpOldRecords")]
public static void Run(
/* Triggers and Bindings here */
ILogger log)
{
// Loop over oldRecords
// For each record, mark it for deletion or update it as per your logic
// Add the updated records to database
}
Answer:
A Timer trigger would be the optimal selection in this scenario. This allows for the scheduling of an Azure function to execute at a specified time each day. The function should utilize input and output bindings to Azure Cosmos DB, which provides specified capabilities. Its role would be to retrieve and clear out older records.
[FunctionName("CleanUpOldRecords")]
public static void Run(
[TimerTrigger("0 0 * * *")] TimerInfo myTimer,
[CosmosDB("<database-name>", "<collection-name>", ConnectionStringSetting = "CosmosDBConnectionString", SqlQuery = "<SQL-query-for-old-records>")]
IReadOnlyList<dynamic> oldRecords,
[CosmosDB("<database-name>", "<collection-name>", ConnectionStringSetting = "CosmosDBConnectionString")]
IAsyncCollector<dynamic> outputDocuments,
ILogger log)
{
// Loop over oldRecords
// For each record, mark it for deletion or update it as per your logic
// Add the updated records to database
}