Home > database >  Azure Function CancellationToken
Azure Function CancellationToken

Time:02-05

I'm quite new to AzureFunctions so excuse me for maybe a simple question...

I have got the following code which is fired when I have an item on the queue

[FunctionName("ProcessCompanies")]
    public async Task ProcessCompaniesAsync([ActivityTrigger] IDurableOrchestrationContext context,
        [Queue("outqueue"), StorageAccount("AzureWebJobsStorage")] ICollector<myCompanyData> msg,
        ILogger log)
    {
        
        log.LogInformation("Getting companies");
        var companies = await _myService.GetCompaniesAsync(); //here how to pass cancellation token?
        log.LogInformation($"Found {companies.companies.Count} companies.");

        companies.companies.ForEach(msg.Add);

        log.LogInformation($"{companies.companies.Count} companies added to queue.");
    }

How can I pass to the Async call the CancellationToken? and mainly, should I pass it? in this case I perform an HTTP API request

CodePudding user response:

You can inject a CancellationToken into the function method. It is a way to get notified about a graceful shutdown of the function. See https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library?tabs=v4,cmd#cancellation-tokens

Once injected you can pass it to downstream calls. Whether you should pass it to that API is up to you. There is some short period of time before the process killed so if the call does not take long you could just try to finish it.

  • Related