Asynchronous code not completing child threads on AWS lambda .net core 1.1

860 Views Asked by At

I am currently running my application using AWS lambda on .net core 1.1

I am finding that when I run the method below, that the final log points (_logger.Info()) and any that happen afterwards in the calling code are not completing.

I have deliberately left the "await" off the log points as I do not want the application to wait until the log is completed before moving onto the next statement as they are web calls to a third party logging service, but I do want all the logging threads to complete before the entire process is complete.

If I put "await" before all the _logger.Info then all the logs are run to completion which suggests that the code works when every method awaits the execution of the last method.

Its as if the AWS Lambda is saying that the main thread is complete therefore stop the entire process even though the logging points that are spawned to run as async are not completed yet.

I have done some research and see that .net core 2.0 has transactions (https://learn.microsoft.com/en-us/dotnet/api/system.transactions.transaction?view=netcore-2.0) but this doesnt appear to be supported by .net core 1.1

Is there any way to tell AWS Lambda to wait until all the spawned threads have been completed successfully before completing? If so could you provide me with an example?

Is something else going on here that I have misunderstood?

Here is the code:

    private async Task LoadExhibitor(JObject input)
    {
        // Retrieve Data
        _logger.Info("Retrieving Data");

        // Set some variables
        ....

        if (rawExhibitor.Error != null)
        {
            _logger.Warn($"No exhibitor returned from ...");

            // Some error handling
            ...
            return;
        }

        // Transform some information to another object
        _logger.Info("Transforming exhibitor to a friendly object");
        var exhibitor = await someTransformer.Transform(rawExhibitor)

        _logger.Info($"Saving to elastic search ...");

        // Save
        await repository.SaveAsync(exhibitor, eventEditionId, locale, sessionId);

        _logger.Info($"Saving to elastic search  has completed");

    }
0

There are 0 best solutions below