TimerTrigger
in Azure Functionspublic class Function2
{
public Function2(CosmosClient cosmosClient)
{
_container = cosmosClient.GetContainer("SampleDB", "TodoItems");
}
private readonly Container _container;
[FunctionName("Function2")]
public async Task Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
var continuationToken = await LoadContinuationTokenAsync();
var changeFeedStartFrom = continuationToken != null ? ChangeFeedStartFrom.ContinuationToken(continuationToken) : ChangeFeedStartFrom.Now();
var changeFeedIterator = _container.GetChangeFeedIterator<TodoItem>(changeFeedStartFrom, ChangeFeedMode.Incremental);
while (changeFeedIterator.HasMoreResults)
{
try
{
var items = await changeFeedIterator.ReadNextAsync();
// TODO: Implementation
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotModified)
{
continuationToken ??= ex.Headers.ContinuationToken;
break;
}
}
await SaveContinuationTokenAsync(continuationToken);
}
}
CosmosDBTrigger
proceeds to the next Change Feed when an execution error occurs.FixedDelayRetry
or ExponentialBackoffRetry
with an unlimited (-1
) maximum number of retries.
public class Function1
{
// infinity retry with 10 sec interval
[FixedDelayRetry(-1, "00:00:10")]
[FunctionName("Function1")]
public async Task Run([CosmosDBTrigger(
databaseName: "SampleDB",
collectionName: "TodoItems",
LeaseCollectionName = "leases")]
IReadOnlyList<Document> input)
{
// TODO: Implementation
}
}
public class Function1
{
// infinity retry with 10 sec interval
[FixedDelayRetry(-1, "00:00:10")]
[FunctionName("Function1")]
public async Task Run([CosmosDBTrigger(
databaseName: "SampleDB",
collectionName: "TodoItems",
LeaseCollectionName = "leases")]
IReadOnlyList<Document> input,
CancellationToken cancellationToken)
{
try
{
// Pass cancellation token
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
catch (OperationCanceledException)
{
// TODO: Implement rollback
throw;
}
}
}